ansible常用的一些模塊


ansible簡介紹

1.ansible是什么?

      l  ansible是一個基於Python開發的自動化運維工具 !

      l  其功能實現基於SSH遠程連接服務!

      l  ansible可以實現批量系統配置、批量軟件部署、批量文件拷貝、批量運行命令等功能

2.ansible軟件特性

      l  不需要單獨安裝客戶端(no agents),基於系統自帶的sshd服務,sshd就相當於ansible的客戶端。

      l  不需要服務端(no servers)。

      l  需要依靠大量的模塊實現批量管理。? 

      l  配置文件/etc/ansible/ansible.cfg,不用配置

3.ansible軟件執行結果

     l  輸出內容顯示綠色:表示執行成功,當沒有任何改變

     l  輸出內容顯示黃色:表示執行成功,蛋對被管理主機進行了改變

     l  輸出內容顯示紅色:表示執行失敗!!!

4.ansible的官方文檔

http://docs.ansible.com/

ansible的部署:

1.因為ansible基於ssh所以需要密鑰對可參考http://www.cnblogs.com/ExzaiTin/p/7687410.html

2.被管理端需要安裝libselinux-python

yum install libselinux-python -y

3.管理端安裝ansible軟件配置ansible的hosts文件(管理機和被管理機可以基於ssh免密鑰,也可以在ansible的hosts文件中寫入被管理機的密碼)配置格式如下(僅供參考,可以不寫密碼和用戶,[]里表示自己定義的主機組,如果域名可以解析的話ip也可以寫為域名)

4.ansible命令格式

ansible <host-pattern> [-m module_name] [-a args] [options]
<host-pattern> 自己定義的主機組
[-m module_name] 指定模塊
[-a args] 指定要執行的動作

ansible常用模塊:
1.ping-測試模塊
ansible test -m ping
172.16.1.8 | SUCCESS => { "changed": false, "ping": "pong" }
##以上為可以正常鏈接字體會顯示為綠色
172.16.1.41 | UNREACHABLE! => { "changed": false, "msg": "Failed to connect to the host via ssh: ssh: connect to host 172.16.1.41 port 22: Connection timed out\r\n", "unreachable": true }
##以上為無法鏈接字體會顯示為紅色

2.setup-遠程查看主機的配置信息

ansible test -m setup

3.command-遠程執行命令

4.shell-遠程節點執行模塊

[root@m01 ~]# ansible test -m shell -a "date"
172.16.1.222 | SUCCESS | rc=0 >> Sat Oct 21 12:00:38 CST 2017

5.script-在遠程執行本地的腳本

[root@m01 ~]# ansible test -m script -a "/sh/test.sh"
172.16.1.222 | SUCCESS => { "changed": true, "rc": 0, "stderr": "Shared connection to 172.16.1.222 closed.\r\n", "stdout": "hello word\r\n", "stdout_lines": [ "hello word" ] }

 

6.copy-復制模塊

##管理主機數據--遠程主機
[root@m01 ~]# ansible test -m copy -a "src=/sh/test.sh dest=/server/scripts mode=0755"
172.16.1.222 | SUCCESS => { "changed": true, "checksum": "d4dfddcc1b1a2b92599a6b498b1dbc0b3dee9128", "dest": "/server/scripts/test.sh", "gid": 0, "group": "root", "md5sum": "e87e71d1c410c5863aadc5b9a3fe209b", "mode": "0755", "owner": "root", "size": 30, "src": "/root/.ansible/tmp/ansible-tmp-1508560437.19-268270019190063/source", "state": "file", "uid": 0 }
##遠程主機數據進行復制
[root@m01 ~]# ansible test -m copy -a "remote_src=true src=/server/scripts/test.sh dest=/tmp/"
172.16.1.222 | SUCCESS => { "changed": true, "checksum": "d4dfddcc1b1a2b92599a6b498b1dbc0b3dee9128", "dest": "/tmp/test.sh", "gid": 0, "group": "root", "md5sum": "e87e71d1c410c5863aadc5b9a3fe209b", "mode": "0644", "owner": "root", "size": 30, "src": "/server/scripts/test.sh", "state": "file", "uid": 0 }
##復制到目標后修改名稱
[root@m01 ~]# ansible test -m copy -a "src=/etc/hosts dest=/tmp/hello.txt"
172.16.1.222 | SUCCESS => { "changed": true, "checksum": "545f587595b5c60d66243fca48e052ed34eed782", "dest": "/tmp/hello.txt", "gid": 0, "group": "root", "md5sum": "fe08440ffebed54cab7a9b4cb3c3beda", "mode": "0644", "owner": "root", "size": 371, "src": "/root/.ansible/tmp/ansible-tmp-1508560939.57-196389366869591/source", "state": "file", "uid": 0 }
##復制時會創建目錄,包括多級目錄
[root@m01 ~]# ansible test -m copy -a "src=/etc/hosts dest=/tmp/a/b/c/" 
172.16.1.222 | SUCCESS => { "changed": true, "checksum": "545f587595b5c60d66243fca48e052ed34eed782", "dest": "/tmp/a/b/c/hosts", "gid": 0, "group": "root", "md5sum": "fe08440ffebed54cab7a9b4cb3c3beda", "mode": "0644", "owner": "root", "size": 371, "src": "/root/.ansible/tmp/ansible-tmp-1508561086.8-245628104704955/source", "state": "file", "uid": 0 }

