Playbook变量练习

2.1 问题
本案例要求熟悉playbook进阶:

练习使用user模块添加用户
练习使用变量简化task,让play通用性更强
练习使用过滤器
2.2 步骤
实现此案例需要按照如下步骤进行。

步骤一:使用user模块添加用户,并修改密码

[root@ansible ansible]# vim user.yml

  • hosts: cache
    remote_user: root
    vars:
    username: xiaoming
    tasks:
    • name: create user “{{username}}”
      user: group=wheel uid=1000 name={{username}}
    • shell: echo 123456 | passwd --stdin xiaoming
    • shell: chage -d 0 {{username}}
      [root@ansible ansible]# ansible-playbook user.yml //执行结果
      PLAY [cache] ******************************************************************
      TASK [Gathering Facts] ********************************************************
      ok: [cache]
      TASK [create user " xiaoming "] ***********************************************
      changed: [cache]
      TASK [command] ****************************************************************
      changed: [cache]
      TASK [command] ****************************************************************
      changed: [cache]
      PLAY RECAP ********************************************************************
      cache : ok=4 changed=3 unreachable=0 failed=0
      步骤二:变量过滤器,创建一个用户,设置密码

[root@ansible ansible]# vim user1.yml

  • hosts: cache
    remote_user: root
    tasks:
    • user:
      name: lisi
      group: root
      password: “{{‘123456’ | password_hash(‘sha512’)}}”
    • shell: chage -d 0 lisi
      [root@ansible ansible]# ansible-playbook user1.yml
      PLAY [cache] ******************************************************************
      TASK [Gathering Facts] ********************************************************
      ok: [cache]
      TASK [user] *******************************************************************
      changed: [cache]
      TASK [command] ****************************************************************
      changed: [cache]
      PLAY RECAP ********************************************************************
      cache : ok=3 changed=2 unreachable=0 failed=0
      步骤三:定义一个变量创建用户

[root@ansible ansible]# vim user2.yml

  • hosts: cache
    remote_user: root
    vars:
    user: zhangs
    tasks:
    • user:
      name: “{{user}}”
      group: root
      password: “{{‘123456’ | password_hash(‘sha512’)}}”
    • shell: chage -d 0 “{{user}}”
      [root@ansible ansible]# ansible-playbook user2.yml
      PLAY [cache] ******************************************************************
      TASK [Gathering Facts] ********************************************************
      ok: [cache]
      TASK [user] *******************************************************************
      changed: [cache]
      TASK [command] ****************************************************************
      changed: [cache]
      PLAY RECAP ********************************************************************
      cache : ok=3 changed=2 unreachable=0 failed=0
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

彭淦淦

是你的双手成就了我的生活!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值