Ansible批量更新远程主机用户密码

Ansible批量更新远程主机用户密码

方法一:  使用Ansible的user模块批量修改远程客户机的用户密码

由于在使用ansible修改用户密码的时候不能使用明文的方式,需要先加密,所以就需要使用一个方法对输入的明文的密码进行加密.
废话不多说了. 下面直接记录下操作方法:
[root@ansible-server ~]# vim /opt/root_passwd.yaml
---
  - hosts: ssh-host
    gather_facts: false
    tasks:
    - name: change user passwd
      user: name={{ item.name }} password={{ item.chpass | password_hash('sha512') }}  update_password=always
      with_items:
           - { name: 'root', chpass: 'kevin@123' }
           - { name: 'app', chpass: 'bjop123' }
注意上面在yaml文件中修改了远程客户机的root用户密码, app用户密码.
如果还想要修改其他用户密码, 则继续按照上面规则添加即可!
执行ansible-play
[root@ansible-server ~]# ansible-playbook /opt/root_passwd.yaml
PLAY [ssh-host] ************************************************************************************************************************
TASK [change user passwd] **************************************************************************************************************
changed: [172.16.60.204] => (item={u'chpass': u'kevin@123', u'name': u'root'})
changed: [172.16.60.205] => (item={u'chpass': u'kevin@123', u'name': u'root'})
changed: [172.16.60.204] => (item={u'chpass': u'bjop123', u'name': u'app'})
changed: [172.16.60.205] => (item={u'chpass': u'bjop123', u'name': u'app'})
changed: [172.16.60.206] => (item={u'chpass': u'kevin@123', u'name': u'root'})
changed: [172.16.60.206] => (item={u'chpass': u'bjop123', u'name': u'app'})
changed: [172.16.60.207] => (item={u'chpass': u'kevin@123', u'name': u'root'})
changed: [172.16.60.207] => (item={u'chpass': u'bjop123', u'name': u'app'})
PLAY RECAP *****************************************************************************************************************************
172.16.60.204              : ok=1    changed=1    unreachable=0    failed=0  
172.16.60.205              : ok=1    changed=1    unreachable=0    failed=0  
172.16.60.206              : ok=1    changed=1    unreachable=0    failed=0  
172.16.60.207              : ok=1    changed=1    unreachable=0    failed=0

方法二:  修改远程主机的单个用户密码使用此方法比较方便

编写playbook文件
[root@ansible-server ~]# vim /opt/root_passwd2.yaml
---
  - hosts: ssh-host
    gather_facts: false
    tasks:
    - name: Change password
      user: name={{ name1 }}  password={{ chpass | password_hash('sha512') }}  update_password=always
执行ansible-playbook,  使用-e参数传递用户名和密码给剧本,其中root为用户名,admin#123就是修改后的root密码
[root@ansible-server ~]# ansible-playbook /opt/root_passwd2.yaml -e "name1=root chpass=admin#123"           
PLAY [ssh-host] ************************************************************************************************************************
TASK [Change password] *****************************************************************************************************************
changed: [172.16.60.204]
changed: [172.16.60.205]
changed: [172.16.60.206]
changed: [172.16.60.207]
PLAY RECAP *****************************************************************************************************************************
172.16.60.204              : ok=1    changed=1    unreachable=0    failed=0  
172.16.60.205              : ok=1    changed=1    unreachable=0    failed=0  
172.16.60.206              : ok=1    changed=1    unreachable=0    failed=0  
172.16.60.207              : ok=1    changed=1    unreachable=0    failed=0

方法三:  使用如下Ansible脚本, 适用于修改清单中部分远程主机的用户密码

编写ansible-playbook脚本 (需要注意下面脚本中"ens192"是客户机ip所在的网卡设备名称, 这个要根据自己实际环境去配置, 比如eth0, eth1等)
[root@ansible-server ~]# 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: 'haha@123' }
     - { ip: "172.16.60.221", password: 'kevin@123' }
     - { ip: "172.16.60.222", password: 'bobo@123' }
执行ansible-playbook:
[root@ansible-server 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'haha@123'})
[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'haha@123'})
skipping: [172.16.60.206] => (item={u'ip': u'172.16.60.205', u'password': u'kevin@123'})
[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'haha@123'})
skipping: [172.16.60.207] => (item={u'ip': u'172.16.60.205', u'password': u'kevin@123'})
skipping: [172.16.60.207] => (item={u'ip': u'172.16.60.206', u'password': u'bobo@123'})
changed: [172.16.60.205] => (item={u'ip': u'172.16.60.205', u'password': u'kevin@123'})
skipping: [172.16.60.205] => (item={u'ip': u'172.16.60.206', u'password': u'bobo@123'})
changed: [172.16.60.204] => (item={u'ip': u'172.16.60.204', u'password': u'haha@123'})
skipping: [172.16.60.204] => (item={u'ip': u'172.16.60.205', u'password': u'kevin@123'})
skipping: [172.16.60.204] => (item={u'ip': u'172.16.60.206', u'password': u'bobo@123'})
changed: [172.16.60.206] => (item={u'ip': u'172.16.60.206', u'password': u'bobo@123'})
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
如果ansible服务端没有和远程主机做ssh信任关系, 则可以在hosts清单配置里直接指明用户名和密码.
如果使用普通用户, 并且允许sudo, 则需要提前在客户机里的/etc/sudoers文件里配置好该普通用户的sudo配置, 即允许该普通用户有sudo权限.
  
[root@ansible-server ~]# vim /etc/ansible/hosts
................
[test-host]
172.16.60.220 ansible_ssh_user=root ansible_ssh_pass=123456 ansible_ssh_port=22
172.16.60.221 ansible_ssh_user=root ansible_ssh_pass=bo@123 ansible_ssh_port=22
172.16.60.222 ansible_ssh_user=app ansible_ssh_pass=bj@123 ansible_ssh_port=22 ansible_sudo_pass=bj@123
  
即172.16.60.222客户机上要提前配置, 允许app用户具有sudo权限.
执行:
[root@ansible-server ~]# ansible test-host -m shell -a "hostname"                     
172.16.60.222 | SUCCESS | rc=0 >>
k8s-node02
172.16.60.220 | SUCCESS | rc=0 >>
k8s-master01
172.16.60.221 | SUCCESS | rc=0 >>
k8s-node01
[root@ansible-server ~]# ansible -i /etc/ansible/hosts test-host -m shell -a "hostname"
172.16.60.222 | SUCCESS | rc=0 >>
k8s-node02
172.16.60.220 | SUCCESS | rc=0 >>
k8s-master01
172.16.60.221 | SUCCESS | rc=0 >>
k8s-node01

 

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值