7.file-設置文件屬性

創建目錄(支持創建多級目錄)
[root@m01 ~]# ansible test -m file -a "dest=/tmp/dir state=directory"
172.16.1.222 | SUCCESS => { "changed": true, "gid": 0, "group": "root", "mode": "0755", "owner": "root", "path": "/tmp/dir", "size": 4096, "state": "directory", "uid": 0 }
創建文件
[root@m01 ~]# ansible test -m file -a "dest=/tmp/hello state=touch"
172.16.1.222 | SUCCESS => { "changed": true, "dest": "/tmp/hello", "gid": 0, "group": "root", "mode": "0644", "owner": "root", "size": 0, "state": "file", "uid": 0 }
創建軟連接
[root@m01 ~]# ansible test -m file -a "src=/tmp/test.sh dest=/root/test.link state=link "
172.16.1.222 | SUCCESS => { "changed": true, "dest": "/root/test.link", "gid": 0, "group": "root", "mode": "0777", "owner": "root", "size": 12, "src": "/tmp/test.sh", "state": "link", "uid": 0 }
刪除文件目錄
[root@m01 ~]# ansible test -m file -a "dest=/root/test.link state=absent"
172.16.1.222 | SUCCESS => { "changed": true, "path": "/root/test.link", "state": "absent" }

8.fetch-遠程獲取

##從遠程主機拉取文件
[root@m01 ~]# ansible test -m fetch -a "src=/tmp/test.sh dest=/tmp"
172.16.1.222 | SUCCESS => { "changed": true, "checksum": "d4dfddcc1b1a2b92599a6b498b1dbc0b3dee9128", "dest": "/tmp/172.16.1.222/tmp/test.sh", "md5sum": "e87e71d1c410c5863aadc5b9a3fe209b", "remote_checksum": "d4dfddcc1b1a2b92599a6b498b1dbc0b3dee9128", "remote_md5sum": null }
[root@m01 ~]# tree /tmp
/tmp
└── 172.16.1.222
    └── tmp
        └── test.sh

2 directories, 1 file
##拉取時僅拉取目標文件或目錄
[root@m01 tmp]# ansible test -m fetch -a "src=/tmp/test.sh dest=/tmp/ flat=yes"
172.16.1.222 | SUCCESS => { "changed": true, "checksum": "d4dfddcc1b1a2b92599a6b498b1dbc0b3dee9128", "dest": "/tmp/test.sh", "md5sum": "e87e71d1c410c5863aadc5b9a3fe209b", "remote_checksum": "d4dfddcc1b1a2b92599a6b498b1dbc0b3dee9128", "remote_md5sum": null }
[root@m01 tmp]# tree /tmp
/tmp
└── test.sh

0 directories, 1 file

9.mount-掛載相關模塊

 

10.cron-定時任務模塊

11.yum-軟件安裝模塊

12.service-服務啟動或管理模塊

13.filesystem-文件系統模塊

14.user-用戶管理

15.synchronize-rsync相關

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM