1. 遠(yuǎn)端執(zhí)行模塊:command
vs shell
-
相同點(diǎn):
- 功能相似肚邢,都是在遠(yuǎn)端運(yùn)行shell命令
- 如果要在 Windows 環(huán)境運(yùn)行裁赠,需要使用對(duì)應(yīng)
win_command
和win_shell
模塊
-
不同點(diǎn):
- shell 將命令打包玻蝌,通過(guò)
/bin/sh
的遠(yuǎn)程模式運(yùn)行 - command 解析命令參數(shù)既棺,然后在遠(yuǎn)端執(zhí)行康二,因此無(wú)法使用 管道("|") 定向符 (">" "<") 以及 ";" 和 "&"
- shell 將命令打包玻蝌,通過(guò)
用例:使用
command
和shell
執(zhí)行同一語(yǔ)句cat /home/root/testfile
#!/usr/local/bin/ansible-playbook
---
- hosts: all
tasks:
- name: 1.1 command
command: cat /home/root/testfile
register: cmd_out
- name: 1.2 see command output
debug: var=cmd_out
- name: 2.1 shell
shell: cat /home/root/testfile
register: shell_out
- name: 2.2 see shell output
debug: var=shell_out
觀察輸出:
# command 輸出
{
'stderr_lines': [],
u'changed': True,
u'end': u'2018-12-24 00:06:51.393299',
'failed': False,
u'stdout': u'feng baobao is like immortal',
# ========== ↓ Attention ↓ ==========
u'cmd': [u'cat', u'/home/root/testfile'],
# ========== ↑ Attention ↑ ==========
u'rc': 0,
u'start':
u'2018-12-24 00:06:51.283211', u'stderr':
u'',
u'delta':
u'0:00:00.110088', 'stdout_lines': [u'feng baobao is like immortal']
}
# shell 輸出
{
'stderr_lines': [],
u'changed': True,
u'end': u'2018-12-24 00:06:58.119163',
'failed': False,
u'stdout': u'feng baobao is like immortal',
# ========== ↓ Attention ↓ ==========
u'cmd': u'cat /home/root/testfile',
# ========== ↑ Attention ↑ ==========
u'rc': 0,
u'start': u'2018-12-24 00:06:58.007352',
u'stderr': u'',
u'delta': u'0:00:00.111811', 'stdout_lines': [u'feng baobao is like immortal']}"
幾無(wú)區(qū)別,除了 cmd
項(xiàng)中,command
輸出是一個(gè)list弄企,每個(gè)元素為命令的split超燃;而shell
輸出是將命令作為一個(gè)string傳遞給遠(yuǎn)端shell
- 思考: 由此看來(lái),
shell
理當(dāng)比command
模塊更強(qiáng)大拘领,那么command
模塊單獨(dú)存在的意義是什么意乓? - 還有“同類”:
script
&raw
-
script
: 將本地腳本傳遞到遠(yuǎn)端,然后在遠(yuǎn)端機(jī)器執(zhí)行該腳本 -
raw
:- 類似
command
但是可以使用管道(網(wǎng)上的說(shuō)法约素,但貌似不全面) - 官方解釋届良,該模塊可以運(yùn)行 "low-down and dirty SSH command",并不需要依賴python和ansible模塊功能
- 限制: 建議只在少數(shù)情況下使用圣猎,如對(duì)遠(yuǎn)端進(jìn)行安裝python操作
- 類似
- 此二者不需要遠(yuǎn)端安裝 python
-
References:
2. 文件操作模塊
模塊 | 功能 |
---|---|
file | 對(duì) 文件/文件夾/鏈接 進(jìn)行 創(chuàng)建/刪除/修改權(quán)限 操作 |
copy | 將 本地或遠(yuǎn)端文件 拷貝到 遠(yuǎn)端路徑 |
template | copy 升級(jí)版士葫,可以對(duì)按 jinja2 規(guī)則的模板文件進(jìn)行構(gòu)建,并拷貝到遠(yuǎn)端目錄中 |
assemble | 將一個(gè)路徑中的所有文件按照字符串順序聚合起來(lái) |
file 模塊示例:
tasks:
- name: 創(chuàng)建
file:
path: /home/root/testfile
state: touch # 創(chuàng)建文件需要用 touch, 創(chuàng)建文件夾用 directory, 刪除用 absent
- name: 鏈接
file:
src: /home/root/filetolink
dest: /home/root/testlink
state: link
- name: 修改權(quán)限屬性等
file:
path: /home/root/testfile
owner: foo
group: foo
mode: 0644
copy 模塊示例:
tasks:
- name: 將本地 /srv/myfiles/foo.conf 拷貝到遠(yuǎn)端 /etc/foo.conf
copy:
src: /srv/myfiles/foo.conf
dest: /etc/foo.conf
owner: foo
group: foo
mode: 0644
- name: 將遠(yuǎn)端 /srv/myfiles/foo.conf 拷貝到遠(yuǎn)端 /etc/foo.conf
copy:
src: /srv/myfiles/foo.conf
dest: /etc/foo.conf
owner: foo
group: foo
remote_src: yes
mode: 0644
References:
文件拉取 fetch
vs slurp
和 copy
送悔、template
將本地文件傳送到遠(yuǎn)端不同慢显, fetch
命令從遠(yuǎn)端將文件拉取到本地。
而 slurp
模塊用于拉取遠(yuǎn)端文件的 base64 碼欠啤。
fetch
示例:
# Store file into /tmp/fetched/host.example.com/tmp/somefile
- fetch:
src: /tmp/somefile
dest: /tmp/fetched
# Specifying a destination path
- fetch:
src: /tmp/uniquefile
dest: /tmp/special/
flat: yes # 不覆蓋已有文件
References:
4. How to EXIT playbook: fail
& meta: end_play
fail
模塊
可用于標(biāo)記任務(wù)失敗荚藻,僅影響當(dāng)前 inventory_hostname, 其他節(jié)點(diǎn)仍可繼續(xù)進(jìn)行后續(xù)步驟。
meta
模塊
該模塊可執(zhí)行一些特殊命令洁段,這些命令能影響playbook的內(nèi)部執(zhí)行或執(zhí)行狀態(tài):
- flush_handler:刷新 handler
- refresh_inventory:某些動(dòng)態(tài)加載 host 需要在此刷新
- noop:無(wú)用
- clear_facts:清除當(dāng)前 inventory 收集的facts
- clear_host_errors:清除 inventory 的 fail 狀態(tài)
- end_play:結(jié)束整個(gè) playbook
- reset_connection:重新設(shè)置鏈接 (從官方用例來(lái)看应狱,可以更改遠(yuǎn)程登錄的用戶)
尷尬的是,目前沒(méi)發(fā)現(xiàn)能夠僅影響1臺(tái)主機(jī)令其結(jié)束任務(wù)祠丝,其他主機(jī)依舊進(jìn)行任務(wù)的方式疾呻。或許可以嘗試 meta
模塊的 refresh_inventory
5. 配置文件相關(guān) ini_file
vs with_ini
ini_file
模塊
用于管理遠(yuǎn)端配置文件写半“段希可以 增、刪叠蝇、改
遠(yuǎn)端配置文件的配置項(xiàng)散吵。
遠(yuǎn)端配置文件需滿足一定格式,如
[section1]
option1=value1
option2=value2
[section2]
option1=value2
option2=value1
section 不可重復(fù)蟆肆;同一 section 下的 option 亦不可重復(fù)。
模塊使用方式:
- name: 修改遠(yuǎn)端 /etc/conf 文件晦款,使其 [drinks] 下的 fav 值為 lemonade (如果不存在option或section炎功,則加上這個(gè)配置項(xiàng)),且權(quán)限模式為 0600缓溅,并備份原始文件
ini_file:
path: /etc/conf
section: drinks
option: fav
value: lemonade
mode: 0600
backup: yes
- name: 刪除遠(yuǎn)端 /etc/anotherconf 文件中 [drinks] 下的 temperature 配置項(xiàng)
ini_file:
path: /etc/anotherconf
section: drinks
option: temperature
state: absent
with_ini
其實(shí)是 lookup
的子模塊蛇损,用于讀取本地的文件。 with_ini
可以讀取本地配置文件。示例如下:
- debug: msg="User in integration is {{ lookup('ini', 'user section=integration file=users.ini') }}"
- debug: msg="User in production is {{ lookup('ini', 'user section=production file=users.ini') }}"
- debug: msg="user.name is {{ lookup('ini', 'user.name type=properties file=user.properties') }}"
- debug:
msg: "{{ item }}"
with_ini:
- value[1-2]
- section: section1
- file: "lookup.ini"
- re: true
目前沒(méi)發(fā)現(xiàn)讀取遠(yuǎn)端配置文件的模塊淤齐。解決方法都是 fetch
目標(biāo)配置文件到本地來(lái)解析股囊。
References: