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设置