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 ofstatemust 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