编写ansible-playbook脚本 (需要注意下面脚本中 "ens192" 是客户机ip所在的网卡设备名称, 这个要根据自己实际环境去配置, 比如eth0, eth1等)
[[email protected] ~] # cat /opt/root_passwd4.yaml
- hosts: test -host
remote_user: root
tasks:
- name: change password for root
shell: echo ‘{{ item.password }}‘ | passwd --stdin root
when: ansible_ens192.ipv4.address == ‘{{ item.ip }}‘
with_items:
- { ip: "172.16.60.220" , password: ‘[email protected]‘ }
- { ip: "172.16.60.221" , password: ‘[email protected]‘ }
- { ip: "172.16.60.222" , password: ‘[email protected]‘ }
执行ansible-playbook:
[[email protected] ansible] # ansible-playbook /opt/root_passwd3.yaml
PLAY [ ssh -host] ************************************************************************************************************************
TASK [Gathering Facts] *****************************************************************************************************************
ok: [172.16.60.204]
ok: [172.16.60.205]
ok: [172.16.60.206]
ok: [172.16.60.207]
TASK [change password for root] ********************************************************************************************************
[WARNING]: when statements should not include jinja2 templating delimiters such as {{ }} or {% %}. Found: ansible_eth0.ipv4.address
== ‘{{ item.ip }}‘
[WARNING]: when statements should not include jinja2 templating delimiters such as {{ }} or {% %}. Found: ansible_eth0.ipv4.address
== ‘{{ item.ip }}‘
skipping: [172.16.60.205] => (item={u ‘ip‘ : u ‘172.16.60.204‘ , u ‘password‘ : u ‘[email protected]‘ })
[WARNING]: when statements should not include jinja2 templating delimiters such as {{ }} or {% %}. Found: ansible_eth0.ipv4.address
== ‘{{ item.ip }}‘
skipping: [172.16.60.206] => (item={u ‘ip‘ : u ‘172.16.60.204‘ , u ‘password‘ : u ‘[email protected]‘ })
skipping: [172.16.60.206] => (item={u ‘ip‘ : u ‘172.16.60.205‘ , u ‘password‘ : u ‘[email protected]‘ })
[WARNING]: when statements should not include jinja2 templating delimiters such as {{ }} or {% %}. Found: ansible_eth0.ipv4.address
== ‘{{ item.ip }}‘
skipping: [172.16.60.207] => (item={u ‘ip‘ : u ‘172.16.60.204‘ , u ‘password‘ : u ‘[email protected]‘ })
skipping: [172.16.60.207] => (item={u ‘ip‘ : u ‘172.16.60.205‘ , u ‘password‘ : u ‘[email protected]‘ })
skipping: [172.16.60.207] => (item={u ‘ip‘ : u ‘172.16.60.206‘ , u ‘password‘ : u ‘[email protected]‘ })
changed: [172.16.60.205] => (item={u ‘ip‘ : u ‘172.16.60.205‘ , u ‘password‘ : u ‘[email protected]‘ })
skipping: [172.16.60.205] => (item={u ‘ip‘ : u ‘172.16.60.206‘ , u ‘password‘ : u ‘[email protected]‘ })
changed: [172.16.60.204] => (item={u ‘ip‘ : u ‘172.16.60.204‘ , u ‘password‘ : u ‘[email protected]‘ })
skipping: [172.16.60.204] => (item={u ‘ip‘ : u ‘172.16.60.205‘ , u ‘password‘ : u ‘[email protected]‘ })
skipping: [172.16.60.204] => (item={u ‘ip‘ : u ‘172.16.60.206‘ , u ‘password‘ : u ‘[email protected]‘ })
changed: [172.16.60.206] => (item={u ‘ip‘ : u ‘172.16.60.206‘ , u ‘password‘ : u ‘[email protected]‘ })
PLAY RECAP *****************************************************************************************************************************
172.16.60.204 : ok=2 changed=1 unreachable=0 failed=0
172.16.60.205 : ok=2 changed=1 unreachable=0 failed=0
172.16.60.206 : ok=2 changed=1 unreachable=0 failed=0
172.16.60.207 : ok=1 changed=0 unreachable=0 failed=0
|