Ansible批量更新远程主机用户密码 (包括Ansible批量做ssh互信)

1)  在服务端安装ansible


1

[[email protected] ~]# yum install -y ansible

2) 配置ansible到远程主机的ssh无密码信任关系 (authoried_keys 模块)


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

批量实现多台服务器之间ssh无密码登录的相互信任关系, 可以参考之前的文章:  https://www.cnblogs.com/kevingrace/p/9063745.html

这里采用Ansible 实现批量建立互信, 方法如下:

首先要生成ansible服务端本机ssh的key

[[email protected] ~]# ssh-keygen -t rsa          //一路回车

[[email protected] ~]# ls /root/.ssh/

id_rsa  id_rsa.pub

====================================================

需要注意ssh建立互信的命令格式:

# ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected][ip,hostname]

====================================================

在客户机比较多的情况下,使用 ssh-copy-id命令的方法显然是有些费时,使用ansible-playbook 推送 ymal进行批量创建ssh互信关系就显得省事多了,

这里就使用到了ansible的authoried_keys 模块:

首先要配置ansible清单 (远程主机的密码这里为"123456")

[[email protected] ~]# vim /etc/ansible/hosts

................

................

[ssh-host]

172.16.60.204

172.16.60.205

172.16.60.206

172.16.60.207

[ssh-host:vars]

ansible_ssh_pass="123456"

==========================================================

发送公钥到目标机器命令格式如下:

# ansible ssh-host -m copy -a "src=/root/.ssh/id_rsa.pub dest=/root/.ssh/authorized_keys mode=600"

==========================================================

编写playbook文件

[[email protected] ~]# vim /opt/ssh_key.yaml

---

  - hosts: ssh-host

    user: root

    tasks:

     - name: ssh-copy

       authorized_key: user=root key="{{ lookup(‘file‘, ‘/root/.ssh/id_rsa.pub‘) }}"

注意上面yaml脚本中的"ssh-key-host"是在/etc/ansible/hosts清单文件里配置的远程客户机列表

这里做的是基于远程主机root用户的ssh互信

执行批量互信

[[email protected] ~]# ansible-playbook /opt/ssh_key.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 [ssh-copy] ************************************************************************************************************************

changed: [172.16.60.205]

changed: [172.16.60.204]

changed: [172.16.60.206]

changed: [172.16.60.207]

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=2    changed=1    unreachable=0    failed=0

最后验证下ssh互信

[[email protected] ~]# ansible -i /etc/ansible/hosts ssh-host -m shell -a "whoami"

172.16.60.204 | SUCCESS | rc=0 >>

root

172.16.60.205 | SUCCESS | rc=0 >>

root

172.16.60.207 | SUCCESS | rc=0 >>

root

172.16.60.206 | SUCCESS | rc=0 >>

root

至此, ansible批量创建到远程客户机的ssh信任关系已经实现了!

3) Ansible批量更新远程主机用户密码方法

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


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

由于在使用ansible修改用户密码的时候不能使用明文的方式,需要先加密,所以就需要使用一个方法对输入的明文的密码进行加密.

废话不多说了. 下面直接记录下操作方法:

[[email protected] ~]# 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: ‘[email protected]‘ }

           - { name: ‘app‘, chpass: ‘bjop123‘ }

注意上面在yaml文件中修改了远程客户机的root用户密码, app用户密码.

如果还想要修改其他用户密码, 则继续按照上面规则添加即可!

执行ansible-play

[[email protected] ~]# ansible-playbook /opt/root_passwd.yaml

PLAY [ssh-host] ************************************************************************************************************************

TASK [change user passwd] **************************************************************************************************************

changed: [172.16.60.204] => (item={u‘chpass‘: u‘[email protected]‘, u‘name‘: u‘root‘})

changed: [172.16.60.205] => (item={u‘chpass‘: u‘[email protected]‘, 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‘[email protected]‘, 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‘[email protected]‘, 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

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


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

编写playbook文件

[[email protected] ~]# 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密码

[[email protected] ~]# 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脚本, 适用于修改清单中部分远程主机的用户密码


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

编写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


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

如果ansible服务端没有和远程主机做ssh信任关系, 则可以在hosts清单配置里直接指明用户名和密码.

如果使用普通用户, 并且允许sudo, 则需要提前在客户机里的/etc/sudoers文件里配置好该普通用户的sudo配置, 即允许该普通用户有sudo权限.

 

[[email protected] ~]# 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 [email protected] ansible_ssh_port=22

172.16.60.222 ansible_ssh_user=app [email protected] ansible_ssh_port=22 [email protected]

 

即172.16.60.220客户机上要提前配置, 允许app用户具有sudo权限.

执行:

[[email protected] ~]# 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

[[email protected] ~]# 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

原文地址:https://www.cnblogs.com/jians/p/11940660.html

时间: 2024-08-11 03:08:55

Ansible批量更新远程主机用户密码 (包括Ansible批量做ssh互信)的相关文章

使用chpasswd命令批量修改系统用户密码

chpasswd命令工作原理: 从系统的标准输入读入用户的名称和口令,并利用这些信息来更新系统上已存在的用户的口令! 语法: 1:# echo 用户名:密码 | chpasswd 2:# chpasswd < doiido.txt 相关参数: -e :如果使用了-e选项,口令将只能以加密的方式传递 -e, --encrypted  supplied passwords are encrypted 如果未使用-e选项,口令将按明文的形式传递 注意事项 1:用户名必须是系统上已存在的用户 2:普通用

Win2008/2012r2下批量更改域用户密码

使用Windows PowerShell批量更改AD用户的密码,使用以下命令,修改对应字段,执行即可. 1.打开Windows PowerShell加载AD模块: Import-Module ActiveDirectory2.更改所有域用户的密码:Get-ADUser -filter * -SearchBase 'DC=xxx,DC=com' | Set-ADAccountPassword -Reset -NewPassword (ConvertTo-SecureString -AsPlainT

JMeter使用SSH Command实现批量检测linux用户密码是否正常

需求分析1.日常工作中,可能需要对linux服务器的用户密码做校验,验证用户密码是否正常,比如用户.密码错误.或者连接就提示需要修改密码,都算异常:2.这种情况如果只有一两台服务器需要校验,可以手动实现,但是如果50台,100台,还去手工校验,那就是一件很繁琐的事,还可能校验出错:3.本人就介绍下实际工作需要校验50台服务器,如何通过jmeter自动实现校验,简单.高效,手工校验可能需要半天才能实现,开发个jmeter脚本,只需要10分钟搞定,以后有相同工作,只用修改参数文件马上能校验成功:4.

如何在Windows Server 2008R2上面批量添加AD用户及自定义OU批量添加用户

首先这里我们需要找HR要到员工的信息表,越详细越好 注:密码不能太过于简单,一定要符合密码的复杂性的要求,不然会提示报错信息 然后把修改号的表保存到C盘的根目录下面 下面添加这些信息for /f "skip=1 eol=; tokens=1-10 delims=, " %a in (c:\aduserdata.csv) do dsadd user cn=%a,cn=users,dc=abc123,dc=com -display a% -upn %[email protected] -s

自动批量修改linux用户密码

通常会有多台服务器需要同时修改密码,此时可不必一台一台去操作,可以借用expect工具实现批量密码修改工作.涉及到四个文件,ip地址列表文件(iplist.txt),远程密码修改脚本(password.sh),复制时调用密码脚本scp.exp,密码修改主程序(chpasswd.sh),需将四个文件放置在/root目录下,如果放在其它目录,需修改脚本中对应的路径 在执行脚本的机器上安装expect,使用rpm包安装时需要依赖tcl包,也可使用yum安装,使用mkpasswd生成密码,一次生成一次,

PowerShell批量修改AD用户密码属性

需求:非常普通的一个需求,就是给AD用户修改密码,但是问题是量太大了.所以写了个脚本 cls $pass = ConvertTo-SecureString -AsPlainText 12333333344.abc -Force Import-Csv -Path d:\pp.csv | foreach {  Get-ADUser -Identity $_.name|Set-ADAccountPassword -Reset -NewPassword $pass Get-ADUser -Identit

【Oracle批量更新】根据一个大表批量更新另一大表的方法比较

[问题]现在有两个千万级别的结构相同数据不同数据表T_SMS_PHONENO(目的表),T_SMS_PHONENO2(源表),根据源表数据更新目的表的数据. [分析]根据经验,更新方法一般有以下几种: 1.直接update. update T_SMS_PHONENO T Set    T.NAME=(select NAME from T_SMS_PHONENO2 where PHONENO=T.PHONENO) 2.采用分条更新.根据记录ID逐条更新. open cur is select ph

VMWare安装的Ubuntu系统忘记用户密码,可以这么做重置密码

开启虚拟机的时候按住shift 修改之后的截图 显示修改成功直接重启客户机 原文地址:https://www.cnblogs.com/htzsyy/p/11391121.html

Ansible 批量创建用户 密码注意事项

user模块是请求的是useradd, userdel, usermod三个指令,goup模块请求的是groupadd, groupdel, groupmod 三个指令. user模块 home:指定用户的家目录,需要与createhome配合使用 groups:指定用户的属组 uid:指定用的uid password:指定用户的密码 name:指定用户名 createhome:是否创建家目录 yes|no system:是否为系统用户 remove:当state=absent时,remove=