ansible提供了衆多子產品,我們可以在ansible主機上運作ansible-doc -l指令檢視ansible所有支援的子產品。通過ansible-doc -s MODULE_NAME 指令可以檢視指定子產品的所有參數
檢視所有子產品
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<code>root@host1:</code><code>/etc/ansible/roles/tomcat8_install/tasks</code><code># ansible-doc -l</code>
<code>less</code> <code>436</code>
<code>Copyright (C) 1984-2009 Mark Nudelman</code>
<code>less</code> <code>comes with NO WARRANTY, to the extent permitted by law.</code>
<code>For information about the terms of redistribution,</code>
<code>see the </code><code>file</code> <code>named README </code><code>in</code> <code>the </code><code>less</code> <code>distribution.</code>
<code>Homepage: http:</code><code>//www</code><code>.greenwoodsoftware.com</code><code>/less</code>
<code>a10_server Manage A10 Networks AX</code><code>/SoftAX/Thunder/vThunder</code> <code>devices </code>
<code>a10_service_group Manage A10 Networks devices' service </code><code>groups</code>
<code>a10_virtual_server Manage A10 Networks devices' virtual servers </code>
<code>acl Sets and retrieves </code><code>file</code> <code>ACL information. </code>
<code>add_host add a host (and alternatively a group) to the ansible-playbook </code><code>in</code><code>-memory inventory </code>
<code>airbrake_deployment Notify airbrake about app deployments </code>
<code>alternatives Manages alternative programs </code><code>for</code> <code>common commands </code>
<code>apache2_module enables</code><code>/disables</code> <code>a module of the Apache2 webserver </code>
<code>apk Manages apk packages </code>
<code>apt Manages apt-packages </code>
<code>apt_key Add or remove an apt key </code>
<code>apt_repository Add and remove APT repositories </code>
<code>apt_rpm apt_rpm package manager </code>
<code>assemble Assembles a configuration </code><code>file</code> <code>from fragments </code>
<code>assert Fail with custom message </code>
<code>at Schedule the execution of a </code><code>command</code> <code>or script </code><code>file</code> <code>via the at </code><code>command</code><code>. </code>
<code>authorized_key Adds or removes an SSH authorized key </code>
<code>azure create or terminate a virtual machine </code><code>in</code> <code>azure </code>
<code>bigip_facts Collect facts from F5 BIG-IP devices </code>
<code>bigip_gtm_wide_ip Manages F5 BIG-IP GTM wide ip </code>
<code>bigip_monitor_http Manages F5 BIG-IP LTM</code>
檢視子產品參數:
29
30
31
32
33
34
<code>ansible-doc -s MODULE_NAME</code>
<code>例如:</code>
<code>[root@localhost ansible]</code><code># ansible-doc -s file</code>
<code>- name: Sets attributes of files</code>
<code> </code><code>action: </code><code>file</code>
<code> </code><code>follow </code><code># This flag indicates that filesystem links, if they exist, should be followed.</code>
<code> </code><code>force </code><code># force the creation of the symlinks in two cases: the source file does not exist (but will appear later);</code>
<code> </code><code>the destination exists and is a </code><code>file</code> <code>(so, we need to unlink the </code><code>"path"</code> <code>file</code>
<code> </code><code>and create </code><code>symlink</code> <code>to the </code><code>"src"</code> <code>file</code> <code>in</code> <code>place of it).</code>
<code> </code><code>group </code><code># name of the group that should own the file/directory, as would be fed to `chown'</code>
<code> </code><code>mode </code><code># mode the file or directory should be. For those used to `/usr/bin/chmod' remember that modes are actually</code>
<code> </code><code>octal numbers (like 0644). Leaving off the leading zero will likely have</code>
<code> </code><code>unexpected results. As of version 1.8, the mode may be specified as a</code>
<code> </code><code>symbolic mode (</code><code>for</code> <code>example, `u+rwx</code><code>' or `u=rw,g=r,o=r'</code><code>).</code>
<code> </code><code>owner </code><code># name of the user that should own the file/directory, as would be fed to `chown'</code>
<code> </code><code>path= </code><code># path to the file being managed. Aliases: `dest', `name'</code>
<code> </code><code>recurse </code><code># recursively set the specified file attributes (applies only to state=directory)</code>
<code> </code><code>selevel </code><code># level part of the SELinux file context. This is the MLS/MCS attribute, sometimes known as the `range'.</code>
<code> </code><code>`_default</code><code>' feature works as for `seuser'</code><code>.</code>
<code> </code><code>serole </code><code># role part of SELinux file context, `_default' feature works as for `seuser'.</code>
<code> </code><code>setype </code><code># type part of SELinux file context, `_default' feature works as for `seuser'.</code>
<code> </code><code>seuser </code><code># user part of SELinux file context. Will default to system policy, if applicable. If set to `_default', it</code>
<code> </code><code>will use the `user' portion of the policy </code><code>if</code> <code>available</code>
<code> </code><code>src </code><code># path of the file to link to (applies only to `state=link'). Will accept absolute, relative and nonexisting</code>
<code> </code><code>paths. Relative paths are not expanded.</code>
<code> </code><code>state </code><code># If `directory', all immediate subdirectories will be created if they do not exist, since 1.7 they will be</code>
<code> </code><code>created with the supplied permissions. If `</code><code>file</code><code>', the </code><code>file</code> <code>will NOT be</code>
<code> </code><code>created </code><code>if</code> <code>it does not exist, see the [copy] or [template] module </code><code>if</code> <code>you</code>
<code> </code><code>want that behavior. If `link', the symbolic link will be created or</code>
<code> </code><code>changed. Use `hard</code><code>' for hardlinks. If `absent'</code><code>, directories will be</code>
<code> </code><code>recursively deleted, and files or symlinks will be unlinked. If `</code><code>touch</code><code>' (new</code>
<code> </code><code>in</code> <code>1.4), an empty </code><code>file</code> <code>will be created </code><code>if</code> <code>the `path' does not exist, </code><code>while</code>
<code> </code><code>an existing </code><code>file</code> <code>or directory will receive updated </code><code>file</code> <code>access and</code>
<code> </code><code>modification </code><code>times</code> <code>(similar to the way `</code><code>touch</code><code>` works from the </code><code>command</code> <code>line)</code>
子產品介紹:
copy子產品:http://docs.ansible.com/ansible/copy_module.html
功能:複制檔案到遠端主機的指定路徑下:
參數:
src:本地檔案的路徑,如果源是一個目錄,會将目錄中所有的檔案都copy過去
dest:遠端主機的絕對路徑
owner:檔案屬主
group:檔案屬組
mode:檔案權限
指令示範:
<code>ansible all -m copy -a </code><code>'src=/etc/fstab dest=/tmp/fstab owner=root mode=0644'</code>
file子產品:http://docs.ansible.com/ansible/file_module.html
功能:設定檔案屬性、建立符号連結、建立目錄等
path:指明檔案路徑,可以使用name或dest來代替
建立檔案的符号連結:
src:指明源檔案
dest:指明符号連結檔案路徑
<code>ansible pms -m </code><code>file</code> <code>-a </code><code>'src=/tmp/fstab dest=/srv/fstab state=link'</code>
<code>[root@localhost srv]</code><code># ll</code>
<code>lrwxrwxrwx 1 root root 10 Jul 6 14:08 fstab -> </code><code>/tmp/fstab</code>
ping子產品:http://docs.ansible.com/ansible/ping_module.html
功能:測試被管理主機的連通性
<code>[root@localhost ansible]</code><code># ansible all -m ping</code>
<code>172.16.206.134 | SUCCESS => {</code>
<code> </code><code>"changed"</code><code>: </code><code>false</code><code>, </code>
<code> </code><code>"ping"</code><code>: </code><code>"pong"</code>
<code>}</code>
<code>10.10.10.202 | SUCCESS => {</code>
command子產品:http://docs.ansible.com/ansible/command_module.html
功能:在遠端主機上執行指令
<code>[root@localhost ansible]</code><code># ansible all -m command -a 'hostname' </code>
<code>172.16.206.134 | SUCCESS | rc=0 >></code>
<code>localhost.localdomain</code>
<code>10.10.10.202 | SUCCESS | rc=0 >></code>
注意:command子產品不支援管道符,這也是command子產品和shell子產品的差別。
例如:
user子產品:http://docs.ansible.com/ansible/user_module.html
功能:在遠端主機上建立或者删除使用者
name:賬戶名
state:
present:建立
absent:删除
group:指定使用者的基本組
uid:指定uid
system:建立系統使用者 值為yes 或者no
<code>[root@localhost ansible]</code><code># ansible pms -m user -a 'name=test state=present uid=306 group=root system=yes' </code>
<code> </code><code>"changed"</code><code>: </code><code>true</code><code>, </code>
<code> </code><code>"comment"</code><code>: </code><code>""</code><code>, </code>
<code> </code><code>"createhome"</code><code>: </code><code>true</code><code>, </code>
<code> </code><code>"group"</code><code>: 0, </code>
<code> </code><code>"home"</code><code>: </code><code>"/home/test"</code><code>, </code>
<code> </code><code>"name"</code><code>: </code><code>"test"</code><code>, </code>
<code> </code><code>"shell"</code><code>: </code><code>"/bin/bash"</code><code>, </code>
<code> </code><code>"state"</code><code>: </code><code>"present"</code><code>, </code>
<code> </code><code>"system"</code><code>: </code><code>true</code><code>, </code>
<code> </code><code>"uid"</code><code>: 306</code>
<code>[root@localhost srv]</code><code># id test</code>
<code>uid=306(</code><code>test</code><code>) gid=0(root) </code><code>groups</code><code>=0(root)</code>
service子產品:http://docs.ansible.com/ansible/service_module.html
功能:管理遠端主機上的服務狀态
enabled=:是否開機自動啟動,取值為yes或者no。enabled=yes,表示服務開啟啟動
name=:服務名
state=: 服務狀态
started:啟動
restarted:重新開機
stopped:停止
reloaded:重載
<code>[root@localhost ansible]</code><code># ansible pms -m service -a 'name=zabbix-agent state=started enabled=yes'</code>
<code> </code><code>"enabled"</code><code>: </code><code>true</code><code>, </code>
<code> </code><code>"name"</code><code>: </code><code>"zabbix-agent"</code><code>, </code>
<code> </code><code>"state"</code><code>: </code><code>"started"</code>
script子產品:http://docs.ansible.com/ansible/script_module.html
功能:在遠端主機執行主要端的shell/python腳本
<code>root@host1:</code><code>/tmp</code><code># ansible pms -m script -a '/tmp/echo.sh'</code>
<code> </code><code>"rc"</code><code>: 0, </code>
<code> </code><code>"stderr"</code><code>: </code><code>""</code><code>, </code>
<code> </code><code>"stdout"</code><code>: </code><code>""</code><code>, </code>
<code> </code><code>"stdout_lines"</code><code>: []</code>
shell子產品支援管道符,這是它與commands子產品的最大差別
<code>ansible pms -m shell -a </code><code>'ps -ef | grep tomcat'</code>
<code>root 18569 1 0 13:09 pts</code><code>/0</code> <code>00:00:36 </code><code>/usr/java/jdk1</code><code>.8.0_66</code><code>/bin/java</code> <code>-Djava.util.logging.config.</code><code>file</code><code>=</code><code>/tmp/catalina_base/conf/logging</code><code>.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -server -XX:PermSize=256M -XX:MaxPermSize=1024m -Xms512M -Xmx1024M -XX:MaxNewSize=256m -Djava.endorsed.</code><code>dirs</code><code>=</code><code>/usr/local/apache-tomcat-8</code><code>.0.29</code><code>/endorsed</code> <code>-classpath </code><code>/usr/local/apache-tomcat-8</code><code>.0.29</code><code>/bin/bootstrap</code><code>.jar:</code><code>/usr/local/apache-tomcat-8</code><code>.0.29</code><code>/bin/tomcat-juli</code><code>.jar -Dcatalina.base=</code><code>/tmp/catalina_base</code> <code>-Dcatalina.home=</code><code>/usr/local/apache-tomcat-8</code><code>.0.29 -Djava.io.tmpdir=</code><code>/tmp/catalina_base/temp</code> <code>org.apache.catalina.startup.Bootstrap start</code>
<code>root 19314 19313 0 17:24 pts</code><code>/1</code> <code>00:00:00 </code><code>/bin/sh</code> <code>-c </code><code>ps</code> <code>-ef | </code><code>grep</code> <code>tomcat</code>
<code>root 19316 19314 0 17:24 pts</code><code>/1</code> <code>00:00:00 </code><code>grep</code> <code>tomcat</code>
yum子產品:http://docs.ansible.com/ansible/yum_module.html
功能:在遠端主機上安裝軟體包
name=: 包名,如果從遠端伺服器本地安裝某個包,則可以寫該包在遠端主機上絕對的路徑,如name=/srv/jdk/jdk-8u66-linux-x64.rpm
state=:狀态,值為present,absent,lastest
present、lasted安裝
absent:解除安裝
lastest:安裝最新版的包,相當于更新軟體包
removed:删除軟體包
installed:安裝軟體包
<code>[root@localhost tmp]</code><code># ansible pms -m yum -a 'name=/srv/jdk/jdk-8u66-linux-x64.rpm state=present' </code>
<code> </code><code>"msg"</code><code>: </code><code>""</code><code>, </code>
<code> </code><code>"results"</code><code>: []</code>
synchronize子產品:http://docs.ansible.com/ansible/synchronize_module.html
注意:ansible主機和遠端主機上都需要安裝rsync
功能:将ansible主機上的源目錄下的檔案同步到遠端主機上
參數:
src:ansible主機上的源路徑
dest:遠端主機上的目标路徑
delete:delete=yes時,删除目标路徑下,源路徑中不存在的目錄或者檔案
compress:是否開啟壓縮功能,預設為開啟
mode:同步模式,預設為push,設定mode=pull,改成pull模式
archive:預設開啟了這個參數,該參數預設開啟了recursive, links, perms, times, owner,group和-D參數。如果你将該參數設定為no,那麼你将停止很多參數,比如會導緻如下目的遞歸失敗,導緻無法拉取
rsync_opts:增加rsync的額外選項,例如
rsync_opts="--exclude=fstab" 表示同步時檔案時,排除fstab檔案,即不同步fstab檔案。如果有delete=yes選項,而目标路徑下有一個源路徑下不存在的檔案,如檔案名為fstab,那麼
rsync_opts="--exclude=fstab"表示不删除目标路徑下的fstab檔案
<code>ansible pms -m synchronize -a </code><code>'src=/tmp/test/ dest=/tmp/aaa/ delete=yes rsync_opts="--exclude=fstab"'</code>
上面的指令表示将ansible主機上/tmp/test/目錄下的所有檔案(除了fstab)同步到遠端主機的/tmp/aaa/目錄下。并删除/tmp/aaa/目錄下,在/tmp/test/上不存在的檔案或者目錄
unarchive子產品:http://docs.ansible.com/ansible/unarchive_module.html
功能:解壓縮,這個子產品有兩種用法:
1、将ansible主機上的壓縮包在本地解壓縮後傳到遠端主機上,這種情況下,copy=yes
2、将遠端主機上的某個壓縮包解壓縮到指定路徑下。這種情況下,需要設定copy=no
copy:預設為yes,當copy=yes,那麼拷貝的檔案是從ansible主機複制到遠端主機上的,如果設定為copy=no,那麼會在遠端主機上尋找src源檔案
src:源路徑,可以是ansible主機上的路徑,也可以是遠端主機上的路徑,如果是遠端主機上的路徑,則需要設定copy=no
mode:設定解壓縮後的檔案權限
<code>ansible pms -m unarchive -a </code><code>'src=/srv/tomcat8/apache-tomcat-8.0.29.tar.gz dest=/usr/local copy=no mode=0755'</code>
功能:從http、https、ftp下載下傳檔案到遠端主機
url:下載下傳位址
dest:遠端主機上的目标徑路
mode:設定下載下傳到遠端主機後的檔案的權限
<code>root@host1:</code><code>/srv</code><code># ansible pms -m get_url -a 'url=ftp://ftp.cheyaoshicorp.com/pub/臨時檔案/derby.init.sh dest=/tmp' </code>
<code> </code><code>"checksum_dest"</code><code>: null, </code>
<code> </code><code>"checksum_src"</code><code>: </code><code>"770a432e9847e594e0154e31c906062585d571e0"</code><code>, </code>
<code> </code><code>"dest"</code><code>: </code><code>"/tmp/derby.init.sh"</code><code>, </code>
<code> </code><code>"gid"</code><code>: 0, </code>
<code> </code><code>"group"</code><code>: </code><code>"root"</code><code>, </code>
<code> </code><code>"md5sum"</code><code>: </code><code>"4564411c7e614859965c9ab5d76df22b"</code><code>, </code>
<code> </code><code>"mode"</code><code>: </code><code>"0644"</code><code>, </code>
<code> </code><code>"msg"</code><code>: </code><code>"OK (3934 bytes)"</code><code>, </code>
<code> </code><code>"owner"</code><code>: </code><code>"root"</code><code>, </code>
<code> </code><code>"size"</code><code>: 3934, </code>
<code> </code><code>"src"</code><code>: </code><code>"/tmp/tmp5nqAsJ"</code><code>, </code>
<code> </code><code>"state"</code><code>: </code><code>"file"</code><code>, </code>
<code> </code><code>"uid"</code><code>: 0, </code>
<code> </code><code>"url"</code><code>: </code><code>"ftp://ftp.cheyaoshicorp.com/pub/臨時檔案/derby.init.sh"</code>
本文轉自 曾哥最愛 51CTO部落格,原文連結:http://blog.51cto.com/zengestudy/1808752,如需轉載請自行聯系原作者