模塊的使用
查看模塊幫助
ansible-doc -l
查看所有模塊
ansible-doc -s MODULE_NAME
查看指定模塊的詳細幫助
ansible命令應用基礎
語法: ansible <host-pattern> [-f forks] [-m module_name] [-a args]
-f forks:啟動的並發線程數
-m module_name: 要使用的模塊
-a args: 模塊特有的參數
常用模塊
command
功能:命令模塊,默認模塊,用於在遠程主機執行命令,缺點:運行的命令中無法使用變量,管道。如果需要使用管道、變量,請使用raw模塊,或者shell模塊。
[root@node1 ~]# ansible-doc -s command
- name: 在遠程節點執行命令
action: command
chdir # 在執行命令之前,先切換到該目錄
creates # 一個文件名,當這個文件存在,則該命令不執行,可以用來做判斷
executable # 切換shell來執行命令,需要使用命令的絕對路徑
free_form= #要執行的Linux指令,一般使用Ansible的-a參數代替。
removes #一個文件名,這個文件不存在,則該命令不執行,與creates相反的判斷
cron
定時任務模塊
[root@node1 ~]# ansible-doc -s cron
- name: 設置管理節點生成定時任務
action: cron
backup # 如果設置,創建一個crontab備份
cron_file #如果指定, 使用這個文件cron.d,而不是單個用戶crontab
day # 日應該運行的工作( 1-31, *, */2, etc )
hour # 小時 ( 0-23, *, */2, etc )
job #指明運行的命令是什么
minute #分鍾( 0-59, *, */2, etc )
month # 月( 1-12, *, */2, etc )
name #定時任務描述
reboot # 任務在重啟時運行,不建議使用,建議使用special_time
special_time # 特殊的時間范圍,參數:reboot(重啟時),annually(每年),monthly(每月),weekly(每周),daily(每天),hourly(每小時)
state #指定狀態,prsent表示添加定時任務,也是默認設置,absent表示刪除定時任務
user # 以哪個用戶的身份執行
weekday # 周 ( 0-6 for Sunday-Saturday, *, etc )
每10分鍾執行一次:
ansible webserver -m cron -a 'name="test crontab" minute="*/10" job="/bin/echo hello,world!" '
ansible test -m cron -a 'name="a job for reboot" special_time=reboot job="/some/job.sh"'
ansible test -m cron -a 'name="yum autoupdate" weekday="2" minute=0 hour=12 user="root
ansible test -m cron -a 'backup="True" name="test" minute="0" hour="5,2" job="ls -alh > /dev/null"'
ansilbe test -m cron -a 'cron_file=ansible_yum-autoupdate state=absent'
user
用戶模塊
[root@node1 ~]# ansible-doc -s user
- name: 管理用戶帳號
action: user
comment # 用戶的描述信息
createhome # 是否創建家目錄
force # 在使用`state=absent'是, 行為與`userdel --force'一致.
group # 指定基本組
groups # 指定附加組,如果指定為('groups=')表示刪除所有組
home # 指定用戶家目錄
login_class #可以設置用戶的登錄類 FreeBSD, OpenBSD and NetBSD系統.
move_home # 如果設置為`home='時, 試圖將用戶主目錄移動到指定的目錄
name= # 指定用戶名
non_unique # 該選項允許改變非唯一的用戶ID值
password # 指定用戶密碼
remove # 在使用 `state=absent'時, 行為是與 `userdel --remove'一致.
shell # 指定默認shell
state #設置帳號狀態,不指定為創建,指定值為absent表示刪除
system # 當創建一個用戶,設置這個用戶是系統用戶。這個設置不能更改現有用戶。
uid #指定用戶的uid
update_password # 更新用戶密碼
user: name=johnd comment="John Doe" uid=1040 group=admin
user: name=james shell=/bin/bash groups=admins,developers append=yes user: name=johnd state=absent remove=yes
user: name=james18 shell=/bin/zsh groups=developers expires=1422403387
user: name=test generate_ssh_key=yes ssh_key_bits=2048 ssh_key_file=.ssh/id_rsa #生成密鑰時,只會生成公鑰文件和私鑰文件,和直接使用ssh-keygen指令效果相同,不會生成authorized_keys文件。
注:指定password參數時,不能使用明文密碼,因為后面這一串密碼會被直接傳送到被管理主機的/etc/shadow文件中,所以需要先將密碼字符串進行加密處理。然后將得到的字符串放到password中即可。
echo "123456" | openssl passwd -1 -salt $(< /dev/urandom tr -dc '[:alnum:]' | head -c 32) -stdin
$1$4P4PlFuE$ur9ObJiT5iHNrb9QnjaIB0
#使用上面的密碼創建用戶
ansible all -m user -a 'name=foo password="$1$4P4PlFuE$ur9ObJiT5iHNrb9QnjaIB0"'
group
用戶組模塊
[root@node1 ~]# ansible-doc -s group
- name: 添加或刪除組
action: group
gid # 設置組的GID號
name= # 管理組的名稱
state # 指定組狀態,默認為創建,設置值為absent為刪除
system # 設置值為yes,表示為創建系統組
創建一個mysql用戶組,並添加到mysql用戶到組中
ansible test_name -m group -a 'name=mysql gid=306 system=yes'
ansible test_name -m user -a 'name=mysql uid=306 system=yes group=msyql'
copy
復制模塊
[root@node1 ~]# ansible-doc -s copy
- name: 將文件復制到被管理主機
action: copy
backup # 創建一個備份文件包括時間戳信息,如果以某種方式重創錯了,還可以拿回原始文件
content # 取代src=,表示直接用此處指定的信息生成為目標文件內容;
dest= # 遠程節點存放文件的路徑,必須是絕對路徑
directory_mode # 遞歸復制設置目錄權限,默認為系統默認權限
force # 如果目標主機包含該文件,但內容不同,如果設置為yes,則強制覆蓋,如果設置為no,則只有當目標主機的目標位置不存在該文件時,才復制。默認為yes
group # 復制到遠程主機后,指定文件或目錄的屬
mode # 復制到遠程主機后,指定文件或目錄權限,類似與 `chmod'指明如 0644
owner # 復制到遠程主機后,指定文件或目錄屬主
src # 要復制到遠程主機的文件在本地的地址,可以是絕對路徑,也可以是相對路徑。如果路徑是一個目錄,它將遞歸復制。在這種情況下,如果路徑使用"/"來結尾,則只復制目錄里的內容,如果沒有使用"/"來結尾,則包含目錄在內的整個內容全部復制,類似於rsync。
將本地的/etc/fatab文件復制到目標主機的/tmp/fatab.ansbile,屬主為root權限為640
ansible test_name -m copy -a 'src=/etc/fstab dest=/tmp/tatab.ansible owner=root mode=640'
在webserver組主機創建文件,自己手動指定文件內容:
ansilbe webserver -copy -a 'content='hello,world!\n' dest=/tmp/tatab.ansible'
ansible test -m copy -a "src=/srv/myfiles/foo.conf dest=/etc/foo.conf owner=foo group=foo mode=0644"
ansible test -m copy -a "src=/mine/ntp.conf dest=/etc/ntp.conf owner=root group=root mode=644 backup=yes"
ansible test -m copy -a "src=/mine/sudoers dest=/etc/sudoers validate='visudo -cf %s'"
file
文件操作模塊
[root@node1 ~]# ansible-doc -s file
- name: 設置文件屬性
action: file
force # 需要在兩種情況下強制創建軟連接,一種是源文件不存在但之后會建立的情況下;另一種是目標連接已存在,需要先取消之前的軟連接,有兩個選項:yes|no
group # 設置文件或目錄的屬組
mode # 設置文件或目錄的權限
owner # 設置文件或目錄的屬主
path= # 必選項,定義文件或目錄的路徑
recurse # 遞歸設置文件的屬性,只對目錄有效
src # 要被鏈接到的路徑,只應用與state=link的情況
state # directory:如果目錄不存在,創建目錄;
file:即使文件不存在,也不會被創建
link:創建軟連接;
hard:創建硬連接;
touch:如果文件不存在,則會創建一個新的文件,如果文件或目錄已存在,則更新其最后修改時間
absent:刪除目錄、文件或者取消鏈接文件
示例:設置/tmp/fstab.ansbile屬主和屬組都為mysql,權限為644
ansible test_name -m file -a 'path=/tmp/fstab.ansible owner=mysql group=mysql mode=644'
在webserver組創建/test/fstab.ansible的連接文件:
ansilbe webserver -m file -a 'path=/test/fstab.link src=/tmp/ansible.fstab state=link'
Ping
功能:測試指定主機是否能連接,如果成功返回pong。
service
[root@node1 ~]# ansible-doc -s service
- name: 管理服務
action: service
arguments # 向服務傳遞的命令行參數
enabled # 設置服務開機自動啟動,參數為yes|no
name= # 控制服務的名稱
pattern # 定義一個模式,如果通過status指令來查看服務的狀態時,沒有響應,就會通過ps指令在進程中根據該模式進行查找,如果匹配到,則認為該服務依然在運行
runlevel # 設置服務自啟動級別
sleep # 如果執行了restarted,則在stop和start之間沉睡幾秒鍾
state # 啟動`started' 關閉`stopped' 重新啟動 `restarted' 重載 `reloaded'
啟動httpd服務,並設置開機自動啟動:
ansible webserver -m service -a 'name=httpd enabled=yes state=started'
ansible test -m service -a "name=httpd state=started enabled=yes"
asnible test -m service -a "name=foo pattern=/usr/bin/foo state=started"
ansible test -m service -a "name=network state=restarted args=eth0"
shell
功能:執行的命令中有管道或者變量,就需要使用shell
[root@node1 ~]# ansible-doc -s shell
- name: Execute commands in nodes.
action: shell
chdir # 執行之前,先cd到指定目錄在執行命令
creates # 一個文件名,當這個文件存在,則該命令不執行
executable # 切換shell來執行命令,需要使用命令的絕對路徑
free_form= # 執行的命令
removes # 一個文件名,這個文件不存在,則該命令不執行
與command模塊類似.
更改user帳號的密碼:
ansible webserver -m shell -a 'echo new_password | passwd --stdin user1'
raw
與shell模塊功能相同,推薦優先使用raw模塊
script
腳本模塊
[root@node1 ~]# ansible-doc -s script
- name: 將本地腳本復制到遠程主機並運行之
action: script
creates # 一個文件名,當這個文件存在,則該命令不執行
free_form= # 本地腳本路徑
removes # 一個文件名,這個文件不存在,則該命令不執行
ansible webserver -m script -a '/root/script.sh'
yum
[root@node1 ~]# ansible-doc -s yum
- name: Manages packages with the `yum' package manager
action: yum
conf_file # yum的配置文件
disable_gpg_check # 關閉gpg_check
disablerepo # 不啟用某個源
enablerepo # 啟用某個源
name= # 指定要安裝的包,如果有多個版本需要指定版本,否則安裝最新的包
state # 安裝(`present'),安裝最新版(`latest'),卸載程序包(`absent')
安裝zsh:
ansible webserver -m yum -a 'name=zsh'
ansible test -m yum -a 'name=httpd state=latest'
ansible test -m yum -a 'name="@Development tools" state=present'
ansible test -m yum -a 'name=http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm state=present'
setup
收集指定服務器的信息,每個被管理節點在接收並運行管理命令之前,會將自己主機相關信息,如操作系統版本、IP地址等報告給遠程的ansbile主機.在playbooks里經常會用到的一個參數gather_facts就與該模塊相關。setup模塊下經常使用的一個參數是filter參數,具體使用示例如下:
ansible 10.212.52.252 -m setup -a 'filter=ansible_*_mb' //查看主機內存信息
ansible 10.212.52.252 -m setup -a 'filter=ansible_eth[0-2]' //查看地接口為eth0-2的網卡信息
ansible all -m setup --tree /tmp/facts //將所有主機的信息輸入到/tmp/facts目錄下,每台主機的信息輸入到主機名文件中(/etc/ansible/hosts里的主機名)
synchronize
使用rsync同步文件,其參數如下:
archive: 歸檔,相當於同時開啟recursive(遞歸)、links、perms、times、owner、group、-D選項都為yes ,默認該項為開啟
checksum: 跳過檢測sum值,默認關閉
compress:是否開啟壓縮
copy_links:復制鏈接文件,默認為no ,注意后面還有一個links參數
delete: 刪除不存在的文件,默認no
dest:目錄路徑
dest_port:默認目錄主機上的端口 ,默認是22,走的ssh協議
dirs:傳速目錄不進行遞歸,默認為no,即進行目錄遞歸
rsync_opts:rsync參數部分
set_remote_user:主要用於/etc/ansible/hosts中定義或默認使用的用戶與rsync使用的用戶不同的情況
mode: push或pull 模塊,push模的話,一般用於從本機向遠程主機上傳文件,pull 模式用於從遠程主機上取文件
src=some/relative/path dest=/some/absolute/path rsync_path="sudo rsync"
src=some/relative/path dest=/some/absolute/path archive=no links=yes
src=some/relative/path dest=/some/absolute/path checksum=yes times=no
src=/tmp/helloworld dest=/var/www/helloword rsync_opts=--no-motd,--exclude=.git mode=pull
目的:將主控方/root/a目錄推送到指定節點的/tmp目錄下
命令:ansible 10.1.1.113 -m synchronize -a 'src=/root/a dest=/tmp/ compress=yes'
執行效果:
delete=yes 使兩邊的內容一樣(即以推送方為主)
compress=yes 開啟壓縮,默認為開啟
--exclude=.Git 忽略同步.git結尾的文件
由於模塊,默認都是推送push。因此,如果你在使用拉取pull功能的時候,可以參考如下來實現
mode=pull
更改推送模式為拉取模式
目的:將10.1.1.113節點的/tmp/a目錄拉取到主控節點的/root目錄下
命令:ansible 10.1.1.113 -m synchronize -a 'mode=pull src=/tmp/a dest=/root/'
由於模塊默認啟用了archive參數,該參數默認開啟了recursive, links, perms, times, owner,group和-D
參數。如果你將該參數設置為no,那么你將停止很多參數,比如會導致如下目的遞歸失敗,導致無法拉取。
其他相關的參數解釋:
dest_port=22 # 指定目的主機的ssh端口,ansible配置文件中的 ansible_ssh_port 變量優先級高於該 dest_port 變量
rsync_path # 指定 rsync 命令來在遠程服務器上運行。這個參考rsync命令的--rsync-path參數,
--rsync-path=PATH # 指定遠程服務器上的rsync命令所在路徑信息
rsync_timeout # 指定 rsync 操作的 IP 超時時間,和rsync命令的 --timeout 參數效果一樣
raw
目的:在10.1.1.113節點上運行hostname命令
命令:ansible 10.1.1.113 -m raw -a 'hostname|tee'
get_url模塊:
目的:將http://10.1.1.116/favicon.ico
文件下載到指定節點的/tmp目錄下
命令:ansible 10.1.1.113 -m get_url -a 'url=http://10.1.1.116/favicon.ico dest=/tmp'
參數:
sha256sum:下載完成后進行sha256 check;
timeout:下載超時時間,默認10s
url:下載的URL
url_password、url_username:主要用於需要用戶名密碼進行驗證的情況
use_proxy:是事使用代理,代理需事先在環境變更中定義
get_url: url=http://example.com/path/file.conf dest=/etc/foo.conf mode=0440
get_url: url=http://example.com/path/file.conf dest=/etc/foo.conf sha256sum=b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c
filesystem
在塊設備上創建文件系統
選項:
dev:目標塊設備
force:在一個已有文件系統 的設備上強制創建
fstype:文件系統的類型
opts:傳遞給mkfs命令的選項
示例:
ansible test -m filesystem -a 'fstype=ext2 dev=/dev/sdb1 force=yes'
ansible test -m filesystem -a 'fstype=ext4 dev=/dev/sdb1 opts="-cc"'
mount
配置掛載點
選項:
dump
fstype:必選項,掛載文件的類型
name:必選項,掛載點
opts:傳遞給mount命令的參數
src:必選項,要掛載的文件
state:必選項
present:只處理fstab中的配置
absent:刪除掛載點
mounted:自動創建掛載點並掛載之
umounted:卸載
name=/mnt/dvd src=/dev/sr0 fstype=iso9660 opts=ro state=present
name=/srv/disk src='LABEL=SOME_LABEL' state=present
name=/home src='UUID=b3e48f45-f933-4c8e-a700-22a159ec9077' opts=noatime state=present
#####創建掛載例子
ansible test -a 'dd if=/dev/zero of=/disk.img bs=4k count=1024'
ansible test -a 'losetup /dev/loop0 /disk.img'
ansible test -m filesystem 'fstype=ext4 force=yes opts=-F dev=/dev/loop0'
ansible test -m mount 'name=/mnt src=/dev/loop0 fstype=ext4 state=mounted opts=rw'
unarchive模塊
用於解壓文件,模塊包含如下選項:
copy:在解壓文件之前,是否先將文件復制到遠程主機,默認為yes。若為no,則要求目標主機上壓縮包必須存在。
creates:指定一個文件名,當該文件存在時,則解壓指令不執行
dest:遠程主機上的一個路徑,即文件解壓的路徑
grop:解壓后的目錄或文件的屬組
list_files:如果為yes,則會列出壓縮包里的文件,默認為no,2.0版本新增的選項
mode:解決后文件的權限
src:如果copy為yes,則需要指定壓縮文件的源路徑
owner:解壓后文件或目錄的屬主
例子:
- unarchive: src=foo.tgz dest=/var/lib/foo
- unarchive: src=/tmp/foo.zip dest=/usr/local/bin copy=no
- unarchive: src="https://example.com/example.zip" dest=/usr/local/bin copy=no