ansible原理及簡單應用

、基礎介紹


1、簡介

ansible是新出現的自動化運維工具,基於python開發,集合了衆多運維工具(puppet、cfengine、chef、func、fabric)的優點,實現了批量系統配置、批量程序部署、批量運行命令等功能。ansible是基於模塊工作的,本身沒有批量部署的能力。真正具有批量部署的是ansible所運行的模塊,ansible只是提供一種框架。主要包括。

(1)、連接插件connection plugins:負責和被監控端實現通信;

(2)、host inventory:指定操作的主機,是一個配置文件裏面定義監控的主機;

(3)、各種模塊:核心模塊、command模塊、自定義模塊;

(4)、藉助於插件完成記錄日誌郵件等功能;

(5)、playbook:劇本執行多個任務時,非必須可以讓節點一次性運行多個任務。


2、總體架構


wKioL1aWFLODasNTAAIygu9zYRA507.jpg


3、特性

(1)、no agents:不需要在被管控主機上安裝任何客戶端;

(2)、no server:無服務器端,使用時直接運行命令即可;

(3)、modules in any languages:基於模塊工作,可使用任意語言開發模塊;

(4)、yaml,not code:使用yaml語言定製劇本playbook;

(5)、ssh by default:基於SSH工作;

(6)、strong multi-tier solution:可實現多級指揮;


4、優點

(1)、輕量級,無需在客戶端安裝agent,更新時,只需要在操作機上進行一次更新即可;

(2)、批量任務執行可以寫成腳本,而且不用分發到遠程就可以執行;

(3)、使用python編寫,維護更簡單,ruby語法過於複雜;

(4)、支持sudo.


5、任務執行流程


wKiom1aWFsSiFO5UAACUwHjVCMs847.png



說明:

(1)、以上內容大多是基於他人分享的基礎上總結而來,學習借鑑之用;

(2)、本次安裝基於 CentOS 6.7 系統環境。


二、Ansible基礎安裝與配置

1、Ansible基礎安裝(編譯或者rpm)

源碼安裝
1)、python2.7安裝
# wget https://www.python.org/ftp/python/2.7.8/Python-2.7.8.tgz
# tar xvzf Python-2.7.8.tgz
# cd Python-2.7.8
# ./configure --prefix=/usr/local
# make --jobs=`grep processor /proc/cpuinfo | wc -l`
# make install

## 將python頭文件拷貝到標準目錄,以避免編譯ansible時,找不到所需的頭文件
# cd /usr/local/include/python2.7
# cp -a ./* /usr/local/include/

## 備份舊版本的python,並符號鏈接新版本的python
# cd /usr/bin
# mv python python2.6
# ln -s /usr/local/bin/python

## 修改yum腳本,使其指向舊版本的python,已避免其無法運行
# vim /usr/bin/yum
#!/usr/bin/python  -->  #!/usr/bin/python2.6

2)、setuptools模塊安裝
https://pypi.python.org/packages/source/s/setuptools/setuptools-7.0.tar.gz
# tar xvzf setuptools-7.0.tar.gz
# cd setuptools-7.0
# python setup.py install

3)、pycrypto模塊安裝
https://pypi.python.org/packages/source/p/pycrypto/pycrypto-2.6.1.tar.gz
# tar xvzf pycrypto-2.6.1.tar.gz
# cd pycrypto-2.6.1
# python setup.py install

4)、PyYAML模塊安裝
http://pyyaml.org/download/libyaml/yaml-0.1.5.tar.gz
# tar xvzf yaml-0.1.5.tar.gz
# cd yaml-0.1.5
# ./configure --prefix=/usr/local
# make --jobs=`grep processor/proc/cpuinfo | wc -l`
# make install

https://pypi.python.org/packages/source/P/PyYAML/PyYAML-3.11.tar.gz
# tar xvzf PyYAML-3.11.tar.gz
# cd PyYAML-3.11
# python setup.py install

5)、Jinja2模塊安裝
https://pypi.python.org/packages/source/M/MarkupSafe/MarkupSafe-0.9.3.tar.gz
# tar xvzf MarkupSafe-0.9.3.tar.gz
# cd MarkupSafe-0.9.3
# python setup.py install

https://pypi.python.org/packages/source/J/Jinja2/Jinja2-2.7.3.tar.gz
# tar xvzf Jinja2-2.7.3.tar.gz
# cd Jinja2-2.7.3
# python setup.py install

