目錄
文章目錄
前言
ansible默認提供了很多模塊來供我們使用。
在Linux中:
ansible-doc -s 模塊名 #查看ansible模塊用法參數等幫助
ansible-doc -l #列出此版本的ansible支持哪些模塊
常用模塊
ping模塊
- name: Try to connect to host, verify a usable python and return 'pong' if successful.
#檢查控制節點和指定的被控制節點機器是否能連通,如果能連通就返回"pong"
[ansible@master ansible]$ ansible test-hosts -m ping
192.168.1.109 | SUCCESS => {
"changed": false,
"ping": "pong"
}
setup模塊
- name: Gathers facts about remote hosts
#主要用於獲取主機信息:架構信息,IP,時間,域名,網卡,MAC,主機名,CPU等信息。
#在playbooks裏經常會用到的一個參數gather_facts就與該模塊相關。
#setup模塊下經常使用的一個參數是filter參數。
#查看指定的被控制節點機器內存信息
[ansible@master ansible]$ ansible test-hosts -m setup -a "filter=ansible_*_mb"
192.168.1.109 | SUCCESS => {
"ansible_facts": {
"ansible_memfree_mb": 203,
"ansible_memory_mb": {
"nocache": {
"free": 5026,
"used": 818
},
"real": {
"free": 203,
"total": 5844,
"used": 5641
},
"swap": {
"cached": 0,
"free": 4063,
"total": 4063,
"used": 0
}
},
"ansible_memtotal_mb": 5844,
"ansible_swapfree_mb": 4063,
"ansible_swaptotal_mb": 4063
},
"changed": false
}
#查看指定的被控制節點機器內存信息並存到本地/tmp/facts/目錄
[ansible@master ansible]$ ansible test-hosts -m setup -a "filter=ansible_*_mb" --tree /tmp/facts/
[ansible@master ansible]$ ll /tmp/facts/
總用量 4
-rw-r--r-- 1 root root 330 3月 27 14:23 192.168.1.109
[ansible@master ansible]$ cat /tmp/facts/192.168.1.109
command模塊
- chdir:在運行命令之前,切換到此目錄。
- name: Executes a commandon the remote nodes.
#在指定被控制節點上運行指定命令
[ansible@master ansible]$ ansible test-hosts -m command -a 'hostname'
192.168.1.109 | SUCCESS | rc=0 >>
slave1
[ansible@master ansible]$ ansible test-hosts -m command -a 'date'
192.168.1.109 | SUCCESS | rc=0 >>
Tue Mar 26 16:36:15 CST 2019
shell模塊
shell 和command 區別:
shell模塊是通過shell程序執行的, 默認是由/bin/sh執行的, <, >, |, ;, & 都可用。但這樣有潛在的 shell 注入風險,後面再詳談。
command 模塊不通過shell程序執行,不能調用shell指令,沒有bash的環境變量,也不能使用shell的一些操作,在遇到"<",">","|","&"將會終止。
所以,command 模塊更安全,因爲他不受用戶環境的影響。 也很大的避免了潛在的 shell 注入風險。
- chdir:在運行命令之前,切換到此目錄。
- executable:更改用於執行命令的shell(bash,sh)。 應該是可執行文件的絕對路徑。
- name: Execute commands or scripts on the remote nodes.
#在指定被控制節點上運行指定命令或者控制節點的腳本。
- name: Run a command that uses non-posix shell-isms (in this example /bin/sh doesn't handle redirection and wildcards together but bash does)
shell: cat < /tmp/*txt
args:
executable: /bin/bash
raw模塊
執行原始的命令,而不是通過模塊子系統。在任何情況下,使用shell或命令模塊是合適的。給定原始的參數直接通過配置的遠程shell運行。可返回標準輸出、錯誤輸出和返回代碼。此模塊沒有變更處理程序支持。
這個模塊不需要遠程系統上的Python,就像腳本模塊一樣。此模塊也支持Windows目標。
- name: Executes a low-down and dirty SSH command
[ansible@master ansible]$ ansible test-hosts -m raw -a 'hostname|tee'
192.168.1.109 | SUCCESS | rc=0 >>
slave1
copy模塊
copy 模塊的作用就是拷貝文件,它與之前介紹過的 fetch 模塊類似,不過,fetch 模塊是從遠程主機中拉取文件到 ansible 管理主機,而 copy 模塊是將 ansible 管理主機上的文件拷貝到遠程主機中。
- src:用於指定需要copy的文件或目錄。
- dest:用於指定文件將被拷貝到遠程主機的哪個目錄中,dest爲必須參數。
- content:當不使用src指定拷貝的文件時,可以使用content直接指定文件內容,src與content兩個參數必有其一,否則會報錯。
- force: 當遠程主機的目標路徑中已經存在同名文件,並且與ansible主機中的文件內容不同時,是否強制覆蓋,可選值有yes和no,默認值爲yes,表示覆蓋,如果設置爲no,則不會執行覆蓋拷貝操作,遠程主機中的文件保持不變。
- backup: 當遠程主機的目標路徑中已經存在同名文件,並且與ansible主機中的文件內容不同時,是否對遠程主機的文件進行備份,可選值有yes和no,當設置爲yes時,會先備份遠程主機中的文件,然後再將ansible主機中的文件拷貝到遠程主機。
- owner: 指定文件拷貝到遠程主機後的屬主,但是遠程主機上必須有對應的用戶,否則會報錯。
- group: 指定文件拷貝到遠程主機後的屬組,但是遠程主機上必須有對應的組,否則會報錯。
- mode: 指定文件拷貝到遠程主機後的權限,如果你想將權限設置爲”rw-r–r--“,則可以使用mode=0644表示,如果你想要在user對應的權限位上添加執行權限,則可以使用mode=u+x表示。
- name: Copies local files to remote locations
#把主控端指定目錄下的xxx文件拷貝到到指定節點機器上
[ansible@master ansible]$ pwd
/etc/ansible
[ansible@master ansible]$ ll
總用量 36
-rw-r--r-- 1 root root 19557 3月 14 11:35 ansible.cfg
-rw-r--r-- 1 root root 1211 3月 26 16:44 hosts
drwxr-xr-x 2 root root 4096 2月 9 01:50 roles
-rw-r--r-- 1 ansible ansible 32 3月 26 17:04 test.log
drwxr-xr-x 2 root root 4096 3月 22 17:01 yml_dir
[ansible@master ansible]$ ansible test-hosts -m copy -a 'src=/etc/ansible/test.log dest=//var/lib/ansible/'
192.168.1.109 | SUCCESS => {
"changed": true,
"checksum": "c5ffc2801ba0cf17c3438fd0960a93597033f834",
"dest": "/var/lib/ansible/test.log",
"gid": 0,
"group": "ansible",
"md5sum": "2186f9799db682df586599249ecd8002",
"mode": "0644",
"owner": "ansible",
"secontext": "unconfined_u:object_r:user_home_dir_t:s0",
"size": 32,
"src": "/etc/ansible/test.log",
"state": "file",
"uid": 0
}
fetch模塊
fetch 模塊是從遠程主機中拉取文件到 ansible 管理主機。
- src:遠程系統上要獲取的文件。 這必須是一個文件,而不是一個目錄。 後續版本可能會支持遞歸提取。
- dest:保存文件的目錄。 例如,如果dest目錄是/backup,在主機host.example.com上命名爲/etc/profile的src文件將被保存到/backup/host.example.com/etc/profile。
- flat:允許您覆蓋將目標文件添加到主機名/ path / to / file的默認行爲。
# 將文件存儲到/tmp/fetched/host.example.com/tmp/somefile中
- fetch:
src: /tmp/somefile
dest: /tmp/fetched
# 直接指定路徑
- fetch:
src: /tmp/somefile
dest: /tmp/prefix-{{ inventory_hostname }}
flat: yes
# 指定目標路徑
- fetch:
src: /tmp/uniquefile
dest: /tmp/special/
flat: yes
file模塊
file模塊主要用於遠程主機上的文件操作,file模塊包含如下選項:
- force:需要在兩種情況下強制創建軟鏈接,一種是源文件不存在但之後會建立的情況下;另一種是目標軟鏈接已存在,需要先取消之前的軟鏈,然後創建新的軟鏈,有兩個選項:yes|no
- group:定義文件/目錄的屬組
- mode:定義文件/目錄的權限
- owner:定義文件/目錄的屬主
- path:必選項,定義文件/目錄的路徑
- recurse:遞歸的設置文件的屬性,只對目錄有效
- src:要被鏈接的源文件的路徑,只應用於state=link的情況
- dest:被鏈接到的路徑,只應用於state=link的情況
- state:
?? directory:如果目錄不存在,創建目錄
?? file:即使文件不存在,也不會被創建
?? link:創建軟鏈接
?? hard:創建硬鏈接
?? touch:如果文件不存在,則會創建一個新的文件,如果文件或目錄已存在,則更新其最後修改時間
?? absent:刪除目錄、文件或者取消鏈接文件
- name: Creates or Deletes and Sets attributes of files
#在指定節點上創建目錄並更改目錄的各屬性。
[ansible@master ansible]$ ansible test-hosts -m file -a "dest=/var/lib/ansible/dir1/ mode=755 owner=ansible group=ansible state=directory"
#在指定節點上刪除目錄。
[ansible@master ansible]$ ansible test-hosts -m file -a "dest=/var/lib/ansible/dir1/ state=absent"
#在指定節點上創建文件並更改文件的各屬性。
[ansible@master ansible]$ ansible test-hosts -m file -a "dest=/var/lib/ansible/test_touch.log mode=755 owner=ansible group=ansible state=touch"
#在指定節點上刪除文件。
[ansible@master ansible]$ ansible test-hosts -m file -a "dest=/var/lib/ansible/test_touch.log state=absent"
group模塊
- gid:指定用的gid;
- name:指定用戶組名稱;
- state:是創建還是刪除。(present表創建;absent表刪除)
- system:如果是yes,則表示創建的組是系統組。
- name: Add or remove groups
#在指定被控制節點上創建一個組
[ansible@master ansible]$ ansible test-hosts -m command -a "tail -5 /etc/group"
192.168.1.109 | SUCCESS | rc=0 >>
zabbix:x:201:
redis:x:492:
mailnull:x:47:
smmsp:x:51:
nagios:x:501:
#[ansible@master ansible]$ ansible test-hosts -m command -a "groupadd newgroup"
#192.168.1.109 | SUCCESS | rc=0 >>
[ansible@master ansible]$ ansible test-hosts -m group -a "name=newgroup"
192.168.1.109 | SUCCESS | rc=0 >>
redis:x:492:
mailnull:x:47:
smmsp:x:51:
nagios:x:501:
newgroup:x:502:
[ansible@master ansible]$ ansible test-hosts -m command -a "tail -5 /etc/group"
192.168.1.109 | SUCCESS | rc=0 >>
redis:x:492:
mailnull:x:47:
smmsp:x:51:
nagios:x:501:
newgroup:x:502: 《==
#在指定被控制節點上刪除一個組
#[ansible@master ansible]$ ansible test-hosts -m command -a "groupdel newgroup"
#192.168.1.109 | SUCCESS | rc=0 >>
[ansible@master ansible]$ ansible test-hosts -m group -a "name=newgroup state=absent"
192.168.1.109 | SUCCESS => {
"changed": true,
"name": "newgroup",
"state": "absent"
}
[ansible@master ansible]$ ansible test-hosts -m command -a "tail -5 /etc/group"
192.168.1.109 | SUCCESS | rc=0 >>
zabbix:x:201:
redis:x:492:
mailnull:x:47:
smmsp:x:51:
nagios:x:501:
user模塊
- name:指定用戶名;
- password:設定用戶密碼,password參數需要接受md5加密後的值;
- state:用戶狀態,默認爲present(present:表示添加用戶; absent:表示刪除用戶);
- update_password:修改用戶密碼;
- always:新密碼和舊密碼不同時進行修改;
- on_create:爲新創建的用戶指定密碼;
- createhome:創建家目錄(yes:默認項,即創建用戶默認是有家目錄的; no:創建用戶時不創建家目錄);
- remove:(yes:刪除用戶家目錄,需要指定此參數 no:默認項,刪除用戶時默認不刪除用戶的家目錄);
- system:(yes:默認創建爲普通用戶,而非系統用戶);
- 如果不指定默認生成的選項有:home:創建家目錄; shell:創建默認的shell爲/bin/bash; system:默認創建爲普通用戶,而非系統用戶,指定是用yes;
- name: Manage user accounts
#在指定被控制節點上創建一個用戶。
[ansible@master ansible]$ ansible test-hosts -m command -a "id yuki"
192.168.1.109 | FAILED | rc=1 >>
id: yuki:無此用戶non-zero return code
[ansible@master ansible]$ echo "123456" |openssl passwd -1 -stdin
$1$Ww2727.S$ObFL4sV4vdUfS9jR5GK2e1
[ansible@master ansible]$ ansible test-hosts -m user -a "name=yuki password=$1$Ww2727.S$ObFL4sV4vdUfS9jR5GK2e1"
192.168.1.109 | SUCCESS => {
"changed": true,
"comment": "",
"create_home": true,
"group": 503,
"home": "/home/yuki",
"name": "yuki",
"password": "NOT_LOGGING_PASSWORD",
"shell": "/bin/bash",
"state": "present",
"system": false,
"uid": 503
}
[ansible@master ansible]$ ansible test-hosts -m command -a "id yuki"
192.168.1.109 | SUCCESS | rc=0 >>
uid=503(yuki) gid=503(yuki) 組=503(yuki)
#在指定被控制節點上更改一個用戶的屬性。
[ansible@master ansible]$ ansible test-hosts -m user -a "name=yuki update_password=always password=$1$N08JURA8$2hzp7nhUzpQUKrAr7Ja/h0"
192.168.1.109 | SUCCESS => {
"append": false,
"changed": true,
"comment": "",
"group": 503,
"home": "/home/yuki",
"move_home": false,
"name": "yuki",
"password": "NOT_LOGGING_PASSWORD",
"shell": "/bin/bash",
"state": "present",
"uid": 503
}
#在指定被控制節點上刪除一個用戶。
[ansible@master ansible]$ ansible test-hosts -m user -a "name=yuki remove=yes state=absent"
192.168.1.109 | SUCCESS => {
"changed": true,
"force": false,
"name": "yuki",
"remove": true,
"state": "absent"
}
[ansible@master ansible]$ ansible test-hosts -m command -a "id yuki"
192.168.1.109 | FAILED | rc=1 >>
id: yuki:無此用戶non-zero return code
yum模塊
- config_file:yum的配置文件;
- disable_gpg_check:關閉gpg_check;
- disablerepo:不啓用某個源;
- enablerepo:啓用某個源;
- name:要進行操作的軟件包的名字,也可以傳遞一個url或者一個本地的rpm包的路徑;
- state:狀態(present:默認的,表示爲安裝;lastest:安裝爲最新的版本;absent:表示刪除)
- name: Manages packages with the `yum' package manager
#安裝最新版本的apache
[ansible@master ansible]$ ansible test-hosts -m yum -a "name=httpd state=latest"
#移除apache
[ansible@master ansible]$ ansible test-hosts -m yum -a "name=httpd state=absent"
#安裝指定版本
[ansible@master ansible]$ ansible test-hosts -m yum -a "name=httpd-2.2.29-1.4.amzn1 state=present"
#安裝整個Development tools相關的軟件包
[ansible@master ansible]$ ansible test-hosts -m yum -a "name='@Development tools' state=present"
#從一個遠程yum倉庫安裝nginx
[ansible@master ansible]$ ansible test-hosts -m yum -a "name=http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm state=present"
service模塊
- arguments:給命令行提供一些選項,aliases: args;
- enabled:是否開機啓動 yes,否則是no;
- name: 必須填寫,服務名稱;
- pattern:定義一個模式,如果通過status指令來查看服務的狀態時,沒有響應,就會通過ps指令在進程中根據該模式進行查找,如果匹配到,則認爲該服務依然在運行。
- runlevel:運行級別;
- sleep:如果執行了restarted,在則stop和start之間沉睡幾秒鐘;
- state:對當前服務執行(started啓動,stopped停止,restarted重啓,reloaded重新加載等操作);
- use:服務模塊實際上使用系統特定的模塊,通常是自動檢測,這個設置可以強制一個特定的模塊。通常情況下,使用ansible_service_mgr 的值,並在找不到匹配的情況下推到舊的服務模塊;
- name: Manage services
#啓動nginx服務,沒有運行則啓動
[ansible@master ansible]$ ansible test-hosts -m service -a "name=nginx state=started enabled=yes"
#停止nginx服務
[ansible@master ansible]$ ansible test-hosts -m service -a "name=nginx state=stopped"
#重啓nginx 服務
[ansible@master ansible]$ ansible test-hosts -m service -a "name=nginx state=restarted"
#重新加載
[ansible@master ansible]$ ansible test-hosts -m service -a "name=nginx state=reloaded"
#啓動服務foo
[ansible@master ansible]$ ansible test-hosts -m service -a "name=foo pattern=/usr/bin/foo state=started"
#重啓eth0 網卡
[ansible@master ansible]$ ansible test-hosts -m service -a "name=network state=restarted args=eth0"
script模塊
- name: Runs a local script on a remote node after transferring it
#在指定的被控制節點上執行控制節點上的腳本
[ansible@master ansible]$ ansible t3 -m script -a "/home/script/test.sh"
cron模塊
- state:(present:創建任務; absent:刪除任務);
- backup:對遠程主機上的原任務計劃內容修改之前做備份;
- job:要執行的任務;
- name:該任務的描述(必須項);
- user:以哪個用戶的身份運行;
- minute:分鐘(0-59),默認爲*;
- hour:小時(0-23),默認爲*;
- day:日(1-31),默認爲*;
- month:月(1-12),默認爲*;
- weekday:周(0-7),默認爲*;
[ansible@master ansible]$ ansible test-hosts -m cron -a "name='sync time from ntpserver' minute=*/10 job='/usr/sbin/ntpdate 3.cn.pool.ntp.org'"
[ansible@master ansible]$ ansible test-hosts -m cron -a 'name="custom job" minute=*/3 hour=* day=* month=* weekday=* job="/usr/sbin/ntpdate 172.16.254.139"'
unarchive模塊
- copy: 在解壓文件之前,是否先將文件複製到遠程主機,默認爲yes,no則要求目標主機上壓縮包必須存在;
- creates:指定一個文件名,當改文件存在時,則解壓指令不執行;
- dest: 遠程主機上的一個路徑,即文件解壓的路徑;
- src:如果copy爲yes ,則需要指定壓縮文件的源路徑;
- group:解壓後的目錄或文件的屬組;
- mode: 解壓後文件的權限;
- owner: 解壓後文件或目錄的屬主;
- list_files: yes,則會列出壓縮包裏的文件,默認爲no;
- name: Unpacks an archive after (optionally) copying it from the local machine.
#解壓控制節點機器上的指定文件到指定的被控制節點機器上的指定路徑下
[ansible@master ansible]$ ansible test-hosts -m unarchive -a "src=foo.tgz dest=/var/lib/foo"
#在指定的被控制節點機器上解壓指定文件到指定路徑下
[ansible@master ansible]$ ansible test-hosts -m unarchive -a "src=/tmp/foo.zip dest=/usr/local/bin remote_src: yes"
#在指定的被控制節點機器上解壓需要先下載的文件到指定路徑下 (added in 2.0version)
[ansible@master ansible]$ ansible test-hosts -m unarchive -a "src=https://example.com/example.zip dest=/usr/local/bin remote_src: yes"
get_url模塊
該模塊主要用於從http、ftp、https服務器上下載文件(類似於wget),主要有如下參數:
- sha256sum:下載完成後進行sha256 check;
- timeout:下載超時時間,默認10s;
- url:下載的URL;
- url_password、url_username:主要用於需要用戶名密碼進行驗證的情況;
- dest:將文件下載到哪裏的絕對路徑。如果dest是目錄,則使用服務器提供的文件名,或者如果沒有提供,將使用遠程服務器上的URL的基本名稱。
- headers:以格式“key:value,key:value”爲請求添加自定義HTTP標頭;
- name: Downloads files from HTTP, HTTPS, or FTP to nodes
[ansible@master ansible]$ ansible test-hosts -m get_url -a 'url=http://10.1.1.116/favicon.ico dest=/tmp'
synchronize模塊
- archive: 歸檔,相當於同時開啓recursive(遞歸)、links、perms、times、owner、group、-D選項都爲yes,默認該項爲開啓;
- checksum: 跳過檢測sum值,默認關閉;
- compress:是否開啓壓縮;
- copy_links:複製鏈接文件,默認爲no ,注意後面還有一個links參數;
- delete: 刪除不存在的文件,默認no;
- src: 源路徑;
- dest:目的路徑;
- dest_port:默認目錄主機上的端口 ,默認是22,走的ssh協議;
- dirs:傳速目錄不進行遞歸,默認爲no,即進行目錄遞歸;
- rsync_opts:rsync參數部分;
- set_remote_user:主要用於/etc/ansible/hosts中定義或默認使用的用戶與rsync使用的用戶不同的情況;
- mode: push或pull 模塊,push模的話,一般用於從本機向遠程主機上傳文件,pull 模式用於從遠程主機上取文件;
- name: A wrapper around rsync to make common tasks in your playbooks quick and easy.
#使用rsync同步文件
[ansible@master ansible]$ ansible test-hosts -m synchronize -a "src=/tmp/nginx-1.13.3 dest=/tmp/ rsync_opts='--exclude=configure,--exclude=LICENSE'"
[ansible@master ansible]$ ansible test-hosts -m synchronize -a "src=/tmp/nginx-1.13.3 dest=/tmp/ checksum=yes times=no links=yes rsync_opts='--exclude=configure,--exclude=LICENSE'"