一叹括、ansible 劇本介紹:
1瀑晒、劇本的作用:
1)绍坝、將多個模塊操作的功能進行整合
2)、實現(xiàn)重復(fù)工作簡單化(提高工作效率)
3)瑰妄、實現(xiàn)特殊需求
2陷嘴、劇本中的內(nèi)容:角色信息、任務(wù)信息
hosts :角色信息
tasks :任務(wù)信息
3间坐、編寫劇本的語法規(guī)范(yaml):
1、注意縮進的關(guān)系:兩個空格一個縮進關(guān)系
2邑退、創(chuàng)建鍵值對 :使用冒號(如 age: 18);如果沒有空格是錯誤的竹宋;(如 age:18(錯誤))
有兩種特殊情況除外:
(1)、冒號結(jié)尾
(2)地技、冒號出現(xiàn)在描述和注釋當(dāng)中
3蜈七、列表:相同的信息或類別出現(xiàn)多次
釋義:多個列表使用短橫線+空格
如:
- team1 zhangsan lisi
- team2 zhangsan lisi
4、
ansible-playbook --syntax-check test01.yaml --- 檢查語法結(jié)構(gòu)
ansible-playbook -C test01.yaml --- 模擬執(zhí)行劇本
二莫矗、ansible 劇本編寫示范:
shell模塊和其他模塊書寫在一個name中會產(chǎn)生沖突
練習(xí)01:一鍵部署rsync守護進程服務(wù)
第1步:設(shè)置主機清單:
[root@m01 ~]# cat /etc/ansible/hosts | tail -n 6
[nfs01]
172.16.1.31
[backup]
172.16.1.41
[web01]
172.16.1.7
第2步:編寫劇本信息(注意書寫格式):
[root@m01 ~]# cat rsync.yaml
- hosts: backup
tasks:
- name: 01 安裝rsync服務(wù)
yum: name=rsync state=installed
- name: 02 編輯rsync配置文件 /etc/rsyncd.conf
copy: src=/etc/rsyncd.conf dest=/etc/
- name: 03 創(chuàng)建虛擬用戶rsync
user: name=rsync shell=/sbin/nologin create_home=no
- name: 04 創(chuàng)建本分文件目錄backup 權(quán)限 600
file: path=/backup state=directory owner=rsync group=rsync
- name: 05 創(chuàng)建服務(wù)端密碼文件 rsync.password 權(quán)限 600
copy: content='rsync_backup:123456' dest=/etc/rsync.password mode=600
- name: 啟動rsyncd 服務(wù)
service: name=rsyncd state=started enabled=yes
- hosts: web01
tasks:
- name: 01 創(chuàng)建服務(wù)端密碼文件
copy: content='123456' dest=/etc/rsync.password mode=600
第3步:測試劇本:
[root@m01 ~]# ansible-playbook --syntax-check rsync.yaml
playbook: rsync.yaml
[root@m01 ~]# ansible-playbook rsync.yaml
PLAY [backup] ***********************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************
ok: [172.16.1.41]
TASK [01 安裝rsync服務(wù)] ****************************************************************************************************************
changed: [172.16.1.41]
TASK [02 編輯rsync配置文件 /etc/rsyncd.conf] **********************************************************************************************
changed: [172.16.1.41]
TASK [03 創(chuàng)建虛擬用戶rsync] ***************************************************************************************************************
ok: [172.16.1.41]
TASK [04 創(chuàng)建本分文件目錄backup 權(quán)限 600] *****************************************************************************************************
ok: [172.16.1.41]
TASK [05 創(chuàng)建服務(wù)端密碼文件 rsync.password 權(quán)限 600] *******************************************************************************************
changed: [172.16.1.41]
TASK [啟動rsyncd 服務(wù)] ******************************************************************************************************************
changed: [172.16.1.41]
PLAY [web01] ************************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************
ok: [172.16.1.7]
TASK [01 創(chuàng)建服務(wù)端密碼文件] *****************************************************************************************************************
changed: [172.16.1.7]
PLAY RECAP **************************************************************************************************************************
172.16.1.41 : ok=7 changed=4 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
172.16.1.7 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@m01 ~]# [root@m01 ~]# cat rsync.yaml
第4步:執(zhí)行劇本
[root@m01 ~]# ansible-playbook rsync.yaml
PLAY [backup] ***********************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************
ok: [172.16.1.41]
TASK [01 安裝rsync服務(wù)] ****************************************************************************************************************
changed: [172.16.1.41]
TASK [02 編輯rsync配置文件 /etc/rsyncd.conf] **********************************************************************************************
changed: [172.16.1.41]
TASK [03 創(chuàng)建虛擬用戶rsync] ***************************************************************************************************************
changed: [172.16.1.41]
TASK [04 創(chuàng)建本分文件目錄backup 權(quán)限 600] *****************************************************************************************************
ok: [172.16.1.41]
TASK [05 創(chuàng)建服務(wù)端密碼文件 rsync.password 權(quán)限 600] *******************************************************************************************
changed: [172.16.1.41]
TASK [啟動rsyncd 服務(wù)] ******************************************************************************************************************
changed: [172.16.1.41]
PLAY [web01] ************************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************
ok: [172.16.1.7]
TASK [01 創(chuàng)建服務(wù)端密碼文件] *****************************************************************************************************************
changed: [172.16.1.7]
PLAY RECAP **************************************************************************************************************************
172.16.1.41 : ok=7 changed=5 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
172.16.1.7 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@m01 ~]#
第5步:測試服務(wù)
[root@web01 ~]# rsync /etc/fstab rsync_backup@10.0.0.41::backup --password-file=/etc/rsync.password
[root@backup ~]# ll /backup/
total 4
-rw-r--r--. 1 rsync rsync 501 Jul 6 23:37 fstab
[root@backup ~]# cls
[root@backup ~]#
三飒硅、劇本編寫擴展:
劇本編寫擴展說明:
1、劇本編寫設(shè)置變量(vars)
2作谚、劇本編寫注冊信息(register)
3三娩、劇本編寫循環(huán)功能(with_items)
4、劇本編寫判斷功能(when)
5妹懒、劇本編寫忽略錯誤(ignore_errors)
6雀监、劇本編寫標(biāo)簽功能(tags)
7、劇本編寫忽略采集(gather_facts)
8、劇本編寫觸發(fā)功能(handlers)
9会前、劇本編寫匯總功能(目錄:files handlers tasks templates vars)
1好乐、劇本編寫設(shè)置變量(vars)
第1種方式:在劇本中設(shè)置變量
[root@m01 /server/scripts]# cat vars_test.yaml
- hosts: backup
vars:
dir_info: /etc/
dir_file: hosts
dest_info: /tmp/
dest_file: hosts.bak
tasks:
- name: copy 文件
copy: src={{ dir_info }}{{ dir_file }} dest={{ dest_info }}{{ dest_file }}
第2種方式:在命令行指定變量
注意:此種方式還可以嵌入組變量
[root@m01 /server/scripts]# cat vars_test.yaml
- hosts: backup
vars:
dir_info: /etc/
dir_file: hosts
tasks:
- name: copy 文件
copy: src={{ dir_info }}{{ dir_file }} dest={{ dest_info }}{{ dest_file }}
[root@m01 /server/scripts]# vim vars_test.yaml
[root@m01 /server/scripts]# ansible-playbook -e dir_info=/etc/ -e dir_file=hosts vars_test.yaml
PLAY [backup] ***********************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************
ok: [172.16.1.41]
TASK [copy 文件] **********************************************************************************************************************
changed: [172.16.1.41]
PLAY RECAP **************************************************************************************************************************
172.16.1.41 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@m01 /server/scripts]#
第3種方式:在主機清單中設(shè)置變量
[root@m01 /server/scripts]# tail -n 10 /etc/ansible/hosts
## db-[99:101]-node.example.com
[nfs01]
172.16.1.31
[backup]
172.16.1.41
[backup:vars]
dir_info=/etc/
dir_file=hosts
[web01]
172.16.1.7
注意 三種變量配置方式優(yōu)先順序如下:命令行配置變量最優(yōu)先,劇本文件中配置其次瓦宜,主機清單中配置最后蔚万。
2、劇本編寫注冊信息(register)
使用說明:register 的作用是將上一模塊的執(zhí)行結(jié)果報轉(zhuǎn)化為register指定的變量中临庇, 如果下面的模塊需要用到這個變量反璃,直接調(diào)用即可。一般配合debug使用
[root@m01 /server/scripts]# cat sshd_zhuce.yaml
- hosts: backup
tasks:
- name: 重啟sshd服務(wù)
service: name=sshd state=restarted
- name: 查看啟動后端口信息
shell: ss -tnlp | grep sshd
register: port_info #將shell 模塊執(zhí)行的結(jié)果保存到 port_info 中苔巨,從某種角度來講 這時的port_info 這時是一個變量版扩。
- name: 顯示端口信息
debug: msg={{ port_info.stdout_lines }} #用debug模塊輸出結(jié)果 stdout_lines 表示標(biāo)準(zhǔn)輸出。
3侄泽、劇本編寫循環(huán)功能(with_items)
功能說明:這個模塊主要做循環(huán)使用礁芦,利用with_items子句生成一個循環(huán)列表,使用 {{ item }}
固定模式來調(diào)用變量悼尾,調(diào)用的是with_items 生成的整個列表柿扣。此功能還有第二種用法,使用{{item.xxx}}
的方式來調(diào)用變量闺魏。具體參照下面第二種方式未状。
第1種方式:
- hosts: backup
remote_user: root
tasks:
- name: create user to backup
user: name={{ item }} state=present
with_items:
- test1
- test2
- test3
第2種方式:
循環(huán)方式二:
- hosts: 172.16.1.7
tasks:
- name: create user01-03
user: name={{ item.name }} shell={{ item.shell }} create_home={{ item.home }}
with_items:
- {name: 'user01', shell: '/sbin/nologin', home: 'yes'}
- {name: 'user02', shell: '/bin/bash', home: 'no'}
- {name: 'user03', shell: '/bin/bash', home: 'yes'}
4、劇本編寫判斷功能 (when)
使用說明:想要使用when 語句析桥,首先要先了解一下setup模塊的用法司草,setup模塊可以查詢到一些主機信息,然后借用這些主機信息來對條件做判斷泡仗。下面給羅列了一些常用的參數(shù)
常見主機信息:
主機參數(shù) | 參數(shù)說明 |
---|---|
ansible_all_ipv4_addresses: | 僅顯示ipv4的信息埋虹。 |
ansible_devices: | 僅顯示磁盤設(shè)備信息。 |
ansible_distribution: | 顯示是什么系統(tǒng)娩怎,例:centos,suse等搔课。 |
ansible_distribution_major_version: | 顯示是系統(tǒng)主版本。 |
ansible_distribution_version: | 僅顯示系統(tǒng)版本截亦。 |
ansible_machine: | 顯示系統(tǒng)類型爬泥,例:32位,還是64位崩瓤。 |
ansible_eth0: | 僅顯示eth0的信息袍啡。 |
ansible_hostname: | 僅顯示主機名。 |
ansible_kernel: | 僅顯示內(nèi)核版本谷遂。 |
ansible_lvm: | 顯示lvm相關(guān)信息葬馋。 |
ansible_memtotal_mb: | 顯示系統(tǒng)總內(nèi)存。 |
ansible_memfree_mb: | 顯示可用系統(tǒng)內(nèi)存。 |
ansible_memory_mb: | 詳細(xì)顯示內(nèi)存情況畴嘶。 |
ansible_swaptotal_mb: | 顯示總的swap內(nèi)存蛋逾。 |
ansible_swapfree_mb: | 顯示swap內(nèi)存的可用內(nèi)存。 |
ansible_mounts: | 顯示系統(tǒng)磁盤掛載情況窗悯。 |
ansible_processor: | 顯示cpu個數(shù)(具體顯示每個cpu的型號)区匣。 |
ansible_processor_vcpus: | 顯示cpu個數(shù)(只顯示總的個數(shù))。 |
[root@m01 /server/scripts/playbook]# vim copy.yaml
- hosts: all
remote_user: root
tasks:
- name: 分發(fā)文件 hosts
copy: src=/etc/hosts dest=/tmp
when: (ansible_hostname == "backup")
- name: 分發(fā)文件 fstab
copy: src=/etc/fstab dest=/tmp
when: (ansible_hostname != "backup")
[root@m01 /server/scripts/playbook]# ansible-playbook copy.yaml
PLAY [all] **************************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************
ok: [172.16.1.31]
ok: [172.16.1.41]
ok: [172.16.1.7]
TASK [分發(fā)文件 hosts] *******************************************************************************************************************
skipping: [172.16.1.31]
skipping: [172.16.1.7]
changed: [172.16.1.41]
TASK [分發(fā)文件 fstab] *******************************************************************************************************************
skipping: [172.16.1.41]
changed: [172.16.1.31]
changed: [172.16.1.7]
PLAY RECAP **************************************************************************************************************************
172.16.1.31 : ok=2 changed=1 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
172.16.1.41 : ok=2 changed=1 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
172.16.1.7 : ok=2 changed=1 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
[root@m01 /server/scripts/playbook]#
5蒋院、劇本編寫忽略錯誤(ignore_errors)
使用說明:此功能用于忽略報錯亏钩,加上此語句表示忽略當(dāng)前報錯,使劇本繼續(xù)執(zhí)行欺旧。
[root@m01 /server/scripts/playbook]# cat ignore_errors.yaml
- hosts: backup
remote_user: root
tasks:
- name: 創(chuàng)建用戶user01
user: namei=user01
ignore_errors: yes
- name: 復(fù)制 reboot.yaml
copy: src=reboot.yaml dest=/tmp
- name: 更改reboot.yaml權(quán)限
file: path=/tmp/hosts mode=600
[root@m01 /server/scripts/playbook]# ansible-playbook ignore_errors.yaml
PLAY [backup] ***********************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************
ok: [172.16.1.41]
TASK [創(chuàng)建用戶user01] *******************************************************************************************************************
fatal: [172.16.1.41]: FAILED! => {"changed": false, "msg": "Unsupported parameters for (user) module: namei Supported parameters include: append, authorization, comment, create_home, expires, force, generate_ssh_key, group, groups, hidden, home, local, login_class, move_home, name, non_unique, password, password_lock, profile, remove, role, seuser, shell, skeleton, ssh_key_bits, ssh_key_comment, ssh_key_file, ssh_key_passphrase, ssh_key_type, state, system, uid, update_password"}
...ignoring
TASK [復(fù)制 reboot.yaml] ***************************************************************************************************************
changed: [172.16.1.41]
TASK [更改reboot.yaml權(quán)限] **************************************************************************************************************
changed: [172.16.1.41]
PLAY RECAP **************************************************************************************************************************
172.16.1.41 : ok=4 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=1
[root@m01 /server/scripts/playbook]#
6姑丑、劇本編寫標(biāo)簽功能(tags)
使用說明:在劇本中使用標(biāo)簽功能,加上標(biāo)簽后 辞友,在執(zhí)行劇本時可以指定只運行標(biāo)簽內(nèi)容栅哀,還是跳過標(biāo)簽內(nèi)容
跳過標(biāo)簽運行劇本: ansible-playbook --skip-tags=標(biāo)簽名 劇本名
只運行標(biāo)簽內(nèi)容:ansible-playbook -t 標(biāo)簽名 劇本名
如下示例:
[root@m01 /server/scripts/playbook]# cat tags.yaml
- hosts: backup
remote_user: root
tasks:
- name: 創(chuàng)建用戶user01
user: namei=user01
ignore_errors: yes
tags: test01
- name: 復(fù)制 reboot.yaml
copy: src=reboot.yaml dest=/tmp
- name: 更改reboot.yaml權(quán)限
file: path=/tmp/hosts mode=600
[root@m01 /server/scripts/playbook]# ansible-playbook -t test01 tags.yaml
PLAY [backup] ***********************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************
ok: [172.16.1.41]
TASK [創(chuàng)建用戶user01] *******************************************************************************************************************
fatal: [172.16.1.41]: FAILED! => {"changed": false, "msg": "Unsupported parameters for (user) module: namei Supported parameters include: append, authorization, comment, create_home, expires, force, generate_ssh_key, group, groups, hidden, home, local, login_class, move_home, name, non_unique, password, password_lock, profile, remove, role, seuser, shell, skeleton, ssh_key_bits, ssh_key_comment, ssh_key_file, ssh_key_passphrase, ssh_key_type, state, system, uid, update_password"}
...ignoring
PLAY RECAP **************************************************************************************************************************
172.16.1.41 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=1
[root@m01 /server/scripts/playbook]#
7、劇本編寫忽略采集(gather_facts)
使用說明:忽略采集功能称龙,直接執(zhí)行任務(wù)信息留拾,目的在于提高工作效率
[root@m01 /server/scripts/playbook]# cat gatehr_facts.yaml
- hosts: backup
gather_facts: no
remote_user: root
tasks:
- name: 創(chuàng)建用戶user01
user: namei=user01
ignore_errors: yes
- name: 復(fù)制 reboot.yaml
copy: src=reboot.yaml dest=/tmp
- name: 更改reboot.yaml權(quán)限
file: path=/tmp/hosts mode=600
[root@m01 /server/scripts/playbook]# ansible-playbook gatehr_facts.yaml
PLAY [backup] ***********************************************************************************************************************
TASK [創(chuàng)建用戶user01] *******************************************************************************************************************
fatal: [172.16.1.41]: FAILED! => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "msg": "Unsupported parameters for (user) module: namei Supported parameters include: append, authorization, comment, create_home, expires, force, generate_ssh_key, group, groups, hidden, home, local, login_class, move_home, name, non_unique, password, password_lock, profile, remove, role, seuser, shell, skeleton, ssh_key_bits, ssh_key_comment, ssh_key_file, ssh_key_passphrase, ssh_key_type, state, system, uid, update_password"}
...ignoring
TASK [復(fù)制 reboot.yaml] ***************************************************************************************************************
ok: [172.16.1.41]
TASK [更改reboot.yaml權(quán)限] **************************************************************************************************************
ok: [172.16.1.41]
PLAY RECAP **************************************************************************************************************************
172.16.1.41 : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=1
[root@m01 /server/scripts/playbook]#
8、劇本編寫觸發(fā)功能(handlers)
使用場景:一般用于某些配置文件發(fā)生改變鲫尊,需要重新啟動某些服務(wù)痴柔,但是一個劇本中可能有多個服務(wù),為了不影響其他的服務(wù)疫向,但是又能實現(xiàn)當(dāng)前的任務(wù)
使用說明:handlers 只會處理notify中列出的任務(wù)咳蔚,而且不論notify中有多少個任務(wù),handlers都會一次處理完成搔驼,如果notify沒有通知handlers將不會運行屹篓;其實handlers與常規(guī)任務(wù)并沒有什么不同,但是這里要注意handlers 和tasks是同一列匙奴。
- hosts: backup
tasks:
- name: copy file
copy: src=rsyncd.conf dest=/etc/
notify: rsync_server
- name: boot server
service: name=rsyncd state=started
handlers:
- name: rsync_server
service: name=rsyncd state=restarted
9、劇本編寫匯總功能
例:以部署自動部署NFS為例(其實還可以完善)
準(zhǔn)備工作在 /etc/ansible/roles下創(chuàng)建files 妄荔、 handlers 泼菌、 tasks 、templates 啦租、vars 五個目錄哗伯,這五個目錄各有其用。
a篷角、tasks: 包含角色要執(zhí)行的主要任務(wù)列表焊刹,此實例內(nèi)容如下:
[root@m01 /etc/ansible/roles]# ls nfs/
files handlers tasks templates vars
[root@m01 /etc/ansible/roles]# cat nfs/tasks/main.yaml
- name: 01 安裝部署軟件
yum: name={{ item.name }} state=installed
with_items:
- {name: 'nfs-utils'}
- {name: 'rpcbind' }
- name: 02 分發(fā)NFS配置文件
copy: src=exports dest={{ conf_dir }}exports
- name: 創(chuàng)建用戶組
group: name={{ group_name }} gid={{ gid_num }} state=present
- name: 03 創(chuàng)建虛擬用戶
user: name={{ user_name }} uid={{ uid_num }} group={{ gid_num }} shell=/sbin/nologin create_home=no state=present
ignore_errors: no
- name: 04 創(chuàng)建掛載目錄
file: path=/web_data owner={{ user_name }} group={{ user_name }} state=directory
- name: 05 啟動NFS服務(wù)
service: name={{ item.start }} state=started
with_items:
- {start: 'nfs'}
- {start: 'rpcbind' }
notify: change_status
- name: 重讀配置文件
service: name=nfs state=reloaded
- name: 查看服務(wù)啟動狀態(tài)
shell: showmount -e {{ ip }}
register: start_info
- name: 顯示服務(wù)啟動狀態(tài)
debug: msg={{ start_info.stdout_lines }}
[root@m01 /etc/ansible/roles]#
b、vars:角色的其他變量(有關(guān)更多信息,請參閱使用變量)虐块。此實例內(nèi)容如下
[root@m01 /etc/ansible/roles]# cat nfs/vars/main.yaml
conf_dir: /etc/
gid_num: "1100"
uid_num: "1100"
user_name: www
group_name: www
ip: 172.16.1.31
[root@m01 /etc/ansible/roles]#
c俩滥、files : 包含可以通過此角色部署的文件。此實例用到的文件如下
[root@m01 /etc/ansible/roles]# ls nfs/files/
exports
[root@m01 /etc/ansible/roles]#
d贺奠、handlers:包含處理程序霜旧,可以由此角色使用,甚至可以在此角色之外的任何位置使用儡率。此實例內(nèi)容如下
[root@m01 /etc/ansible/roles]# cat nfs/handlers/main.yaml
- name: change_status
service: name=nfs state=reloaded
[root@m01 /etc/ansible/roles]#
e挂据、templates - 包含可以通過此角色部署的模板。此實例未使用到此目錄儿普,如需要使用請到官網(wǎng)查看崎逃,或發(fā)間信于我,我來幫助你
f眉孩、在/etc/ansible/roles/ 下編寫site.yml的文件个绍,文件內(nèi)容如下,這里注意文件的后綴是.yml勺像。
[root@m01 /]# cat etc/ansible/roles/site.yml
- hosts: nfs01
remote_user: root
roles:
- nfs
[root@m01 /]#
g障贸、在 /etc/ansible/roles/下執(zhí)行劇本即可,再提一下 使用ansible之前吟宦,一定好提前規(guī)劃好ansible的主機文件即/etc/ansible/hosts,具體配置在本博客Ansible基礎(chǔ)(一)中可以找到
[root@m01 /etc/ansible/roles]# ansible-playbook --syntax-check site.yml 3進行語法檢查
playbook: site.yml
[root@m01 /etc/ansible/roles]# ansible-playbook site.yml #無問題后執(zhí)行即可這里不再做演示
10篮洁、多個劇本整合成一個劇本執(zhí)行
方式一
(1)事先寫好的劇本,不包含主機信息
[root@m01 /server/ansible_play]# cat rsync.yaml
- name: 01:安裝軟件
yum: name=rsync state=installed
- name: 02:分發(fā)配置文件
copy: src=/etc/rsyncd.conf dest=/etc/rsyncd.conf
- name: 03:創(chuàng)建虛擬用戶
user: name=rsync shell=/sbin/nologin create_home=no
- name: 04:創(chuàng)建備份目錄
file: path=/backup_data state=directory owner=rsync group=rsync
- name: 05:創(chuàng)建密碼文件
copy: content='rsync_backuo:123456' dest=/etc/rsync.password mode=600
- name: 06:啟動服務(wù)
service: name=rsyncd state=started enabled=yes
- name: 01:創(chuàng)建密碼文件
copy: content='123456' dest=/etc/rsync.password mode=600
[root@m01 /server/ansible_play]# cat with_items.yaml
- name: 01:安裝NFS
yum: name={{ item.name }} state=installed
with_items:
- {name: 'nfs-utils'}
- {name: 'rpcbind'}
[root@m01 /server/ansible_play]#
(2)將上面的兩個劇本放在一個劇本中執(zhí)行
其實就是給主機信息殃姓,然后用include調(diào)用
[root@m01 /server/ansible_play]# cat auto.yaml
- hosts: backup
tasks:
- include_tasks: rsync.yaml
- hosts: nfs01
tasks:
- include_tasks: with_items.yaml
[root@m01 /server/ansible_play]#
方式二
(1)正常書寫單個劇本
[root@m01 /server/ansible_play]# cat rsync.yaml
- hosts: backup
tasks:
- name: 01:安裝軟件
yum: name=rsync state=installed
- name: 02:分發(fā)配置文件
copy: src=/etc/rsyncd.conf dest=/etc/rsyncd.conf
- name: 03:創(chuàng)建虛擬用戶
user: name=rsync shell=/sbin/nologin create_home=no
- name: 04:創(chuàng)建備份目錄
file: path=/backup_data state=directory owner=rsync group=rsync
- name: 05:創(chuàng)建密碼文件
copy: content='rsync_backuo:123456' dest=/etc/rsync.password mode=600
- name: 06:啟動服務(wù)
service: name=rsyncd state=started enabled=yes
- hosts: web01
tasks:
- name: 01:創(chuàng)建密碼文件
copy: content='123456' dest=/etc/rsync.password mode=600
[root@m01 /server/ansible_play]# cat with_items.yaml
- hosts: nfs01
tasks:
- name: 01:安裝NFS
yum: name={{ item.name }} state=installed
with_items:
- {name: 'nfs-utils'}
- {name: 'rpcbind'}
[root@m01 /server/ansible_play]#
(2)匯總到一起執(zhí)行,執(zhí)行時只需ansible-playbook auto.yaml 即可
[root@m01 /server/ansible_play]# cat auto.yaml
- import_playbook: rsync.yaml
- import_playbook: with_items.yaml
[root@m01 /server/ansible_play]#