6)、paramiko模塊安裝
https://pypi.python.org/packages/source/e/ecdsa/ecdsa-0.11.tar.gz
# tar xvzf ecdsa-0.11.tar.gz
# cd ecdsa-0.11
# python setup.py install

https://pypi.python.org/packages/source/p/paramiko/paramiko-1.15.1.tar.gz
# tar xvzf paramiko-1.15.1.tar.gz
# cd paramiko-1.15.1
# python setup.py install

7)、simplejson模塊安裝
https://pypi.python.org/packages/source/s/simplejson/simplejson-3.6.5.tar.gz
# tar xvzf simplejson-3.6.5.tar.gz
# cd simplejson-3.6.5
# python setup.py install

8)、ansible安裝
https://github.com/ansible/ansible/archive/v1.7.2.tar.gz
# tar xvzf ansible-1.7.2.tar.gz
# cd ansible-1.7.2
# python setup.py install

yum安裝(依賴epel源)
# wget http://mirrors.aliyun.com/epel/6/x86_64/epel-release-6-8.noarch.rpm
# yum -y install ansible


2、Ansible配置

1)、SSH免密鑰登錄設置

服務器環境:

192.168.9.9:作爲ansible管理節點

192.168.9.11:node1節點

192.168.9.12:node2節點

##修改/etc/hosts文件
# vim /etc/hosts
192.168.9.9     mail.bjwf125.com        mail
192.168.9.11    node1.bjwf125.com       node1
192.168.9.12    node2.bjwf125.com       node2
##生成公鑰私鑰
# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Created directory '/root/.ssh'.        #創建目錄
Enter passphrase (empty for no passphrase):    #輸入密碼
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
00:3a:43:ef:40:da:b1:41:09:56:ea:91:28:6f:31:61 [email protected]
The key's randomart p_w_picpath is:
+--[ RSA 2048]----+
|.+Eo.            |
|o*+B .           |
|++X . .          |
|o..B   .         |
| .o .   S        |
| .               |
|                 |
|                 |
|                 |
+-----------------+
[root@mail ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub node1
The authenticity of host 'node1 (192.168.9.11)' can't be established.
RSA key fingerprint is 76:31:63:16:97:7a:6e:d8:73:5d:a0:fd:b6:c3:34:bd.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'node1,192.168.9.11' (RSA) to the list of known hosts.
root@node1's password: 
Now try logging into the machine, with "ssh 'node1'", and check in:

  .ssh/authorized_keys

to make sure we haven't added extra keys that you weren't expecting.

[root@mail ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub node2
The authenticity of host 'node2 (192.168.9.12)' can't be established.
RSA key fingerprint is 76:31:63:16:97:7a:6e:d8:73:5d:a0:fd:b6:c3:34:bd.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'node2,192.168.9.12' (RSA) to the list of known hosts.
root@node2's password: 
Now try logging into the machine, with "ssh 'node2'", and check in:

  .ssh/authorized_keys

to make sure we haven't added extra keys that you weren't expecting.


2)、ansible配置

[root@mail ~]# cd /etc/ansible
[root@mail ansible]# ls
ansible.cfg  hosts  roles
[root@mail ansible]# vim hosts
[root@mail ansible]# cp hosts{,.bak}
[root@mail ansible]# ls
ansible.cfg  hosts  hosts.bak  roles
[root@mail ansible]# vim hosts
##定義主機組
[webservers]      ##主機組名,可以任意命令
node1             ##管理節點,可以是IP,也可以是主機名,我這定義的是主機名
node2


3)、簡單測試

[root@mail ansible]# ansible webservers -m command -a "date"
node1 | success | rc=0 >>
Thu Jan 14 09:50:38 HKT 2016

node2 | success | rc=0 >>
Thu Jan 14 09:50:38 HKT 2016          ##運行成功

ansible命令應用基礎:
語法:ansible <host-pattern> [-f forks] [-m module_name] [-a args]
             -f forks:fork多少個進程併發處理,默認爲5個;
             -m module_name:要使用的模塊;
             -a args:模塊特有的參數;
             -u:ssh連接的用戶名,默認用root,ansible.cfg中可以配置
             -k:提示輸入ssh登錄密碼。當使用密碼驗證的時候用
             -s:sudo運行
             -U:sudo到那個用戶,默認爲root
             -K:提示輸入sudo密碼,當不是NOPASSWD模式時使用
             -C:只是測試一下會改變什麼內容,不會真正去執行
             -c:連接類型(default=smart)
             -i:指定hosts文件路徑,默認default=/etc/ansible/hosts
             -I 指定pattern,對<host_pattern>已匹配的主機中再過濾一次
             --list-hosts:只打印有哪些主機會執行這個playbook文件,不是實際執行
             -M:要執行的模塊路徑,默認爲/usr/share/ansible
             -o:壓縮輸出,摘要輸出
             --private-key 私鑰路徑
             -T: ssh連接超時時間,默認10秒
             -t:日誌輸出到該目錄,日誌文件名以主機名命名
             -v:verbost


4)、ansible常用的模塊

(1)、command模塊:默認模塊,用於在各被管理節點運行指定的命令;
  相關選項如下:
    creates:一個文件名,當該文件存在,則該命令不執行
    free_form:要執行的linux指令
    chdir:在執行指令之前,先切換到該目錄
    removes:一個文件名,當該文件不存在,則該選項不執行
    executable:切換shell來執行指令,該執行路徑必須是一個絕對路徑
[root@mail ansible]# ansible webservers -m command -a 'ifconfig eth0'
node2 | success | rc=0 >>
eth0      Link encap:Ethernet  HWaddr 00:0C:29:A3:A6:AA  
          inet addr:192.168.9.12  Bcast:192.168.11.255  Mask:255.255.252.0
          inet6 addr: fe80::20c:29ff:fea3:a6aa/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:122795 errors:0 dropped:0 overruns:0 frame:0
          TX packets:280 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:10895221 (10.3 MiB)  TX bytes:27024 (26.3 KiB)

node1 | success | rc=0 >>
eth0      Link encap:Ethernet  HWaddr 00:0C:29:20:16:2E  
          inet addr:192.168.9.11  Bcast:192.168.11.255  Mask:255.255.252.0
          inet6 addr: fe80::20c:29ff:fe20:162e/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:122812 errors:0 dropped:0 overruns:0 frame:0
          TX packets:270 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:10897214 (10.3 MiB)  TX bytes:26930 (26.2 KiB)
          
(2)、user模塊:用戶模塊,用於在各被管理節點管理用戶所使用;
[root@mail ansible]# ansible webservers -m user -a 'name=bjwf125'
node2 | success >> {
    "changed": true, 
    "comment": "", 
    "createhome": true, 
    "group": 500, 
    "home": "/home/bjwf125", 
    "name": "bjwf125", 
    "shell": "/bin/bash", 
    "state": "present", 
    "system": false, 
    "uid": 500
}

node1 | success >> {
    "changed": true, 
    "comment": "", 
    "createhome": true, 
    "group": 500, 
    "home": "/home/bjwf125", 
    "name": "bjwf125", 
    "shell": "/bin/bash", 
    "state": "present", 
    "system": false, 
    "uid": 500
}

[root@node1 ~]# tail -1 /etc/passwd
bjwf125:x:500:500::/home/bjwf125:/bin/bash

(3)、group模塊:用戶組模塊,用於在被各管理節點管理用戶組所使用;
[root@mail ansible]# ansible webservers -m group -a 'name=mysql gid=306'
node2 | success >> {
    "changed": true, 
    "gid": 306, 
    "name": "mysql", 
    "state": "present", 
    "system": false
}

node1 | success >> {
    "changed": true, 
    "gid": 306, 
    "name": "mysql", 
    "state": "present", 
    "system": false
}

[root@node1 ~]# grep mysql /etc/group
mysql:x:306:
[root@node1 ~]# tail -1 /etc/gshadow
mysql:!::
[root@node1 ~]# tail -1 /etc/group      #多種方法查看結果(個人爲了增強記憶)

(4)、cron模塊:計劃任務模塊,用戶在各被管理節點管理計劃任務;
[root@mail ansible]# ansible webservers -m cron -a "name=time minute='*/2' job='/usr/sbin/ntpdate 10.0.1.200'"
node1 | success >> {
    "changed": true, 
    "jobs": [
        "time"
    ]
}

node2 | success >> {
    "changed": true, 
    "jobs": [
        "time"
    ]
}

[root@node1 ~]# crontab -l      #在被管理節點查看cron任務
*/5 * * * * /sbin/hwclock -s
#Ansible: time
*/2 * * * * /usr/sbin/ntpdate 10.0.1.200

移除cron
[root@mail ~]# ansible webservers -m cron -a "name=time minute='*/2' job='/usr/sbin/ntpdate 10.0.1.200' state=absent"
node1 | success >> {
    "changed": false, 
    "jobs": []
}

node2 | success >> {
    "changed": false, 
    "jobs": []
}
[root@node1 ~]# crontab -l      #在被管理節點查看cron任務
*/5 * * * * /sbin/hwclock -s

(5)、copy模塊:複製模塊,複製文件至各管理節點:
###這裏需要注意的事,必須把各節點的selinux給關閉了,不然會失敗的。
  相關選項如下:
    backup:在覆蓋之前,將源文件備份,備份文件包含時間信息。有兩個選項:yes|no
    content:用於替代“src”,可以直接設定指定文件的值
    dest:必選項。要將源文件複製到的遠程主機的絕對路徑,如果源文件是一個目錄,那麼該路徑
    也必須是個目錄
    directory_mode:遞歸設定目錄的權限,默認爲系統默認權限
    force:如果目標主機包含該文件,但內容不同,如果設置爲yes,則強制覆蓋,如果爲no,則只
    有當目標主機的目標位置不存在該文件時,才複製。默認爲yes
    others:所有的file模塊裏的選項都可以在這裏使用
    src:被複制到遠程主機的本地文件,可以是絕對路徑,也可以是相對路徑。如果路徑是一個目錄
    ,它將遞歸複製。在這種情況下,如果路徑使用“/”來結尾,則只複製目錄裏的內容,如果沒有
    使用“/”來結尾,則包含目錄在內的整個內容全部複製,類似於rsync。
[root@mail ~]# ansible all -m copy -a 'src=/etc/fstab dest=/tmp/fstab.ansible mode=654'
node2 | success >> {
    "changed": true, 
    "checksum": "677ecf6a60822701d7d32e8722d22222be85c6ce", 
    "dest": "/tmp/fstab.ansible", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "e784c8bb3dde9df0e8248941def7ac7f", 
    "mode": "0654", 
    "owner": "root", 
    "size": 805, 
    "src": "/root/.ansible/tmp/ansible-tmp-1452739223.25-249168597668683/source", 
    "state": "file", 
    "uid": 0
}

node1 | success >> {
    "changed": true, 
    "checksum": "677ecf6a60822701d7d32e8722d22222be85c6ce", 
    "dest": "/tmp/fstab.ansible", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "e784c8bb3dde9df0e8248941def7ac7f", 
    "mode": "0654", 
    "owner": "root", 
    "size": 805, 
    "src": "/root/.ansible/tmp/ansible-tmp-1452739223.24-124628130207594/source", 
    "state": "file", 
    "uid": 0
}

[root@node1 tmp]# ll
-rw-r-xr-- 1 root root 805 Jan 14 10:40 fstab.ansible

(6)、file模塊:文件模塊,修改各節點指定的文件屬性;
       file模塊的相關選項:
            force:需要在兩種情況下強制創建軟鏈接,一種是源文件不存在,但之後會建立的情況
            下;另一種是目標軟鏈接已存在,需要先取消之前的軟鏈,然後創建新的軟鏈,有兩個
            選項:yes|no
            group:定義文件/目錄的屬組
            mode:定義文件/目錄的權限
            owner:定義文件/目錄的屬主
            path:必選項,定義文件/目錄的路徑
            recurse:遞歸設置文件的屬性,只對目錄有效
            src:被鏈接的源文件路徑,只應用於state=link的情況
            dest:被鏈接到的路徑,只應用於state=link的情況
            state:
               directory:如果目錄不存在,就創建目錄
               file:即使文件不存在,也不會被創建
               link:創建軟鏈接
               hard:創建硬鏈接
               touch:如果文件不存在,則會創建一個新的文件,如果文件或目錄已存在,則更新
               其最後修改時間
               absent:刪除目錄、文件或者取消鏈接文件
[root@mail ~]# ansible all -m file -a 'path=/tmp/fstab.ansible mode=600 owner=bjwf125'
node2 | success >> {
    "changed": true, 
    "gid": 0, 
    "group": "root", 
    "mode": "0600", 
    "owner": "bjwf125", 
    "path": "/tmp/fstab.ansible", 
    "size": 805, 
    "state": "file", 
    "uid": 500
}

