Ansible基础 - 05其他常用模块
Ansible基础 - 05其他常用模块
一、ping模块
[root@cl-server ~]# ansible cl -m ping
二、user/group模块
### 添加用户,指定用户UID
[root@cl-server ~]# ansible cl -m user -a 'name=test uid=1000'
cl-node01 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"comment": "",
"create_home": true,
"group": 1000,
"home": "/home/test",
"name": "test",
"shell": "/bin/bash",
"state": "present",
"system": false,
"uid": 1000
}
### 添加用户,不指定则按最大UID递增
[root@cl-server ~]# ansible cl -m user -a 'name=testuser'
cl-node01 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"comment": "",
"create_home": true,
"group": 1001,
"home": "/home/testuser",
"name": "testuser",
"shell": "/bin/bash",
"state": "present",
"system": false,
"uid": 1001
}
添加用户,不创建家目录,指定已存在的目录作为家目录
ansible cl -m user -a 'name=testuser02 createhome=false home=/tmp/'
删除用户,但不删除已创建的用户家目录
[root@cl-server ~]# ansible cl -m user -a 'name=testuser state=absent'
cl-node01 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"force": false,
"name": "testuser",
"remove": false,
"state": "absent"
}
添加用户,使用已创建的组: ansible cl -m user -a 'name=testuser05 group=testgroup'
添加群组: ansible cl -m group -a 'name=testgroup gid=9999'
删除群组: 当有用户使用时,不能删除: ansible cl -m group -a 'name=testgroup state=absent'
三、cron模块
创建定时任务: ansible cl -m cron -a 'name="echo_message_to_tt" minute=*/1 job="echo 123 >> /tmp/tt.txt"'
[root@cl-node01 tmp]# crontab -l
#Ansible: echo_message_to_tt
*/1 * * * * echo 123 >> /tmp/tt.txt
ansible cl -m cron -a ' name="echo_message_to_tt" minute=*/1 job="echo 123 >> /tmp/tt03.txt" user=testuser03'
删除定时任务:[root@cl-server ~]# ansible cl -m cron -a ' name="echo_message_to_tt03" state=absent'
创建两个不指定name 的定时任务: 默认名称为None
ansible cl -m cron -a 'minute=*/16 job="echo 123 >> /tmp/tt016.txt"'
ansible cl -m cron -a 'minute=*/16 job="echo 123 >> /tmp/tt016.txt"'
删除名称为 None 的定时任务, 全部删除: ansible cl -m cron -a 'name="None" state=absent'
删除任务不成功: ansible cl -m cron -a ' minute=*/18 job="echo 123 >> /tmp/tt018.txt" state=absent'
根据name会将所有匹配的任务都删除: ansible cl -m cron -a 'name="None" minute=*/18 job="echo 123 >> /tmp/tt018.txt" state=absent'
当添加的定时任务name 存在时,会更新已有的定时任务:ansible cl -m cron -a 'name="echo_message_to_tt18" minute=*/28 job="echo 123 >> /tmp/tt028.txt"'
通过模拟手动添加定时任务,Ansible也可以管理。
四、yum 模块
安装软件包: ansible cl -m yum -a 'name=telnet state=present'
卸载软件包: ansible cl -m yum -a 'name=telnet state=absent'
五、Service模块
"msg": "value of state must be one of: reloaded, restarted, started, stopped,
开启服务,并设置开机自启动: ansible cl -m service -a 'name=supervisord state=started enabled=true'
停止服务: ansible cl -m service -a 'name=supervisord state=stopped'
六、setup模块
查看主机的所有ansible 环境变量: ansible cl-node01 -m setup
查看主机的包含mem的ansible 环境变量: ansible cl -m setup -a 'filter="*mem*"'
将输出的内容同时保存到文件中,以主机为文件夹: ansible cl -m setup -a 'filter="*mem*"' -tree /tmp/filter-mem