3.2、Ansible單命令測試


0.Ansible的group支持all、通配符(*)、IP地址

1.查看Ansible的版本

$  ansbile  --version 

[root@test ~]# ansible --version

ansible 1.7.2

[root@test ~]#

2.消除首次ssh登錄時要求輸入yes確認

在所有機器上修改/etc/ssh/ssh_config文件中設置StrictHostKeyChecking no即可(默認為 ask )

[root@master ~]# grep "StrictHostKeyChecking" /etc/ssh/ssh_config

StrictHostKeyChecking no

3.拷貝文件

ansible <groupname> -m copy -a "src=/etc/ssh/ssh_config dest=/etc/ssh/ssh_config owner=root group=root mode=644 force=yes"

示例:

[root@test ~]# ansible all -m copy -a "src=/etc/ssh/ssh_config dest=/etc/ssh/ssh_config owner=root group=root mode=644 force=yes"

192.168.91.135 | success >> {

    "changed": true,

    "dest": "/etc/ssh/ssh_config",

    "gid": 0,

    "group": "root",

    "md5sum": "d27d7cc9767512b64c84d06544e01546",

    "mode": "0644",

    "owner": "root",

    "size": 2072,

    "src": "/root/.ansible/tmp/ansible-tmp-1476168613.68-180284084445387/source",

    "state": "file",

    "uid": 0

}

[root@test ~]#

4.查看所有機器的磁盤情況

ansible <groupname> -m shell -a "df -h" -k

5.測試機器的連通性

ansible <groupname> -m ping

6.Ansible所有的模塊

      http://docs.ansible.com/ansible/list_of_all_modules.html

7.將本機上的配置文件組裝發送到遠程主機

ansible <groupname> -m assemble -a "src=/root/configure dest=/root/test/a.conf remote_src=False

8.將本機上的配置文件組裝發送到遠程主機,帶分隔符

ansible <groupname> -m assemble -a "src=/root/configure dest=/root/test/a.conf remote_src=False delimiter='####'"

9.設定權限進行拷貝

ansible <groupname> -m copy -a "src=/root/configure/a.conf dest=/root/test owner=root group=root mode=0777"

10.拷貝的時候備份

ansible <groupname> -m copy -a "src=/root/kel/1 dest=/tmp/kel owner=root group=root backup=yes"

11.拷貝文件之后進行驗證

ansible <groupname> -m copy -a "src=/etc/sudoers dest=/tmp/2 validate='visudo -cf %s'"

12. fetch一個文件進行保存

ansible <groupname> -m fetch -a "src=/root/123 dest=/root"

13.指定路徑目錄進行保存

ansible <groupname> -m fetch -a "src=/root/Ssh.py dest=/root/kel/ flat=yes"

14. 設置文件屬性

ansible <groupname> -m file -a "path=/root/123 owner=kel group=kel mode=0644"

15.創建目錄

ansible <groupname> -m file -a "path=/tmp/kel state=directory mode=0755"

16.修改權限

ansible <groupname> -m file -a "path=/tmp/kel mode=0444"

17.創建軟連接

ansible <groupname> -m file -a "src=/tmp/1 dest=/tmp/2 owner=kel state=link"

18.添加其中的節的值

ansible <groupname> -m ini_file -a "dest=/tmp/kel section=kel option=kel value=kel mode=0600 backup=yes"

19.基礎模塊使用

19.1 並行和Shell命令

設置ssh-agent記住認證

   $ ssh-agent bash

   $ ssh-add ~/.ssh/id_rsa

19.2  10秒內重啟

    $ ansible <groupname> -a "/sbin/reboot" -f 10

19.3 在默認情況下,ansible使用的是當前用戶,當你需要使用其他用戶的時候,可以使用選項-u username

$ ansible <groupname> -a "/usr/bin/foo" -u username

19.4  要使用sudo的時候

    $ ansible <groupname> -a "/usr/bin/foo" -u username --sudo[--ask-sudo-pass]

 --ask-sudo-pass(-K)此選項是用來詢問sudo的密碼,如果設置了,如果未設置,那么無需使用

19.5  也可以在sudo到別的用戶來進行執行

    $ ansible <groupname> -a "/usr/bin/foo" -u username -U otheruser[--ask-sudo-pass]

            參數-f 10表示並發進行,也就是10個進程同時運行,在使用的時候,默認的情況下為5,選擇合適的數據,從而使得系統能夠進行處理

 參數-m表示選擇的模塊,在默認情況下,command是默認的模塊

            Command不適用於有shell變量的情況,也不適用於有管道符的情況,如果要使用此種情況,那么可以使用shell模塊

19.6  使用Shell模塊

    $ ansible <groupname> -m shell -a 'echo $TERM'

20.  文件傳輸

20.1  傳輸文件到很多主機

$ ansible <groupname> -m copy -a "src=/etc/hosts dest=/tmp/hosts"

20.2  修改用戶和用戶組權限

$ ansible <groupname> -m file -a "dest=/srv/foo/a.txt mode=600"

$ ansible <groupname> -m file -a "dest=/srv/foo/b.txt mode=600 owner=mdehaan group=mdehaan"

20.3   創建目錄

 $ ansible <groupname> -m file -a "dest=/path/to/c mode=755 owner=mdehaan group=mdehaan state=directory"

21. 管理包

21.1   確定一個包已經安裝,但是不更新

$ ansible <groupname> -m yum -a "name=acme state=present"

21.2   確定一個包安裝的是指定版本

$ ansible <groupname> -m yum -a "name=acme-1.5 state=present"

21.3  確定一個包是最新包

$ ansible <groupname> -m yum -a "name=acme state=latest"

21.4   確定一個包未安裝

$ ansible <groupname> -m yum -a "name=acme state=absent" 

22. 用戶和用戶組

22.1   創建用戶和管理已經存在的用戶和組

$ ansible <groupname> -m user -a "name=foo password=<crypted password here>"

$ ansible <groupname> -m user -a "name=foo state=absent"

23. 服務管理

23.1 確定一個服務正在運行

 $ ansible <groupname> -m service -a "name=httpd state=started"

23.2  重啟一個服務

$ ansible <groupname> -m service -a "name=httpd state=restarted"

23.3   確定一個服務是停止的

$ ansible <groupname> -m service -a "name=httpd state=stopped"

24.限制后台運行時間

24.1   后台運行總是耗費比較長的時間,從而其狀態在隨后總是能夠查看的,如果踢掉主機,又不想輪訓

$ ansible <groupname> -B 3600 -P 0 -a "/usr/bin/long_running_operation --do-stuff"

24.2   如果要檢查服務的狀態,可以使用模塊async_status,傳遞job id

$ ansible <groupname> -m async_status -a "jid=488359678239.2844"

24.3   輪訓是內建的

 $ ansible <groupname> -B 1800 -P 60 -a "/usr/bin/long_running_operation --do-stuff"

參數-B表示運行最多30分鍾,30*60,-P 60 輪訓其狀態每60S,

當時間運行在-B參數后的時間之后,此服務會被停止運行。

可以使用參數—forksvalue,來確保服務盡快運行

25.收集信息

$ ansible <groupname> -m setup

來自 <http://www.cnblogs.com/LuisYang/p/5961001.html>

 


免責聲明!

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



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