一、添加被管理机器的地址
[devops@iZ8vb41aj4ty78rteodye5Z ansible]# sudo /usr/bin/pwd
/etc/ansible
[devops@iZ8vb41aj4ty78rteodye5Z ansible]# sudo /usr/bin/vim /etc/ansible/hosts
[test_server] #主机组名称
192.168.101.248 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass="123456"
192.168.101.247 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass="123456"
二、编写 playbook 文件
[devops@iZ8vb41aj4ty78rteodye5Z ansible]# sudo /usr/bin/vim create_user.yml
---
- hosts: test_server
gather_facts: false
tasks:
- name: create user test
user: name={{ item.name }} password={{ item.chpass | password_hash('sha512') }} update_password=always
with_items:
- { name: 'test',chpass: 'test123'}
[devops@iZ8vb41aj4ty78rteodye5Z ansible]# ansible-playbook create_user.yml
PLAY [ronglian] ***********************************************************************************************************************************************************
TASK [create user test] **************************************************************************************************************************************************
changed: [192.168.101.248] => (item={u'chpass': u'test123', u'name': u'test'})
changed: [192.168.101.247] => (item={u'chpass': u'test123', u'name': u'test'})
PLAY RECAP ****************************************************************************************************************************************************************
192.168.101.247 : ok=1 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
192.168.101.248 : ok=1 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
三、验证客户端登录
[devops@iZ8vb41aj4ty78rteodye5Z ansible]# ssh test@192.168.101.247
test@192.168.101.247's password:
Last login: Mon Jul 13 14:35:13 2020 from 192.168.2.156
Welcome to Alibaba Cloud Elastic Compute Service !
[tset@paas-cm13-192.168.101.247 iZ8vb0a6mfjnr3vait56rwZ ~]$