1、查看幫助
[admin@node1 ansible]$ ansible-doc -s user
name:
state:absent(刪除)present(存在)
home:
shell:
system:
uid:
* 創建用戶
錯誤1:
~~~
[admin@node1 ansible]$ ansible webserver -m user -a "name=hacluster state=present uid=1510 shell=/sbin/nologin" -u admin
192.168.20.131 | FAILED! => {
"changed": false,
"cmd": "/sbin/useradd -u 1510 -s /sbin/nologin -m hacluster",
"msg": "[Errno 13] Permission denied",
"rc": 13
}
192.168.20.132 | FAILED! => {
"changed": false,
"cmd": "/sbin/useradd -u 1510 -s /sbin/nologin -m hacluster",
"msg": "[Errno 13] Permission denied",
"rc": 13
}
~~~
第一種解決辦法:
在/etc/ansible/hosts配置文件中寫
~~~
/etc/ansible/hosts
[testserver]
192.168.20.130 ansible_port=1050 ansible_become_user=root ansible_become_pass="Lsf&8816"
* 在執行命令
[admin@node1 ansible]$ ansible webserver -m user -a "name=hacluster uid=1051 shell=/sbin/nologin state=present" --become
192.168.20.132 | SUCCESS => {
"changed": true,
"comment": "",
"createhome": true,
"group": 1051,
"home": "/home/hacluster",
"name": "hacluster",
"shell": "/sbin/nologin",
"state": "present",
"system": false,
"uid": 1051
}
192.168.20.131 | SUCCESS => {
"changed": true,
"comment": "",
"createhome": true,
"group": 1051,
"home": "/home/hacluster",
"name": "hacluster",
"shell": "/sbin/nologin",
"state": "present",
"stderr": "Creating mailbox file: File exists\n",
"stderr_lines": [
"Creating mailbox file: File exists"
],
"system": false,
"uid": 1051
}
~~~
第二種方法:在ad-hoc命令后面加上-b或--become和--ask-sudo-pass
~~~
[admin@node1 ansible]$ ansible webserver -m user -a "name=hacluster01 uid=1052 shell=/sbin/nologin state=present" --ask-sudo-pass -b
[DEPRECATION WARNING]: The sudo command line option has been deprecated in favor of the "become" command line arguments. This feature will be removed in version 2.6. Deprecation warnings can be disabled by
setting deprecation_warnings=False in ansible.cfg.
SUDO password: 輸入admin的密碼
192.168.20.131 | SUCCESS => {
"changed": true,
"comment": "",
"createhome": true,
"group": 1052,
"home": "/home/hacluster01",
"name": "hacluster01",
"shell": "/sbin/nologin",
"state": "present",
"system": false,
"uid": 1052
}
192.168.20.132 | SUCCESS => {
"changed": true,
"comment": "",
"createhome": true,
"group": 1052,
"home": "/home/hacluster01",
"name": "hacluster01",
"shell": "/sbin/nologin",
"state": "present",
"system": false,
"uid": 1052
}
~~~
* 刪除用戶
~~~
[admin@node1 ansible]$ clear
[admin@node1 ansible]$ ansible webserver -m user -a "name=hacluster01 state=absent" --ask-sudo-pass -b
[DEPRECATION WARNING]: The sudo command line option has been deprecated in favor of the "become" command line arguments. This feature will be removed in version 2.6. Deprecation warnings can be disabled by
setting deprecation_warnings=False in ansible.cfg.
SUDO password:
192.168.20.131 | SUCCESS => {
"changed": true,
"force": false,
"name": "hacluster01",
"remove": false,
"state": "absent"
}
192.168.20.132 | SUCCESS => {
"changed": true,
"force": false,
"name": "hacluster01",
"remove": false,
"state": "absent"
}
~~~
- 第一章:Ansible基礎入門
- 第二章:Ansible系列手冊
- 第一節:Ansible系列之主機清單
- 第二節:Ansible系列之變量
- 第三節:Ansible系列之YAML
- 第四節:Ansible系列之條件判斷
- 第五節:Ansible系列之循環
- 第六節: Ansible系列之tags
- 第七節:Ansible系列之Jinja2
- 第三章:Ansible系列之模塊
- 第一節:user模塊
- 第二節:group模塊
- 第三節:cron模塊
- 第四節:copy模塊
- 第五節: file模塊
- 第六節:yum模塊
- 第七節:service模塊
- 第八節:shell模塊
- 第九節:script模塊
- 第十節:setup模塊
- 第十一節:filesystem和mount模塊
- 第十二節:synchronize模塊
- 第十三節: get_url模塊
- 第十四節: package模塊
- 第十五節:stat模塊
- 第十六節:unarchive模塊
- 第十七節: commang模塊
- 第四章:Ansible-playbook介紹
- 第五章:Ansible系統環境
- 第一節:Ansible Role 系統環境之epel設置