模塊
ansible 中的模塊可以用在ansible命令行或后面要講的playbook中。不同的模塊提供不同的功能标捺,官方提供的非常多迈螟,幾千種,常用的有幾十種,這里只介紹常見的幾種模塊。
模塊是Ansible基本的可復用的單元。模塊的功能范圍很小阔拳,可能只針對某操作系統(tǒng)。
模塊的冪等性
ansible絕大多數模塊都天然具有 冪等 特性楞件,只有極少數模塊如shell
和command
模塊不具備冪等性衫生。所謂的冪等性是指多次執(zhí)行同一個操作不會影響最終結果。例如土浸,ansible的yum模塊安裝rpm包時罪针,如果待安裝的包已經安裝過了,則再次或多次執(zhí)行安裝操作都不會真正的執(zhí)行下去黄伊。再例如泪酱,copy模塊拷貝文件時,如果目標主機上已經有了完全相同的文件还最,則多次執(zhí)行copy模塊不會真正的拷貝墓阀。ansible具有冪等性的模塊在執(zhí)行時,都會自動判斷是否要執(zhí)行拓轻。
自己編寫的腳本有可能執(zhí)行第二次的時候有可能帶來不一樣的意外或影響斯撮,而模塊的冪等性可以降低一定的風險。
ansible-doc 命令
學習ansible模塊時扶叉,可以先用ansible-doc命令勿锅,閱讀相關模塊的說明文檔
比如我想通過ansible執(zhí)行拷貝文件操作,先用ansible-doc -l | grep 'copy'
過濾出所有包含copy的模塊名枣氧。
image.png
ansible-doc copy
查看copy模塊的使用詳情
image.png
ansible-doc -s copy
查看copy模塊的精簡信息
shell 和 command
查看某服務器的內存使用情況
ansible myserver -m command -a "free -m"
可簡寫, 因為 -m command 是默認
ansible myserver -a "free -m"
模塊包括 command, script(在遠程主機執(zhí)行主控端的shell腳本), shell (執(zhí)行遠程主機的shell腳本文件)
例子
ansible myserver -m command -a "free -m"
ansible myserver -m script -a "/home/local.sh"
ansible myserver -m shell -a "/home/server.sh"
實際上shell模塊執(zhí)行命令的方式是在遠程使用/bin/sh來執(zhí)行的
在批量服務器上完成同一操作
ansible merch -m shell -a "touch demo.txt"
查看 shell 模塊提供的參數
ansible-doc -s shell
- name: Execute commands in nodes.
shell:
chdir: # cd into this directory before running the command
# 執(zhí)行命令前溢十,先cd到指定目錄
creates: # a filename, when it already exists, this step will *not* be run.
# 用于判斷命令是否要執(zhí)行。如果指定的文件(可以使用通配符)存在达吞,則不執(zhí)行张弛。
executable: # change the shell used to execute the command. Should be an absolute path to the executable.
# 不再使用默認的/bin/sh解析并執(zhí)行命令,而是使用此處指定的命令解析。例如使用expect解析expect腳本吞鸭。必須為絕對路徑寺董。
free_form: # (required) The shell module takes a free form command to run, as a string. There's not an actual option
named "free form". See the examples!
removes: # a filename, when it does not exist, this step will *not* be run.
# 用于判斷命令是否要執(zhí)行。如果指定的文件(可以使用通配符)不存在瞒大,則不執(zhí)行螃征。
stdin: # Set the stdin of the command directly to the specified value.
warn: # if command warnings are on in ansible.cfg, do not warn about this particular line if set to no/false.
例如:
tasks:
- shell: touch helloworld.txt creates=/tmp/hello.txt
但建議搪桂,在參數可能產生歧義的情況下透敌,使用args來傳遞ansible的參數。如:
- shell: touch helloworld.txt
args:
creates: /tmp/hello.txt
COPY 復制模塊
實現主控端向目標主機拷貝文件踢械,類似于scp的功能酗电。
拷貝當前目錄的 demo.png 到遠程服務器的/home/ubuntu目錄下,并修改文件權限
ansible cloud -m copy -a "src=demo.png dest=/home/ubuntu mode=755"
template 模塊
template模塊用法和copy模塊用法基本一致内列,它主要用于復制配置文件撵术。
ansible-doc -s template
- name: Templates a file out to a remote server.
action: template
dest # 必填,拷貝到遠程機器的目標路徑
src # 必填话瞧,Ansible控制機模板文件所在位置
force # 是否覆蓋同名文件
group # 設置遠程文件的所屬組
owner # 設置遠程文件的所有者
mode # 設置遠程文件權限嫩与,如 0644,'u+rw', 'u=rw,g=r,o=r' 等方式
backup # 拷貝的同時也創(chuàng)建一個包含時間戳信息的備份文件交排,默認為no
類似的模塊
file # 文件處理模塊划滋,可以遞歸創(chuàng)建目錄
fetch # 拉取文件模塊,從遠程主機將文件拉取到本地端
rsync # 實現rsync部分功能的模塊
debug 模塊
用于輸出自定義的信息埃篓,類似于echo处坪、print等輸出命令。ansible中的debug主要用于輸出變量值架专、表達式值同窘,以及用于when條件判斷時。使用方式非常簡單部脚。
ansible-doc -s debug
- name: Print statements during execution
debug:
msg: # The customized message that is printed. If omitted, prints a generic message.
# 輸出自定義信息想邦。如果省略,則輸出普通字符委刘。
var: # A variable name to debug. Mutually exclusive with the 'msg' option.
# 指定待調試的變量丧没。只能指定變量,不能指定自定義信息钱雷,且變量不能加{{}}包圍骂铁,而是直接的變量名。
verbosity: # A number that controls when the debug is run, if you set to 3 it will only run debug when -vvv or above
# 控制debug運行的調試級別罩抗,有效值為一個數值N拉庵。
script 模塊
script模塊用于控制遠程主機執(zhí)行腳本。在執(zhí)行腳本前套蒂,ansible會將本地腳本傳輸到遠程主機钞支,然后再執(zhí)行茫蛹。在執(zhí)行腳本的時候,其采用的是遠程主機上的shell環(huán)境烁挟。
例如婴洼,將ansible端/tmp/a.sh發(fā)送到各被控節(jié)點上執(zhí)行,但如果被控節(jié)點的/tmp下有hello.t xt 撼嗓,則不執(zhí)行柬采。
---
- hosts: centos
remote_user: root
tasks:
- name: execute /tmp/a.sh,but only /tmp/hello.txt is not yet created
script: /tmp/a.sh hello
args:
creates: /tmp/hello.txt
參考
模塊非常多,有什么需求先去官網查且警,然后看文檔
官方模塊說明