Ansible Ad-Hoc與常用模組
ansible 執行結果資訊–各顏色說明;ansible Ad-Hoc 說明;ansible 如何檢視幫助文件與常用模組詳解
主機規劃
新增使用者賬號
說明:
1、 運維人員使用的登入賬號;
2、 所有的業務都放在 /app/ 下「yun使用者的家目錄」,避免業務資料亂放;
3、 該使用者也被 ansible 使用,因為幾乎所有的生產環境都是禁止 root 遠端登入的(因此該 yun 使用者也進行了 sudo 提權)。
1 # 使用一個專門的使用者,避免直接使用root使用者 2 # 新增使用者、指定家目錄並指定使用者密碼 3 # sudo提權 4 # 讓其它普通使用者可以進入該目錄檢視資訊 5 useradd -u 1050 -d /app yun && echo '123456' | /usr/bin/passwd --stdin yun 6 echo "yun ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers 7 chmod 755 /app/
Ansible 配置清單Inventory
之後文章都是如下主機配置清單
1 [yun@ansi-manager ansible_info]$ pwd 2 /app/ansible_info 3 [yun@ansi-manager ansible_info]$ cat hosts_key 4 # 方式1、主機 + 埠 + 金鑰 5 [manageservers] 6 172.16.1.180:22 7 8 [proxyservers] 9 172.16.1.18[1:2]:22 10 11 # 方式2:別名 + 主機 + 埠 + 密碼 12 [webservers] 13 web01 ansible_ssh_host=172.16.1.183 ansible_ssh_port=22 14 web02 ansible_ssh_host=172.16.1.184 ansible_ssh_port=22 15 web03 ansible_ssh_host=172.16.1.185 ansible_ssh_port=22
Ansible執行返回 => 顏色資訊說明
黃色:成功執行並且伴隨著狀態的改變
ansible proxyservers -m command -a 'cat /etc/hosts' -i hosts_key
綠色:成功執行並且沒有發生狀態的改變,或者只是對遠端節點狀態資訊進行檢視
ansible proxyservers -m ping -i hosts_key
紅色:操作執行命令有異常
ansible proxyservers -m command -a 'll /tmp' -i hosts_key
紫色:表示對命令執行發出警告資訊(可能存在的問題,給你一下建議)
1 # 其中 hosts_kkk 檔案不存在 2 ansible proxyservers -m command -a 'll /tmp' -i hosts_kkk
Ansible 之 Ad-Hoc
Ansible中有兩種模式, 分別是 Ad-Hoc 模式和 Playbooks 模式。
ad-hoc簡而言之,就是“臨時命令”,不會儲存。
ad-hoc模式的使用場景
場景一,在多臺機器上,檢視某個程序是否啟動
場景二,在多臺機器上,拷貝指定日誌檔案到本地,等等
ad-hoc模式的命令使用
Ansible檢視幫助方法
1 [yun@ansi-manager ~]$ ansible-doc -l # 檢視所有模組與簡要說明 2 [yun@ansi-manager ~]$ ansible-doc copy # 檢視指定模組方法「可優先檢視 EXAMPLES 資訊」★★★★★ 3 [yun@ansi-manager ~]$ ansible-doc -s copy # 檢視指定模組的 Playbooks 程式碼段
Ansible常用模組
工作目錄與主機清單
當前所在的工作目錄和主機清單
1 [yun@ansi-manager ansible_info]$ pwd 2 /app/ansible_info 3 [yun@ansi-manager ansible_info]$ ll 4 total 4 5 -rw-rw-r-- 1 yun yun 226 Oct 8 16:07 hosts_key 6 [yun@ansi-manager ansible_info]$ cat hosts_key 7 # 方式1、主機 + 埠 + 金鑰 8 [manageservers] 9 172.16.1.180:22 10 11 [proxyservers] 12 172.16.1.18[1:2]:22 13 14 # 方式2:別名 + 主機 + 埠 + 密碼 15 [webservers] 16 web01 ansible_ssh_host=172.16.1.183 ansible_ssh_port=22 17 web02 ansible_ssh_host=172.16.1.184 ansible_ssh_port=22 18 web03 ansible_ssh_host=172.16.1.185 ansible_ssh_port=22
command 命令模組
預設模組, 用於執行命令。但不支援管道或重定向。
正常示例
1 [yun@ansi-manager ansible_info]$ ansible proxyservers -a 'df -h' -i hosts_key 2 # 或者 3 [yun@ansi-manager ansible_info]$ ansible proxyservers -m command -a 'df -h' -i hosts_key
異常示例
1 # 重定向不支援 2 ansible proxyservers -m command -a 'df -h > /tmp/df.info' -i hosts_key 3 # 管道不支援 4 ansible proxyservers -m command -a "df -h | grep 'boot'" -i hosts_key
shell 命令模組
功能和 command 相同,且支援管道和重定向。與 command 相比,優先使用該模組。
示例
1 ansible proxyservers -m shell -a "df -h | grep 'boot'" -i hosts_key 2 ansible proxyservers -m shell -a "df -h > /tmp/df.info" -i hosts_key
script 指令碼模組
在本地執行模組,等同於在遠端執行。且不需要將指令碼檔案推送到目標主機進行執行。
示例
指令碼中有 sudo 提權
1 # 在 ansible 管理機操作 2 [yun@ansi-manager ansible_info]$ cat /app/yunwei/yum_test.sh 3 #!/bin/sh 4 # 由於使用的是 yun 使用者,而不是 root 使用者,因此需要 sudo 提權 5 sudo yum install -y iftop 6 [yun@ansi-manager ansible_info]$ ansible proxyservers -m script -a "/app/yunwei/yum_test.sh" -i hosts_key
指令碼中無 sudo 提權
1 # 在 ansible 管理機操作 2 [yun@ansi-manager ansible_info]$ cat /app/yunwei/yum_test.sh 3 #!/bin/sh 4 yum install -y iftop 5 ####################################### 6 # 由於我們使用的是 yun 普通使用者 7 # 因此這裡需要使用 -b 選擇進行提權,這樣在遠端會以 root 使用者執行 8 [yun@ansi-manager ansible_info]$ ansible proxyservers -b -m script -a "/app/yunwei/yum_test.sh" -i hosts_key
在目標機器檢視是否在 yum 安裝 iftop
1 [root@ansi-haproxy01 ~]# ps -ef | grep 'iftop' 2 root 3867 3866 0 23:25 pts/1 00:00:00 sudo yum install -y iftop 3 root 3868 3867 48 23:25 pts/1 00:00:12 /usr/bin/python /bin/yum install -y iftop 4 root 4144 3155 0 23:25 pts/0 00:00:00 grep --color=auto iftop
yum 安裝軟體模組
在目標機器實現 yum 安裝軟體
1 # 檢視 yum 模組方法「可優先檢視 EXAMPLES 資訊的使用案例,知曉如何使用」 2 [yun@ansi-manager ansible_info]$ ansible-doc yum 3 # 由於我們使用的是 yun 普通使用者 4 # 因此必須使用 -b 選擇進行提權 5 [yun@ansi-manager ansible_info]$ ansible proxyservers -b -m yum -a 'name=httpd state=present' -i ./hosts_key
相關選項說明:
name:要安裝軟體包的名稱
state:狀態說明
‘present’ 和 ‘installed’ 簡單地確保安裝了所需的包。「優先使用 present」
‘latest’ 將更新指定的軟體包,如果它不是最新可用的版本。
‘absent’ and ‘removed’ 將刪除指定的包【慎用!!!】。「如要使用優先使用 absent」
download_only:只下載包,不安裝
copy 檔案拷貝模組「本地到遠端」
將控制機的檔案或目錄拷貝到受控機,並且可以指定目標檔案/目錄的屬性資訊。
控制機操作
1 # 檢視 yum 模組方法「可優先檢視 EXAMPLES 資訊的使用案例,知曉如何使用」 2 [yun@ansi-manager ansible_info]$ ansible-doc copy 3 # 由於我們使用的是 yun 普通使用者 4 # 因此有時需要使用 -b 選擇進行提權 5 # 將 content 中的內容直接寫入目標檔案中 6 [yun@ansi-manager ansible_info]$ ansible proxyservers -b -m copy -a "content='123\n' dest=/tmp/copy_test2 owner=root group=root mode=0644 backup=yes" -i ./hosts_key 7 ## 拷貝檔案 8 [yun@ansi-manager ansible_info]$ cat /tmp/copy_test 9 111111 10 222222 11 333333 12 [yun@ansi-manager ansible_info]$ ansible proxyservers -b -m copy -a "src=/tmp/copy_test dest=/tmp/ owner=root group=root mode=0644 backup=yes" -i ./hosts_key 13 ## 拷貝目錄 14 [yun@ansi-manager ansible_info]$ ansible proxyservers -b -m copy -a "src=/app/yunwei dest=/tmp/ owner=root group=root mode=0644 backup=yes" -i ./hosts_key
被控端檢視
1 [yun@ansi-haproxy01 tmp]$ cat copy_test2 2 123 3 [yun@ansi-haproxy01 tmp]$ cat /tmp/copy_test 4 111111 5 222222 6 333333
相關選項說明:
src:原始檔「可以是絕對路徑或相對路徑」
remote_src:為 False「預設」,則原始檔在本地;為 True ,則原始檔在遠端「瞭解」
dest:推送資料的目標路徑或目標檔案
owner:指定遠端檔案的屬主
group:指定遠端檔案的屬組
mode:指定遠端檔案的許可權
backup:如果推送的目標檔案存在且與原始檔內容不同,那麼會對目標檔案進行備份「通過 checksum 校驗」
content:將 content 中的內容直接寫入目標檔案中
注意事項:
1、同一個原始檔,如果原始檔內容沒有任何修改,那麼進行第二次相同的操作時,檢測到要拷貝的檔案和目標檔案內容相同「通過 checksum 校驗」,且目標檔案屬性前後未發生改變,那麼就不會進行拷貝。由於目標檔案屬性未發生改變,所以返回資料顏色為綠色。如下圖所示。
2、同一個原始檔,如果原始檔內容沒有任何修改,那麼進行第二次相同的操作時,如果原始檔和目標檔案內容相同「通過 checksum 校驗」,但目標檔案屬性前後要求發生改變「屬主、屬組、許可權」,那樣也不會進行拷貝。但由於目標檔案屬性發生改變,所以返回資料顏色為黃色。
fetch 檔案拷貝模組「遠端到本地」
該模組功能類似於 copy 模組,但是是反向的。將遠端的檔案拷貝到本地。備註:當前僅支援檔案,暫不支援遞迴拷貝。
由於ansible使用的是 yun 使用者,因此從遠端拷貝過來的檔案屬主、屬組都是 yun。
控制機操作
1 # 檢視 yum 模組方法「可優先檢視 EXAMPLES 資訊的使用案例,知曉如何使用」 2 [yun@ansi-manager ansible_info]$ ansible-doc fetch 3 # 由於我們使用的是 yun 普通使用者 4 # 因此有時需要使用 -b 選擇進行提權
被控端檔案準備1
1 [test1@ansi-haproxy01 tmp]$ ll /tmp/test1 2 -rw-rw-r-- 1 test1 test1 20 Nov 2 11:04 /tmp/test1 3 [test1@ansi-haproxy01 tmp]$ cat /tmp/test1 4 111 5 222 6 333 7 aaa 8 bbb
被控端檔案準備2
1 [test1@ansi-haproxy02 tmp]$ ll /tmp/test1 2 -rw-rw-r-- 1 test1 test1 20 Nov 2 11:04 /tmp/test1 3 [test1@ansi-haproxy02 tmp]$ cat /tmp/test1 4 1111
拷貝方式1
1 [yun@ansi-manager ansible_info]$ ansible 172.16.1.181 -b -m fetch -a "src=/tmp/test1 dest=/tmp/" -i ./hosts_key # 拷貝 172.16.1.181 主機的 2 或者 3 [yun@ansi-manager ansible_info]$ ansible proxyservers -b -m fetch -a "src=/tmp/test1 dest=/tmp/" -i ./hosts_key # 拷貝 proxyservers 主機組的 4 ## 檢視拷貝結果「注意目錄層次」 5 [yun@ansi-manager ansible_info]$ ll /tmp/ 6 total 4 7 drwxrwxr-x 3 yun yun 17 Nov 2 11:21 172.16.1.181 8 drwxrwxr-x 3 yun yun 17 Nov 2 11:23 172.16.1.182 9 [yun@ansi-manager ansible_info]$ tree /tmp/172.16.1.18* 10 /tmp/172.16.1.181 11 └── tmp 12 └── test1 13 /tmp/172.16.1.182 14 └── tmp 15 └── test1 16 17 2 directories, 2 files
拷貝方式2
1 # 如果使用 flat=yes,那麼最好只拷貝一臺遠端主機的檔案,如果是多臺那麼後面執行的結果,會把前面的覆蓋掉。 2 # dest 路徑有 / 結尾 3 [yun@ansi-manager ansible_info]$ ansible 172.16.1.181 -b -m fetch -a "src=/tmp/test1 dest=/tmp/kkk2/ flat=yes" -i ./hosts_key # 推薦,只拷貝一臺 4 # dest 路徑無 / 結尾 5 [yun@ansi-manager ansible_info]$ ansible 172.16.1.181 -b -m fetch -a "src=/tmp/test1 dest=/tmp/kkk flat=yes" -i ./hosts_key # 推薦,只拷貝一臺 6 [yun@ansi-manager ansible_info]$ ansible proxyservers -b -m fetch -a "src=/tmp/test1 dest=/tmp/kkk flat=yes" -i ./hosts_key # 不推薦,會產生覆蓋 7 ## 檢視拷貝結果 8 [yun@ansi-manager ansible_info]$ cat /tmp/kkk2/test1 9 111 10 222 11 333 12 aaa 13 bbb 14 [yun@ansi-manager ansible_info]$ ll /tmp/kkk 15 -rw-rw-r-- 1 yun yun 20 Nov 2 11:25 /tmp/kkk 16 [yun@ansi-manager ansible_info]$ cat /tmp/kkk # 該檔案沒有 "11111"資訊; 產生了覆蓋效應 17 111 18 222 19 333 20 aaa 21 bbb
相關選項說明:
src:原始檔,當前僅支援檔案,不支援目錄
dest:推送資料的目標路徑,預設為:dest[路徑]/hostname/src[路徑]。參見上面示例
flat: 預設 False。當為 yes/True 時,那麼拷貝效果類似於本地的 copy。
template 模板使用
該模組功能類似於 copy 模組,但 copy 模組不支援變數,不支援模板。
template 模組支援變數,支援 Jinja 模板。因此如果生成中的配置檔案涉及變數,那麼請使用 template 模組。
涉及到 playbook、變數和 Jinja ,這些在後面的文章會有詳解。
1 # 檢視 yum 模組方法「可優先檢視 EXAMPLES 資訊的使用案例,知曉如何使用」 2 [yun@ansi-manager ansible_info]$ ansible-doc template 3 # 由於我們使用的是 yun 普通使用者 4 # 因此有時需要使用 -b 選擇進行提權 5 # palybook 6 [yun@ansi-manager ansible_info]$ pwd 7 /app/ansible_info 8 [yun@ansi-manager ansible_info]$ ll 9 total 16 10 drwxrwxr-x 2 yun yun 35 Oct 11 11:23 file 11 -rw-rw-r-- 1 yun yun 226 Oct 8 16:07 hosts_key 12 -rw-rw-r-- 1 yun yun 304 Oct 11 11:40 test_template.yml 13 [yun@ansi-manager ansible_info]$ ll file/ # 涉及的檔案 14 total 4 15 -rw-rw-r-- 1 yun yun 175 Oct 11 11:23 test_template.conf.j2 16 [yun@ansi-manager ansible_info]$ cat file/test_template.conf.j2 17 # facts 變數 18 dns_info={{ ansible_dns['nameservers'][0] }} 19 mem_total={{ ansible_memtotal_mb }} 20 # 自定義變數 21 listen_port={{ listen_port }} 22 access_addr={{ access_addr }} 23 24 [yun@ansi-manager ansible_info]$ 25 [yun@ansi-manager ansible_info]$ cat test_template.yml # 涉及的 playbook 26 --- 27 # template 示例 28 - hosts: proxyservers 29 vars: 30 - listen_port: 8080 31 - access_addr: zhangblog.com 32 33 tasks: 34 - name: "template conf" 35 template: 36 src: ./file/test_template.conf.j2 37 dest: /tmp/test_template.conf 38 owner: root 39 group: yun 40 mode: '0600' 41 [yun@ansi-manager ansible_info]$ ansible-playbook -b -i ./hosts_key test_template.yml # 執行
目標機器檢視
1 [root@ansi-haproxy01 tmp]# pwd 2 /tmp 3 [root@ansi-haproxy01 tmp]# cat test_template.conf 4 # facts 變數 5 dns_info=223.5.5.5 6 mem_total=1821 7 # 自定義變數 8 listen_port=8080 9 access_addr=zhangblog.com
相關選項說明:
src:原始檔「可以是絕對路徑或相對路徑」
dest:推送資料的目標路徑或目標檔案
owner:指定遠端檔案的屬主
group:指定遠端檔案的屬組
mode:指定遠端檔案的許可權
backup:如果推送的目標檔案存在且與原始檔內容不同,那麼會對目標檔案進行備份
file 檔案配置模組
在受控機建立檔案或目錄,或修改屬性資訊「如:屬主、屬組、許可權」
1 # 檢視 yum 模組方法「可優先檢視 EXAMPLES 資訊的使用案例,知曉如何使用」 2 [yun@ansi-manager ansible_info]$ ansible-doc file 3 # 由於我們使用的是 yun 普通使用者 4 # 因此有時需要使用 -b 選擇進行提權 5 ## 在受控機改變目標檔案的屬性【該檔案是已存在的】 6 [yun@ansi-manager ansible_info]$ ansible proxyservers -b -m file -a "path=/tmp/yum_test.sh owner=yun group=yun mode=0600" -i ./hosts_key 7 ## 在受控機建立軟連線 8 [yun@ansi-manager ansible_info]$ ansible proxyservers -b -m file -a "src=/tmp/yum_test.sh dest=/tmp/yum_link.sh owner=yun group=yun state=link" -i ./hosts_key 9 ## 在受控機建立硬連結 10 [yun@ansi-manager ansible_info]$ ansible proxyservers -b -m file -a "src=/tmp/yum_test.sh dest=/tmp/yum_hard.sh owner=yun group=root state=hard" -i ./hosts_key 11 ## 在受控機,如果目標檔案不存在則建立 12 [yun@ansi-manager ansible_info]$ ansible proxyservers -b -m file -a "path=/tmp/dest_file owner=yun group=yun state=touch" -i ./hosts_key 13 ## 在控制機,如果目標目錄不存在則建立「可建立多級目錄」 14 [yun@ansi-manager ansible_info]$ ansible proxyservers -b -m file -a "path=/tmp/desc_dir/aaa/bbb owner=yun group=root mode=700 state=directory" -i ./hosts_key 15 ## 在控制機,改變目標目錄和目錄下所有目錄或檔案的屬性資訊「遞迴修改」 16 [yun@ansi-manager ansible_info]$ ansible proxyservers -b -m file -a "path=/tmp/desc_dir owner=yun group=zhang mode=766 state=directory recurse=yes" -i ./hosts_key 17 ## 在受控機,如果目標檔案或目錄存在,則刪除「慎用!!!」 18 [yun@ansi-manager ansible_info]$ ansible proxyservers -b -m file -a "path=/tmp/desc_dir state=absent " -i ./hosts_key
相關選項說明:
path:指定目標檔案或目錄
owner:指定目標檔案的屬主
group:指定目標檔案的屬組
mode:指定目標檔案的許可權
state:狀態說明
file:預設值,指定檔案
link:建立軟連線
hard:建立硬連結
touch:如果檔案不存在則建立
directory:如果目錄不存在則建立
absent:如果目標檔案或目錄存在,則刪除「慎用!!!」
recurse:遞迴授權
lineinfile 行編輯模組
此模組確保檔案中有特定的行,或者使用反向引用的正則表示式替換現有的行。當您只想更改檔案中的一行時,這非常有用。
如果您想要更改多個相似的行,請檢視[replace]模組。如果你想要插入/更新/刪除檔案中的一個行塊,請檢視[blockinfile]模組。對於其他情況,請參見[copy]或[template]模組。
資料檔案準備
1 [yun@ansi-manager tmp]$ cat /tmp/lineinfile_test 2 # disabled - No SELinux policy is loaded. 3 SELINUX=disabled1 4 # disabled - No SELinux policy is loaded. 5 SELINUX=disabled2 6 # disabled - No SELinux policy is loaded. 7 SELINUX=disabled3 8 # SELINUXTYPE= can take one of three two values: 9 10 # httpd listen port 11 Listen 80
1 # 檢視 yum 模組方法「可優先檢視 EXAMPLES 資訊的使用案例,知曉如何使用」 2 [yun@ansi-manager ansible_info]$ ansible-doc lineinfile 3 # 由於我們使用的是 yun 普通使用者 4 # 因此有時需要使用 -b 選擇進行提權
行插入
1 # 如果檔案中沒有 line 中的字串,那麼就追加在檔案末尾;有則不作任何操作。 2 [yun@ansi-manager ansible_info]$ ansible manageservers -b -m lineinfile -a "path=/tmp/lineinfile_test line='# who are you?'" -i ./hosts_key 3 # 首先保證line 中的字串在檔案中沒有,如果有則不會新增 4 # 其次會使用insertafter中的正則規則進行正則匹配,匹配成功則在最後一次匹配行後面插入line,如果沒匹配成功則在檔案末尾插入 5 [yun@ansi-manager ansible_info]$ ansible manageservers -b -m lineinfile -a "path=/tmp/lineinfile_test insertafter='SELINUX' line='insertafter test1'" -i ./hosts_key 6 [yun@ansi-manager ansible_info]$ ansible manageservers -b -m lineinfile -a "path=/tmp/lineinfile_test insertafter='SELINUXXX' line='insertafter test2'" -i ./hosts_key 7 # 首先保證line 中的字串在檔案中沒有,如果有則不會新增 8 # 其次會使用insertbefore中的正則規則進行正則匹配,匹配成功則在最後一次匹配行前面插入line,如果沒匹配成功則在檔案末尾插入 9 [yun@ansi-manager ansible_info]$ ansible manageservers -b -m lineinfile -a "path=/tmp/lineinfile_test insertbefore='SELINUX' line='insertbefore test1'" -i ./hosts_key 10 [yun@ansi-manager ansible_info]$ ansible manageservers -b -m lineinfile -a "path=/tmp/lineinfile_test insertbefore='SELINUXXX' line='insertbefore test2'" -i ./hosts_key
行替換
1 # state=present時,如果多次匹配,那麼最後一次匹配會被修改;如果沒有匹配成功則在檔案末尾追加,不管line是否存在。 2 [yun@ansi-manager ansible_info]$ ansible manageservers -b -m lineinfile -a "path=/tmp/lineinfile_test regexp='^SELINUX=' line='SELINUX=enforcing'" -i ./hosts_key 3 # state=present時,如果多次匹配,那麼最後一次匹配會被修改;如果沒有匹配成功則檔案保持不變,使用backrefs=yes。 4 [yun@ansi-manager ansible_info]$ ansible manageservers -b -m lineinfile -a "path=/tmp/lineinfile_test regexp='^ELINUX=' line='SELINUX=enforcing' backrefs=yes" -i ./hosts_key
行刪除
1 # state=absent時,如果多次匹配,那麼每一次匹配都會刪除匹配行 2 [yun@ansi-manager ansible_info]$ ansible manageservers -b -m lineinfile -a "path=/tmp/lineinfile_test regexp='^SELINUX=' state=absent" -i ./hosts_key 3 # 根據line匹配,如果匹配則刪除匹配行 4 [yun@ansi-manager ansible_info]$ ansible manageservers -b -m lineinfile -a "path=/tmp/lineinfile_test line='# httpd listen port' state=absent" -i ./hosts_key
相關選項說明:
path:要修改的檔案。
line:與 state=present 配合使用;在檔案中要插入或者替換的行。
state:狀態說明
present:新增或修改,預設值
absent:刪除
backrefs:與 state=present 配合使用;如果設定了 line 可以包含反向引用(位置和命名),當‘regexp’匹配,就會填充反向引用。
這個引數稍微改變了模組的操作;’insertbefore’ 和 ‘insertafter’ 將被忽略,如果 ‘regexp’ 與檔案中的任何地方不匹配,檔案將保持不變。
regexp:對檔案的每行進行正則匹配;對於 state=present 只有最後的一次行匹配會被替換;對於 state=absent 只要匹配就會刪除該行。
backup:對原始檔備份。預設:False
create:與 state=present 配合使用;如果不存在則建立檔案。預設:False
insertafter:與 state=present 配合使用;在匹配行後插入。使用正則表示式,在指定正則表示式的最後一次匹配之後插入該行。如果需要第一個匹配,則使用(firstmatch=yes)。
如果沒有匹配成功,那麼會在檔案末尾處插入。優先順序低於 regexp。
insertbefore:與 state=present 配合使用;在匹配行前插入。使用正則表示式,在指定正則表示式的最後一次匹配之前插入該行。如果需要第一個匹配,則使用(firstmatch=yes)。
如果沒有匹配成功,那麼會在檔案末尾處插入。優先順序低於 regexp。
firstmatch:與 insertafter 或 insertbefore 配合使用;在 insertafter 或 insertbefore 的首次正則匹配。預設:False
owner:指定遠端檔案的屬主
group:指定遠端檔案的屬組
mode:指定遠端檔案的許可權
blockinfile 多行編輯模組
該模組可以幫助我們在指定的檔案中插入”一段文字”,這段文字是被標記過的。
換句話說就是,我們在這段文字上做了記號,以便在以後的操作中可以通過”標記”找到這段文字,然後修改或者刪除它
1 # 檢視 yum 模組方法「可優先檢視 EXAMPLES 資訊的使用案例,知曉如何使用」 2 [yun@ansi-manager ansible_info]$ ansible-doc blockinfile 3 # 由於我們使用的是 yun 普通使用者 4 # 因此有時需要使用 -b 選擇進行提權
對不存在的檔案進行——多行插入與修改
1 # /tmp/blockinfile_test 檔案是不存在的,因此使用了create選項 2 # 插入/修改 如果沒有則插入,如果之前有資訊則修改 3 ##### 這裡之前沒有,則是建立檔案並【插入】資訊 4 [yun@ansi-manager ansible_info]$ ansible manageservers -b -m blockinfile -a 'path=/tmp/blockinfile_test block="Match User ansible-agent\nPasswordAuthentication no" create=yes' -i ./hosts_key 5 [root@ansi-manager ansible_info]# cat /tmp/blockinfile_test # 檢視檔案資訊 6 # BEGIN ANSIBLE MANAGED BLOCK 7 Match User ansible-agent 8 PasswordAuthentication no 9 # END ANSIBLE MANAGED BLOCK 10 ##### 檔案已存在,mark標記已存在,這裡是【修改】 11 [yun@ansi-manager ansible_info]$ ansible manageservers -b -m blockinfile -a 'path=/tmp/blockinfile_test block="iface eth0 inet static\n address 192.0.2.23\n netmask 255.255.255.0" create=yes' -i ./hosts_key 12 [root@ansi-manager ansible_info]# cat /tmp/blockinfile_test # 檢視檔案資訊 13 # BEGIN ANSIBLE MANAGED BLOCK 14 iface eth0 inet static 15 address 192.0.2.23 16 netmask 255.255.255.0 17 # END ANSIBLE MANAGED BLOCK
對已存在的檔案進行——多行插入與修改
1 [root@ansi-manager ansible_info]# cat /tmp/blockinfile_test2 # 檢視檔案資訊 2 <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> 3 <html> 4 <head> 5 <title>blockinfile info</title> 6 </head> 7 <body> 8 <h1>welcome to here.</h1> 9 </body></html> 10 ##### 插入多行標記塊資訊 這裡也使用了 insertafter 選項 11 [yun@ansi-manager ansible_info]$ ansible manageservers -b -m blockinfile -a 'path=/tmp/blockinfile_test2 marker="<!-- {mark} ANSIBLE MANAGED BLOCK -->" insertafter="<body>" block="<h1>Welcome to blockinfile</h1>\n<p>Last Login By you!</p>"' -i ./hosts_key 12 [root@ansi-manager ansible_info]# cat /tmp/blockinfile_test2 # 檢視檔案資訊 13 <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> 14 <html> 15 <head> 16 <title>blockinfile info</title> 17 </head> 18 <body> 19 <!-- BEGIN ANSIBLE MANAGED BLOCK --> 20 <h1>Welcome to blockinfile</h1> 21 <p>Last Login By you!</p> 22 <!-- END ANSIBLE MANAGED BLOCK --> 23 <h1>welcome to here.</h1> 24 </body></html> 25 ##### 刪除標記塊資訊 26 [yun@ansi-manager ansible_info]$ ansible manageservers -b -m blockinfile -a 'path=/tmp/blockinfile_test2 marker="<!-- {mark} ANSIBLE MANAGED BLOCK -->" block=""' -i ./hosts_key 27 [root@ansi-manager ansible_info]# cat /tmp/blockinfile_test2 # 檢視檔案資訊 28 <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> 29 <html> 30 <head> 31 <title>blockinfile info</title> 32 </head> 33 <body> 34 <h1>welcome to here.</h1> 35 </body></html>
相關選項說明:
path:要修改的檔案。
block:要插入標記行內的文字。如果該選項缺失或是一個空字串,該塊將被刪除,就像 “state” 被指定為 “absent” 。(別名:content)
state:狀態說明
present:新增或修改,預設值
absent:刪除
create:檔案不存在則建立。預設:False
backup:對原始檔備份。預設:False
insertafter:在匹配行後插入。使用正則表示式,在指定正則表示式的最後一次匹配之後插入該block。
如果沒有匹配成功,那麼會在檔案末尾處插入。
insertbefore:在匹配行前插入。使用正則表示式,在指定正則表示式的最後一次匹配之前插入該block。
如果沒有匹配成功,那麼會在檔案末尾處插入。
marker:標記線模板。’{mark}’ 將被替換為 ‘in marker_begin’ 的值【預設:BEGIN】和 ‘marker_end’ 的值【預設:END】
marker_begin:開始的標記變數資訊。預設:BEGIN
marker_end:結尾的標記變數資訊。預設:END
owner:指定遠端檔案/目錄的屬主
group:指定遠端檔案/目錄的屬組
mode:指定遠端檔案/目錄的許可權
ini_file INI格式配置模組
在一個ini的檔案中管理(新增、刪除、更改)單獨的配置,而不必使用[template]或[assemble]來管理整個檔案。新增不存在的section。
1 # 檢視 yum 模組方法「可優先檢視 EXAMPLES 資訊的使用案例,知曉如何使用」 2 [yun@ansi-manager ansible_info]$ ansible-doc ini_file 3 # 由於我們使用的是 yun 普通使用者 4 # 因此有時需要使用 -b 選擇進行提權
新增與修改
1 # 第一次執行,如果檔案不存在,預設會建立 新增 section 和 option 資訊 2 [yun@ansi-manager ansible_info]$ ansible manageservers -b -m ini_file -a "path=/tmp/test.ini section=drinks option=username value=bobe mode='0644'" -i ./hosts_key 3 # 修改 option 資訊 4 [yun@ansi-manager ansible_info]$ ansible manageservers -b -m ini_file -a "path=/tmp/test.ini section=drinks option=username value=alice" -i ./hosts_key 5 # 新增另外一個section資訊 6 [yun@ansi-manager ansible_info]$ ansible manageservers -b -m ini_file -a "path=/tmp/test.ini section=base_info option=address value='BeiJing'" -i ./hosts_key 7 # 新增一個option資訊,還是放在 drinks 這個section下 8 [yun@ansi-manager ansible_info]$ ansible manageservers -b -m ini_file -a "path=/tmp/test.ini section=drinks option=password value=123456" -i ./hosts_key
刪除
1 # 刪除整個section, 刪除drinks這個section 2 [yun@ansi-manager ansible_info]$ ansible manageservers -b -m ini_file -a "path=/tmp/test.ini section=drinks state=absent" -i ./hosts_key 3 # 刪除指定section下的option 4 [yun@ansi-manager ansible_info]$ ansible manageservers -b -m ini_file -a "path=/tmp/test.ini section=base_info option=address state=absent" -i ./hosts_key
相關選項說明:
path:INI格式檔案路徑。如果沒有則預設會建立。
section:INI檔案中的 section 名。當設定單個值時,如果是 ‘state=present’ 則會自動新增這個值。如果留空或設定為 ‘null’,’option’ 將放在第一個 ‘section’ 之前。
如果配置格式不支援 section,也需要使用 “null”。
option:如果設定(需要更改的 value),這是選項的名稱。如果新增/刪除整個“section”,可以省略。預設:null
value:option 的值。刪除 “option” 時可以省略。
state:狀態說明
present:新增或修改,預設值
absent:刪除
backup:對原始檔備份。預設:False
create:如果不存在則建立檔案。預設:True
owner:指定遠端檔案的屬主
group:指定遠端檔案的屬組
mode:指定遠端檔案的許可權
no_extra_spaces:在 = 符號前後不插入空格。預設:False
replace 多行替換模組
對檔案所匹配的內容進行替換/刪除。
資料檔案準備
1 [yun@ansi-manager ~]$ cat /tmp/replace_test 2 172.16.1.181 test1.zhangblog.com 3 172.16.1.182 test2.zhangblog.org 4 172.16.1.183 test3.zhangblog.net 5 6 # httpd listen port 7 Listen 80 8 ServerRoot "/etc/httpd" 9 User apache 10 Group apache
1 # 檢視 yum 模組方法「可優先檢視 EXAMPLES 資訊的使用案例,知曉如何使用」 2 [yun@ansi-manager ansible_info]$ ansible-doc replace 3 # 由於我們使用的是 yun 普通使用者 4 # 因此有時需要使用 -b 選擇進行提權 5 # 將regexp正則匹配到的行,替換為replace的內容;且這裡使用了反向引用。 6 [yun@ansi-manager ansible_info]$ ansible manageservers -b -m replace -a "path=/tmp/replace_test regexp='(\s+)test\d+\.zhangblog\.(\w+)?$' replace='\1new.host.name.\2'" -i ./hosts_key 7 # 將 after 之後的每行都替換為 replace 中的內容 8 [yun@ansi-manager ansible_info]$ ansible manageservers -b -m replace -a "path=/tmp/replace_test after='# httpd listen port' regexp='^(.+)$' replace='after replace'" -i ./hosts_key 9 # 如果 after沒有匹配完一行,那麼 after匹配之後未匹配的該行內容也會替換為replace 中的內容 10 [yun@ansi-manager ansible_info]$ ansible manageservers -b -m replace -a "path=/tmp/replace_test after='# httpd listen' regexp='^(.+)$' replace='# after replace'" -i ./hosts_key 11 # 將 before 之前的每行都替換為 replace 中的內容 12 [yun@ansi-manager ansible_info]$ ansible manageservers -b -m replace -a "path=/tmp/replace_test before='listen port' regexp='^(.+)$' replace='before replace'" -i ./hosts_key 13 # 如果 before沒有匹配完一行,那麼 before匹配之前未匹配的該行內容也會替換為replace 中的內容 14 [yun@ansi-manager ansible_info]$ ansible manageservers -b -m replace -a "path=/tmp/replace_test before='listen port' regexp='^(.+)$' replace='before replace'" -i ./hosts_key 15 # 刪除匹配內容,行數不會改變。如果整行匹配,則最終為空行。 16 [yun@ansi-manager ansible_info]$ ansible manageservers -b -m replace -a "path=/tmp/replace_test regexp='zhangblog'" -i ./hosts_key 17 [yun@ansi-manager ansible_info]$ ansible manageservers -b -m replace -a "path=/tmp/replace_test regexp='.*test.*'" -i ./hosts_key
相關選項說明:
path:要操作的檔案路徑。
after:如果指定,只有after匹配之後的內容將被替換/刪除。可以與before組合使用。after可能匹配一行也可能匹配一行的部分;且不支援正則匹配。
before:如果指定,只有before匹配之前的內容將被替換/刪除。可以與after組合使用。before可能匹配一行也可能匹配一行的部分;不支援正則匹配。
regexp:要在檔案內容中查詢的正則表示式。
replace:替換regexp匹配項的字串。可能包含反向引用,如果regexp匹配,將使用regexp捕獲組展開這些反向引用。如果沒有設定,則完全刪除匹配項。
反向引用可以像 ‘\1’ 那樣含糊地使用,也可以像 ‘\g<1>’ 那樣顯式地使用。
backup:對原始檔備份。預設:False
encoding:用於讀寫檔案的字元編碼。預設:utf-8
owner:指定遠端檔案的屬主
group:指定遠端檔案的屬組
mode:指定遠端檔案的許可權
sysctl 修改核心引數模組
這個模組操作 sysctl 條目,並在更改它們之後可選地執行 /sbin/sysctl -p 。
1 # 檢視 yum 模組方法「可優先檢視 EXAMPLES 資訊的使用案例,知曉如何使用」 2 [yun@ansi-manager ansible_info]$ ansible-doc sysctl 3 # 由於我們使用的是 yun 普通使用者 4 # 因此有時需要使用 -b 選擇進行提權 5 # 會修改 /etc/sysctl.conf 檔案並執行 /sbin/sysctl -p 使其生效 6 [yun@ansi-manager ansible_info]$ ansible manageservers -b -m sysctl -a 'name=net.ipv4.ip_forward value=1' -i ./hosts_key 7 [yun@ansi-manager ansible_info]$ ansible manageservers -b -m sysctl -a 'name=vm.swappiness value=5' -i ./hosts_key
相關選項說明:
name:核心引數變數名。(別名:key)
value:sysctl鍵的期望值。(別名:val)
reload:當為 yes 時,如果 sysctl_file 檔案被修改,那麼會執行 /sbin/sysctl -p,使修改的引數生效。當為 no 時,則不過載 sysctl ,儘管 sysctl_file 已被修改。
state:狀態說明
present:新增或修改,預設值
absent:刪除
sysctl_file:指定 sysctl.conf 檔案的絕對路徑。預設:/etc/sysctl.conf
sysctl_set:使用sysctl命令驗證令牌值,必要時使用 -w 進行設定。預設:no
ignoreerrors:使用此選項可忽略關於未知鍵的錯誤。預設:no
get_url 檔案下載模組
通過 HTTP,HTTPS或 FTP 下載一個檔案。
1 # 檢視 yum 模組方法「可優先檢視 EXAMPLES 資訊的使用案例,知曉如何使用」 2 [yun@ansi-manager ansible_info]$ ansible-doc get_url 3 ## 下載一個檔案 4 [yun@ansi-manager ansible_info]$ ansible proxyservers -m get_url -a "url='http://www.zhangblog.com/uploads/jvm/jvm-01-01.png' dest=/tmp/ mode=0640" -i ./hosts_key 5 ## 下載前會比對校驗和,如果不匹配則不下載 6 [yun@ansi-manager ansible_info]$ ansible proxyservers -m get_url -a "url='http://www.zhangblog.com/uploads/jvm/jvm-01-01.png' dest=/tmp/jvm-001.png checksum='md5:9af3f6066ea46ea81c0b3c9d719dbce0'" -i ./hosts_key
相關選項說明:
mode:指定目標檔案的許可權
url:指定檔案來源「支援 HTTP,HTTPS 和 FTP」
dest:指定目標存放的目錄或檔案
checksum:校驗和「支援 sha256 和 md5」
timeout:請求超時時間,預設 10 秒。
owner:指定遠端檔案的屬主
group:指定遠端檔案的屬組
mode:指定遠端檔案的許可權
backup:原始檔備份
service、systemd 服務管理模組
如果在 CentOS 6 及以下版本,優先使用 service 。
如果在 CentOS 7 及以上版本,優先使用 systemd 。
這裡考慮到我們使用的 CentOS 7,因此使用的是 systemd 。至於 service 請自行檢視文件。
1 # 檢視 yum 模組方法「可優先檢視 EXAMPLES 資訊的使用案例,知曉如何使用」 2 [yun@ansi-manager ansible_info]$ ansible-doc service 3 [yun@ansi-manager ansible_info]$ ansible-doc systemd 4 # 由於我們使用的是 yun 普通使用者 5 # 因此有時需要使用 -b 選擇進行提權 6 ## 啟動 httpd 服務,並且加入開機自啟動 7 [yun@ansi-manager ansible_info]$ ansible proxyservers -b -m systemd -a "name=httpd state=started enabled=yes" -i ./hosts_key 8 ## 重啟 httpd 服務,並且重新載入 /usr/lib/systemd/system/httpd.service 服務配置檔案,且加入開機自啟動 9 [yun@ansi-manager ansible_info]$ ansible proxyservers -b -m systemd -a "name=httpd state=restarted daemon_reload=yes enabled=yes" -i ./hosts_key 10 ## 停止 httpd 服務,並且不加入開機自啟動 11 [yun@ansi-manager ansible_info]$ ansible proxyservers -b -m systemd -a "name=httpd state=stopped enabled=no" -i ./hosts_key
相關選項說明:
name:服務名稱
state:服務狀態
started:啟動服務
stopped:停止服務
reloaded:重載入服務
restarted:重啟動服務
enabled:是否加入開機自啟動「yes 加入, no 不加入, 預設 null」
daemon_reload:當我們修改了服務管理配置檔案,是否重載入其配置「yes 重載入服務配置檔案, no 不載入,預設值」
group 組模組
建立或刪除使用者組
1 # 檢視 yum 模組方法「可優先檢視 EXAMPLES 資訊的使用案例,知曉如何使用」 2 [yun@ansi-manager ansible_info]$ ansible-doc group 3 # 由於我們使用的是 yun 普通使用者 4 # 因此有時需要使用 -b 選擇進行提權 5 ## 建立 test 組,並指定組ID 6 [yun@ansi-manager ansible_info]$ ansible proxyservers -b -m group -a " name=test gid=9001" -i ./hosts_key 7 ## 建立 testsystem 組,並指定為系統組 8 [yun@ansi-manager ansible_info]$ ansible proxyservers -b -m group -a "name=testsystem system=true" -i ./hosts_key 9 ## 刪除 testsystem 組 10 [yun@ansi-manager ansible_info]$ ansible proxyservers -b -m group -a "name=testsystem state=absent" -i ./hosts_key
相關選項說明:
gid:指定組ID,預設 null
name:指定組名稱
state:組狀態
present:建立組,預設
absent:刪除組
system:是否為系統組
true:是系統組
false:不是系統組
user 使用者模組
建立或刪除使用者
1 # 檢視 yum 模組方法「可優先檢視 EXAMPLES 資訊的使用案例,知曉如何使用」 2 [yun@ansi-manager ansible_info]$ ansible-doc user 3 # 由於我們使用的是 yun 普通使用者 4 # 因此有時需要使用 -b 選擇進行提權
1、建立使用者,指定 UID,指定附加組,不建立家目錄,不可以登入
[yun@ansi-manager ansible_info]$ ansible proxyservers -b -m user -a "name=zhangtest uid=1005 groups=zhang,yun create_home=no shell=/sbin/nologin" -i ./hosts_key
受控機檢視資訊
1 [yun@ansi-haproxy02 ~]$ id zhangtest 2 uid=1005(zhangtest) gid=1005(zhangtest) groups=1005(zhangtest),1000(zhang),1050(yun) 3 [yun@ansi-haproxy02 ~]$ tail -n1 /etc/passwd 4 zhangtest:x:1005:1005::/home/zhangtest:/sbin/nologin # /home/zhangtest 該目錄不存在
2、刪除使用者,但不刪除使用者的家目錄
[yun@ansi-manager ansible_info]$ ansible proxyservers -b -m user -a "name=zhangtest state=absent" -i ./hosts_key
3、為使用者建立密碼或SSH key 祕鑰
[yun@ansi-manager ansible_info]$ ansible proxyservers -b -m user -a "name=zhangtest2 generate_ssh_key=yes ssh_key_bits=2048 ssh_key_file=.ssh/id_rsa" -i ./hosts_key
4、為使用者建立密碼
1 # 得到密碼串 2 [yun@ansi-manager ansible_info]$ ansible localhost -m debug -a "msg={{ '123456' | password_hash('sha512', 'salt') }}" 3 localhost | SUCCESS => { 4 "msg": "$6$salt$MktMKPZJ6t59GfxcJU20DwcwQzfMvOlHFVZiOVD71w.igcOo1R7vBYR65JquIQ/7siC7VRpmteKvZmfSkNc69." 5 } 6 ## 注意 -a '' 是單引號,而不是雙引號。如果使用雙引號,特殊字元會被解析 7 [yun@ansi-manager ansible_info]$ ansible proxyservers -b -m user -a 'name=zhangtest2 password=$6$salt$MktMKPZJ6t59GfxcJU20DwcwQzfMvOlHFVZiOVD71w.igcOo1R7vBYR65JquIQ/7siC7VRpmteKvZmfSkNc69.' -i ./hosts_key
相關選項說明:
name:使用者名稱稱
state:使用者狀態
present:預設值,建立使用者
absent:刪除使用者
password:使用者密碼,預設:null
shell:設定使用者的shell
uid:指定使用者 UID
group:設定使用者主組,預設:null
groups:設定使用者附加組,預設:null
system:如果為 yes 則建立系統使用者,預設:false
home:設定使用者家目錄
create_home:是否建立家目錄,預設:true,如果不建立為:no
comment:使用者描述,預設:null
expires:使用者賬號失效日期,預設:null
update_password:更新密碼
always:如果密碼不同,則更新,預設值
on_create:只有新建使用者時使用
remove:只有在 state=absent 時生效,作用:刪除使用者家目錄
generate_ssh_key:是否生成SSH key 金鑰對,預設:false
ssh_key_bits:設定 SSH key 位元組長度,預設:有 ssh-keygen 設定
ssh_key_comment:設定 SSH key 的描述,預設為:$HOSTNAME
ssh_key_file:指定 SSH key 的檔名,預設:null 【即為:.ssh/id_rsa】
ssh_key_passphrase:指定 SSH key 密碼,如果不提供則沒有密碼,預設:null
ssh_key_type:設定 SSH key 型別,預設:rsa
cron 定時任務模組
建立、註釋或刪除定時任務
1 # 檢視 yum 模組方法「可優先檢視 EXAMPLES 資訊的使用案例,知曉如何使用」 2 [yun@ansi-manager ansible_info]$ ansible-doc cron 3 # 由於我們使用的是 yun 普通使用者 4 # 因此有時需要使用 -b 選擇進行提權
1 ## 建立定時任務, name 為定時任務說明 2 [yun@ansi-manager ansible_info]$ ansible proxyservers -b -m cron -a 'name="crond test" minute=0 hour="2,5" job="ls /tmp >/dev/null 2>&1"' -i ./hosts_key 3 ## 註釋指定定時任務,其中 name ,時間引數,和 job 都需要。 4 [yun@ansi-manager ansible_info]$ ansible proxyservers -b -m cron -a 'name="crond test" minute=0 hour="2,5" job="ls /tmp >/dev/null 2>&1" disabled=true' -i ./hosts_key 5 ## 刪除定時任務 6 [yun@ansi-manager ansible_info]$ ansible proxyservers -b -m cron -a 'name="crond test" state=absent' -i ./hosts_key
相關選項說明:
name:定時任務描述
state:狀態
present:新增,預設值
absent:刪除
user:指定哪個使用者的定時任務會修改,預設是 root 使用者,預設:null
minute:哪個分鐘執行 ( 0-59, *, */2, etc ),預設:*
hour:哪個小時執行 ( 0-23, *, */2, etc ),預設 *
day:每月的哪天執行 ( 1-31, *, */2, 等 ),預設:*
month:哪個月執行 ( 1-12, *, */2, etc ),預設:*
weekday:每週的哪天執行 ( 0-6 為 Sunday-Saturday, *, etc ),預設:*
disabled:是否註釋指定定時任務,預設:false
job:定時任務要操作的具體資訊
mount 檔案系統掛載模組
檔案系統的掛載與取消。
1 # 檢視 yum 模組方法「可優先檢視 EXAMPLES 資訊的使用案例,知曉如何使用」 2 [yun@ansi-manager ansible_info]$ ansible-doc mount 3 # 由於我們使用的是 yun 普通使用者 4 # 因此有時需要使用 -b 選擇進行提權
案例示例:
在 ansi-manager 機器作為 NFS 的服務端,ansi-haproxy01、ansi-haproxy02 作為 NFS 的客戶端。
1、在所有機器安裝 NFS 必要的包
[yun@ansi-manager ansible_info]$ ansible manageservers,proxyservers -b -m yum -a "name=nfs-utils,rpcbind state=present" -i ./hosts_key
2、NFS 服務端配置與啟動
1 [yun@ansi-manager ansible_info]$ ansible manageservers -b -m copy -a 'content="/data 172.16.1.0/24(rw,sync,root_squash,all_squash)\n" dest=/etc/exports' -i ./hosts_key 2 # 建立 /data 目錄,屬主、屬組為 nfsnobody 3 [yun@ansi-manager ansible_info]$ ansible manageservers -b -m file -a "path=/data owner=nfsnobody group=nfsnobody state=directory" -i ./hosts_key 4 [yun@ansi-manager ansible_info]$ ansible manageservers -b -m systemd -a "name=rpcbind state=started enabled=yes" -i ./hosts_key 5 [yun@ansi-manager ansible_info]$ ansible manageservers -b -m systemd -a "name=nfs state=started enabled=yes" -i ./hosts_key 6 ## 檢視 Export list 資訊 7 [yun@ansi-manager ansible_info]$ ansible manageservers -b -m shell -a "showmount -e" -i ./hosts_key
3、NFS 客戶端操作
1 [yun@ansi-manager ansible_info]$ ansible proxyservers -b -m systemd -a "name=rpcbind state=started enabled=yes" -i ./hosts_key 2 ## 檢查共享資訊 3 [yun@ansi-manager ansible_info]$ ansible proxyservers -b -m shell -a "showmount -e 172.16.1.180" -i ./hosts_key
4、NFS 客戶端掛載
1 ## 不掛載裝置,僅在 /etc/fstab 中寫入掛載配置資訊 2 [yun@ansi-manager ansible_info]$ ansible proxyservers -b -m mount -a "src=172.16.1.180:/data path=/mnt fstype=nfs state=present backup=yes" -i ./hosts_key 3 ## 掛載裝置,並在 /etc/fstab 中寫入掛載配置資訊 4 [yun@ansi-manager ansible_info]$ ansible proxyservers -b -m mount -a "src=172.16.1.180:/data path=/mnt fstype=nfs state=mounted" -i ./hosts_key 5 ## 不解除安裝裝置,僅在 /etc/fstab 中刪除掛載配置資訊 6 [yun@ansi-manager ansible_info]$ ansible proxyservers -b -m mount -a "src=172.16.1.180:/data path=/mnt fstype=nfs state=unmounted" -i ./hosts_key 7 ## 解除安裝裝置,並在 /etc/fstab 中刪除掛載配置資訊 8 [yun@ansi-manager ansible_info]$ ansible proxyservers -b -m mount -a "src=172.16.1.180:/data path=/mnt fstype=nfs state=absent" -i ./hosts_key
相關選項說明:
src:要掛在的裝置/磁碟
path:掛載點
opts:掛載引數選項,如:ro,noauto;預設:null
fstype:檔案系統型別
state:狀態
present:不掛載裝置,僅在 /etc/fstab 中寫入掛載配置資訊
mounted:掛載裝置,並在 /etc/fstab 中寫入掛載配置資訊
unmounted:不解除安裝裝置,僅在 /etc/fstab 中刪除掛載配置資訊
absent:解除安裝裝置,並在 /etc/fstab 中刪除掛載配置資訊
backup:對之前的檔案備份
debug 除錯模組與 register 變數
這裡會使用 playbook 書寫。具體的 playbook 詳解,參見後面的文章。
涉及到 playbook、變數和 Jinja ,這些在後面的文章會有詳解。
1 # 檢視 yum 模組方法「可優先檢視 EXAMPLES 資訊的使用案例,知曉如何使用」 2 [yun@ansi-manager ansible_info]$ ansible-doc debug 3 # 由於我們使用的是 yun 普通使用者 4 # 因此有時需要使用 -b 選擇進行提權 5 # Ad-Hoc 方式 6 [yun@ansi-manager ansible_info]$ ansible proxyservers -b -m debug -i ./hosts_key 7 [yun@ansi-manager ansible_info]$ ansible proxyservers -b -m debug -a 'msg="print customized message"' -i ./hosts_key 8 ################################################## 9 # Playbooks 方式 10 [yun@ansi-manager ansible_info]$ pwd 11 /app/ansible_info 12 [yun@ansi-manager ansible_info]$ ll 13 total 24 14 -rw-rw-r-- 1 yun yun 483 Aug 18 09:12 hosts_key 15 -rw-rw-r-- 1 yun yun 245 Aug 18 21:55 test_debug_register.yml 16 [yun@ansi-manager ansible_info]$ cat test_debug_register.yml 17 --- 18 # 如何使用 debug 模組與 register 變數 19 - hosts: proxyservers 20 21 tasks: 22 - name: "get host port info" 23 shell: netstat -lntp 24 register: host_port 25 26 - name: "print host port" 27 debug: 28 #msg: "{{ host_port }}" # 輸出全部資訊 29 #msg: "{{ host_port.cmd }}" # 引用方式一 30 msg: "{{ host_port['stdout_lines'] }}" # 引用方式二 31 32 [yun@ansi-manager ansible_info]$ ansible-playbook -b -i ./hosts_key test_debug_register.yml
第一個 task 中,使用了 register 註冊變數,名為 host_port ;當 shell 模組執行完畢後,會將資料放到該變數中。
第二給 task 中,使用了 debug 模組,並從 host_port 中獲取資料。
assert 斷言模組【瞭解】
對自定義訊息斷言。
涉及到 playbook、變數和 Jinja ,這些在後面的文章會有詳解。
1 # 檢視 yum 模組方法「可優先檢視 EXAMPLES 資訊的使用案例,知曉如何使用」 2 [yun@ansi-manager ansible_info]$ ansible-doc assert 3 # 由於我們使用的是 yun 普通使用者 4 # 因此有時需要使用 -b 選擇進行提權 5 # Ad-Hoc 方式 6 [yun@ansi-manager ansible_info]$ ansible proxyservers -m assert -a "that='2 == 3' success_msg=ok fail_msg=fail" -i ./hosts_key # 斷言失敗 7 [yun@ansi-manager ansible_info]$ ansible proxyservers -m assert -a "that='2 <= 3' success_msg=ok fail_msg=fail" -i ./hosts_key # 斷言成功 8 [yun@ansi-manager ansible_info]$ ansible proxyservers -m assert -a "that='2 < 3' success_msg=ok fail_msg=fail" -i ./hosts_key # 斷言成功 9 [yun@ansi-manager ansible_info]$ ansible proxyservers -m assert -a "that='2 < 3 and 4 == 4' success_msg=ok fail_msg=fail quiet=yes" -i ./hosts_key # 斷言成功 10 [yun@ansi-manager ansible_info]$ ansible proxyservers -m assert -a "that='2 < 3 and 4 > 4' success_msg=ok fail_msg=fail quiet=yes" -i ./hosts_key # 斷言失敗 11 [yun@ansi-manager ansible_info]$ ansible proxyservers -m assert -a "that='3 < 3 or 4 == 4' success_msg=ok fail_msg=fail quiet=yes" -i ./hosts_key # 斷言成功 12 ################################################## 13 # Playbooks 方式 14 [yun@ansi-manager ansible_info]$ pwd 15 /app/ansible_info 16 [yun@ansi-manager ansible_info]$ ll 17 total 12 18 -rw-rw-r-- 1 yun yun 226 Oct 8 16:07 hosts_key 19 -rw-rw-r-- 1 yun yun 902 Oct 11 10:57 test_assert.yml 20 [yun@ansi-manager ansible_info]$ cat test_assert.yml # playbook 資訊 21 --- 22 # assert 示例 23 - hosts: proxyservers 24 # 使用如下變數,分別測試下 25 vars: 26 #- my_param: 20 27 #- my_param: -2 28 - my_param: 200 29 30 tasks: 31 - name: "assert example 1" 32 assert: 33 # ansible_os_family 為 facts 中的變數資訊 34 that: 35 - ansible_os_family == "RedHat" 36 success_msg: "success info" 37 fail_msg: "fail info" 38 39 - name: "assert example 2" 40 assert: 41 # that 下面的列表,為 && 關係 42 that: 43 - my_param >= 0 44 - my_param <= 100 45 fail_msg: "'my_param' must be between 0 and 100" 46 success_msg: "success info" 47 # 是否忽略該 task 的錯誤 48 ignore_errors: True 49 50 - name: "assert example 3" 51 assert: 52 that: 53 - my_param <= 10 or my_param >= 100 54 fail_msg: "'my_param' must be <= 10 or >= 100" 55 success_msg: "success info" 56 57 [yun@ansi-manager ansible_info]$ ansible-playbook -b -i ./hosts_key test_assert.yml # 執行
相關選項說明:
that:列表字串表示式
success_msg:當斷言成功時輸出的資訊
fail_msg:別名 msg,當斷言失敗時輸出的資訊
quiet:預設 False,設定為 yes 避免冗長輸出
selinux 安全模組
配置 SELinux 。
1 # 檢視 yum 模組方法「可優先檢視 EXAMPLES 資訊的使用案例,知曉如何使用」 2 [yun@ansi-manager ansible_info]$ ansible-doc selinux 3 # 由於我們使用的是 yun 普通使用者 4 # 因此有時需要使用 -b 選擇進行提權 5 ## 關閉 selinux 6 [yun@ansi-manager ansible_info]$ ansible proxyservers -b -m selinux -a "state=disabled" -i ./hosts_key
相關選項說明:
state:狀態
disabled:不可用
enforcing:強制執行
permissive:會提醒
———END———
如果覺得不錯就關注下唄 (-^O^-) !
&n