node1 | success >> {
    "changed": true, 
    "gid": 0, 
    "group": "root", 
    "mode": "0600", 
    "owner": "bjwf125", 
    "path": "/tmp/fstab.ansible", 
    "size": 805, 
    "state": "file", 
    "uid": 500
}
#注:path:指定節點文件路徑   mode:指定權限  owner:指定屬主

[root@node2 tmp]# ll
-rw------- 1 bjwf125 root 805 Jan 14 10:40 fstab.ansible

(7)、ping模塊:測試模塊,測試各個被管理節點是否在線;
[root@mail ~]# ansible all -m ping
node2 | success >> {
    "changed": false, 
    "ping": "pong"
}

node1 | success >> {
    "changed": false, 
    "ping": "pong"
}

(8)、service模塊:管理各個節點的服務
[root@mail ~]# ansible all -m service -a 'name=ntpd enabled=true'
node1 | success >> {
    "changed": true, 
    "enabled": true, 
    "name": "ntpd"
}

node2 | success >> {
    "changed": true, 
    "enabled": true, 
    "name": "ntpd"
}

(9)、shell模塊:與command模塊功能相同,但比command的模塊功能更加強大
[root@mail ~]# ansible all -m shell -a 'cat /etc/group | grep mysql'
node1 | success | rc=0 >>
mysql:x:306:

node2 | success | rc=0 >>
mysql:x:306:

(10)、script模塊:自動複製腳本至各節點,並運行之
[root@mail ~]# cat ansible.sh
#!/bin/bash
#
echo "welcome to mylinux." > /tmp/mylinux
[root@mail ~]# ansible all -m script -a '/root/ansible.sh'
node2 | success >> {
    "changed": true, 
    "rc": 0, 
    "stderr": "", 
    "stdout": ""
}

node1 | success >> {
    "changed": true, 
    "rc": 0, 
    "stderr": "", 
    "stdout": ""
}

[root@node1 tmp]# cat mylinux 
welcome to mylinux.

