ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
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" } ~~~