(11)、setup模塊:收集ansible的facters
[root@mail ~]# ansible all -m setup
node1 | success >> {       #用來查看遠程主機的一些基本信息(很強大,感覺超出“基本”了)
    "ansible_facts": {
        "ansible_all_ipv4_addresses": [
            "192.168.9.11"
        ], 
        "ansible_all_ipv6_addresses": [
            "fe80::20c:29ff:fe20:162e"
        ], 
        "ansible_architecture": "x86_64", 
        "ansible_bios_date": "07/02/2015", 
        "ansible_bios_version": "6.00", 
        "ansible_cmdline": {
            "KEYBOARDTYPE": "pc", 
            "KEYTABLE": "us", 
            "LANG": "en_US.UTF-8", 
            "SYSFONT": "latarcyrheb-sun16", 
            "quiet": true, 
            "rd_NO_DM": true, 
            "rd_NO_LUKS": true, 
            "rd_NO_LVM": true, 
            "rd_NO_MD": true, 
            "rhgb": true, 
            "ro": true, 
            "root": "UUID=a4588fdb-b266-4f99-b5d5-013149014d55"
        }, 
        "ansible_date_time": {
            "date": "2016-01-14", 
            "day": "14", 
            "epoch": "1452741270", 
            "hour": "11", 
            "iso8601": "2016-01-14T03:14:30Z", 
            "iso8601_micro": "2016-01-14T03:14:30.591778Z", 
            "minute": "14", 
            "month": "01", 
            "second": "30", 
            "time": "11:14:30", 
            "tz": "HKT", 
            "tz_offset": "+0800", 
            "weekday": "Thursday", 
            "year": "2016"
        },
        
(12)、yum模塊:用於在各個管理節點安裝軟件所使用
[root@mail ~]# ansible all -m yum -a 'name=httpd state=present'
node1 | success >> {
    "changed": false, 
    "msg": "", 
    "rc": 0, 
    "results": [
        "httpd-2.2.15-47.el6.centos.1.x86_64 providing httpd is already installed"
    ]
}

node2 | success >> {
    "changed": false, 
    "msg": "", 
    "rc": 0, 
    "results": [
        "httpd-2.2.15-47.el6.centos.1.x86_64 providing httpd is already installed"
    ]
}

[root@node1 tmp]# rpm -q httpd
httpd-2.2.15-47.el6.centos.1.x86_64

(13)、更多模塊
其他常用模塊,比如synchronize、acl就不再一一列舉了,可以結合自身所需要的系統環境來進行測試。

##更多模塊可以參考:
[root@mail ~]# ansible-doc -l
Homepage: http://www.greenwoodsoftware.com/less
a10_server                    Manage A10 Networks AX/SoftAX/Thunder/vThunder devices           
a10_service_group             Manage A10 Networks AX/SoftAX/Thunder/vThunder devices           
a10_virtual_server            Manage A10 Networks AX/SoftAX/Thunder/vThunder devices           
acl                           Sets and retrieves file ACL information.                         
add_host                      add a host (and alternatively a group) to the ansible-playbook in...
airbrake_deployment           Notify airbrake about app deployments                            
alternatives                  Manages alternative programs for common commands                 
apache2_module                enables/disables a module of the Apache2 webserver               
apt                           Manages apt-packages                                             
apt_key                       Add or remove an apt key                                         
apt_repository                Add and remove APT repositories                                  
apt_rpm                       apt_rpm package manager                                          
assemble                      Assembles a configuration file from fragments                    
assert                        Fail with custom message                                         
at                            Schedule the execution of a command or script file via the at com...
authorized_key                Adds or removes an SSH authorized key                            
azure                         create or terminate a virtual machine in azure                   
bigip_facts                   Collect facts from F5 BIG-IP devices                             
bigip_monitor_http            Manages F5 BIG-IP LTM http monitors                              
bigip_monitor_tcp             Manages F5 BIG-IP LTM tcp monitors                               
bigip_node                    Manages F5 BIG-IP LTM nodes                                      
bigip_pool                    Manages F5 BIG-IP LTM pools                                      
bigip_pool_member             Manages F5 BIG-IP LTM pool members                               
bigpanda                      Notify BigPanda about deployments  

##或者一些網址: http://docs.ansible.com/modules_by_category.html

  

三、一些概念補充


playbook的組成:playbook是由一個或多個“play”組成的列表,可以讓它們聯同起來按事先編排的機制執行;所謂task無非是調用ansible的一個module,而在模塊參數中可以使用變量;模塊執行是冪等的,這意味着多次執行是安全的,因爲其結果均一致;


執行模型task list中的各任務按次序逐個在hosts中指定的所有主機上執行,即在所有主機上完成第一個任務後再開始第二個。在順序運行某playbook時,如果中途發生錯誤,所有已執行任務都將回滾,因此,在修改playbook後重新執行一次即可;


task組成:每個task都應該有其name,用於playbook的執行結果輸出,建議其內容儘可能清晰地描述任務執行步驟。如果未提供name,則action的結果將用於輸出;


notify指定handler的執行機制:“notify”這個action可用於在每個play的最後被觸發,在notify中列出的操作稱爲handler,僅在所有的變化發生完成後一次性地執行指定操作。


四、ansible使用幫助

ansible-doc -l              ##列出ansible的所有模塊
ansible-doc -s module name  ##查看模塊的屬性信息 
[root@mail ~]# ansible-doc -s yum    ##查看yum模塊的屬性信息
less 436
Copyright (C) 1984-2009 Mark Nudelman

less comes with NO WARRANTY, to the extent permitted by law.
For information about the terms of redistribution,
see the file named README in the less distribution.
Homepage: http://www.greenwoodsoftware.com/less
- name: M a n a g e s   p a c k a g e s   w i t h   t h e   I ( y u m )   p a c k a g e   m a n a g e r
  action: yum
      conf_file              # The remote yum configuration file to use for the transaction.
      disable_gpg_check      # Whether to disable the GPG checking of signatures of packages being installed. Has an effect only if state is `present' or `latest'.
      disablerepo            # `Repoid' of repositories to disable for the install/update operation. These repos will not persist beyond the transaction. When specifying mu
      enablerepo             # `Repoid' of repositories to enable for the install/update operation. These repos will not persist beyond the transaction. When specifying mul
      list                   # Various (non-idempotent) commands for usage with `/usr/bin/ansible' and `not' playbooks. See examples.
      name=                  # Package name, or package specifier with version, like `name-1.0'. When using state=latest, this can be '*' which means run: yum -y update. Yo
      state                  # Whether to install (`present', `latest'), or remove (`absent') a package.
      update_cache           # Force updating the cache. Has an effect only if state is `present' or `latest'.


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章