1 ansible-playbook 任務(wù)劇本
1.1 劇本文件概念
(1)playbook可以將多個批量操作模塊功能整合,完成一件事情。(2)簡化運(yùn)維工作復(fù)雜度(3)playbook通過yaml語法識別描述的狀態(tài)文件,擴(kuò)展名是yaml
1.2 劇本文件組成部分
(1)劇本的角色(hosts)定義的是主機(jī)信息(2)劇本的任務(wù)(tasks)定義的是具體任務(wù)信息(3)一個劇本文件有多個hosts組成,一個hosts可以包含多個tasks任務(wù)
1.3 劇本文件優(yōu)勢特點(diǎn)
(1)實現(xiàn)自動化功能更加全面(2)可以更好的控制邏輯關(guān)系(3)劇本展現(xiàn)命令語法更直觀(4)擁有持久反復(fù)執(zhí)行的特性
1.4 劇本文件編寫規(guī)范
(1)縮進(jìn)特點(diǎn): 兩個空格表示一個縮進(jìn)關(guān)系(2)冒號用法: 冒號后面需要有空格 冒號結(jié)尾不需要有空格主機(jī)信息: 172.16.1.41 --- key: value (鍵值寫法) (3)列表用法: 利用短橫線加空格構(gòu)建列表清單
1.5 劇本執(zhí)行使用方法
(1)檢查劇本語法:ansible-playbook --syntax-check test.yaml (2)劇本模擬執(zhí)行:ansible-playbook -C test.yaml (3)劇本真實運(yùn)行:ansible-playbook test.yaml
1.6 劇本編寫擴(kuò)展功能
(1)劇本變量編寫功能(2)劇本信息通知功能(3)劇本信息判斷功能(4)劇本信息循環(huán)功能(5)劇本編寫忽略錯誤(6)劇本標(biāo)簽設(shè)置功能(7)劇本忽略采集功能(8)劇本信息觸發(fā)功能
1.6.1 劇本變量編寫功能
設(shè)置變量方法一: 在劇本執(zhí)行命令參數(shù)中設(shè)置變量,命令行最優(yōu)先
[root@m01 ansible_playbook]#ansible-playbook -e dir=/etc -e file=rsyncd.conf test_變量編寫.yaml
設(shè)置變量方法二: 在劇本中設(shè)置變量,劇本變量其次優(yōu)先
[root@m01 ansible_playbook]#vim test_變量編寫.yaml
- hosts: 172.16.1.41
vars:
dir: /etc
file: rsyncd.conf
tasks:
- name: copy file
copy: src={{ dir }}/{{ file }} dest={{ dir }}/
# {{}}調(diào)用變量
設(shè)置變量方法二: 在主機(jī)清單中設(shè)置變量,主機(jī)清單變量最不優(yōu)先
[root@m01 ansible_playbook]#vim /etc/ansible/hosts
[sersync_server]
172.16.1.31
[sersync_client]
172.16.1.41
[sersync_server:vars]
dir=/etc
file=rsyncd.conf
# 直接給主機(jī)組設(shè)置變量,這樣主機(jī)組內(nèi)的所有主機(jī)都可以調(diào)用變量了
1.6.2 劇本信息通知功能
編輯劇本
[root@m01 ansible_playbook]#vim test_通知功能.yaml
- hosts: 172.16.1.41
tasks:
- name: boot server
service: name=rsyncd state=started
- name: check server boot
shell: netstat -lntup|grep 873
register: oldboy
- debug: msg={{ oldboy.stdout_lines }}
# 將shell中命令執(zhí)行結(jié)果通過register注冊給oldboy,oldboy相當(dāng)于一個變量,{{}}調(diào)取oldboy
# debug類似echo,輸出信息
# stdout_lines 將輸出的信息變得有格式
運(yùn)行劇本
[root@m01 ansible_playbook]#ansible-playbook test_通知功能.yaml
PLAY [172.16.1.41] ***********************************************************************************
TASK [Gathering Facts] *******************************************************************************
ok: [172.16.1.41]
TASK [boot server] ***********************************************************************************
ok: [172.16.1.41]
TASK [check server boot] *****************************************************************************
changed: [172.16.1.41]
TASK [debug] *****************************************************************************************
ok: [172.16.1.41] => {
"msg": [
"tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 3708/rsync ",
"tcp6 0 0 :::873 :::* LISTEN 3708/rsync "
]
}
PLAY RECAP *******************************************************************************************
172.16.1.41 : ok=4 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
1.6.3 劇本信息判斷功能
nfs服務(wù)客戶端三臺主機(jī)
centos7 10.0.0.7、centos6 10.0.0.8、centos7 10.0.0.9
此時在批量啟動的時候需要進(jìn)行判斷,因為centos6,centos7啟動命令不一樣
判斷的格式
- hosts: nfs_client
tasks:
- name: boot centos7 nfs
shell: systemctl start nfs
判斷: 如果是centos7 ???
- name: boot centos6 nfs
shell: /etc/init.d/nfs start
判斷: 如果是centos6 ???
setup模塊:收集遠(yuǎn)程主機(jī)信息
語法:
[root@m01 ansible_playbook]#ansible 172.16.1.41 -m setup -a "filter=ansible_hostname"
172.16.1.41 | SUCCESS => {
"ansible_facts": {
"ansible_hostname": "backup",
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false
}
# filter 過濾 篩選
實現(xiàn)收集子信息的方法
問題: 獲取主機(jī)信息,以及子信息
方法一:
- hosts: rsync
tasks:
- name: touch file
file: path=/etc/oldboy01.txt state=touch
when: (ansible_eth1.ipv4.address == "172.16.1.41")
方法二:
- hosts: rsync
tasks:
- name: touch file
file: path=/etc/oldboy01.txt state=touch
when: (ansible_eth1["ipv4"]["address"] == "172.16.1.41")
setup模塊常用來收集的信息
根據(jù) ip 地址進(jìn)行判斷創(chuàng)建目錄
[root@m01 ansible_playbook]#vim test_判斷功能.yaml
- hosts: nfs_client
tasks:
- name: create file for 41 host
file: path=/tmp/172.16.1.41 state=directory
when: (ansible_hostname == "backup")
- name: create file for 7 host
file: path=/tmp/172.16.1.7 state=directory
when: (ansible_hostname == "web01")
運(yùn)行劇本
root@m01 ansible_playbook]#ansible-playbook -C test_判斷功能.yaml
PLAY [nfs_client] ************************************************************************************
TASK [Gathering Facts] *******************************************************************************
ok: [172.16.1.41]
ok: [172.16.1.7]
TASK [create file for 41 host] ***********************************************************************
skipping: [172.16.1.7]
changed: [172.16.1.41]
TASK [create file for 7 host] ************************************************************************
skipping: [172.16.1.41]
changed: [172.16.1.7]
PLAY RECAP *******************************************************************************************
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
1.6.4 劇本信息循環(huán)功能
循環(huán)創(chuàng)建多個用戶
[root@m01 ansible_playbook]#vim test_循環(huán)功能.yaml
- hosts: 172.16.1.41
tasks:
- name: create user
user: name={{ item }}
with_items:
- oldgirl01
- oldgirl02
- oldgirl03
- oldgirl04
- oldgirl05
循環(huán)創(chuàng)建多個用戶 多個用戶uid數(shù)值是不同的
[root@m01 ansible_playbook]#vim test_循環(huán)功能.yaml
- hosts: 172.16.1.41
tasks:
- name: create user
user: name={{ item.name }} uid={{ item.uid }}
with_items:
- {name: "oldgirl06", uid: "3006"}
- {name: "oldgirl07", uid: "3007"}
- {name: "oldgirl08", uid: "3008"}
- {name: "oldgirl09", uid: "3009"}
- name: check create user info
shell: grep oldgirl0 /etc/passwd
register: user_info
- debug: msg={{ user_info.stdout_lines }}
1.6.5 劇本編寫忽略錯誤功能
忽略功能主要用來調(diào)試劇本
[root@m01 ansible_playbook]#vim test_h忽略功能.yaml
- hosts: 172.16.1.41
tasks:
- name: create rsync user
shell: useradd rsync -M -s /sbin/nologin
ignore_errors: yes
- name: create backup dir
shell: mkdir /backup
ignore_errors: yes
- name: boot server
shell: systemctl start rsyncd
ignore_errors: yes
在使用shell進(jìn)行一些操作時,shell產(chǎn)生的結(jié)果已經(jīng)存在時,會導(dǎo)致劇本無法進(jìn)行下去,因此使用忽略功能可以有效的使劇本進(jìn)行下去。
1.6.6 劇本標(biāo)簽設(shè)置功能
標(biāo)簽功能主要用來調(diào)試劇本
tags:標(biāo)簽
[root@m01 ansible_playbook]#vim test_標(biāo)簽功能.yaml
- hosts: 172.16.1.41
tasks:
- name: 01:安裝軟件
yum: name=rsync state=installed
ignore_errors: yes
- name: 02:創(chuàng)建用戶
user: name=rsync create_home=no shell=/sbin/nologin
ignore_errors: yes
tags: create_user
- name: 03:創(chuàng)建目錄
file: path=/backup state=directory
運(yùn)行劇本
ansible-playbook -t create_user test_標(biāo)簽功能.yaml --- 執(zhí)行劇本中標(biāo)簽任務(wù)
ansible-playbook --skip-tags create_user test_標(biāo)簽功能.yaml --- 跳過指定標(biāo)簽任務(wù),執(zhí)行其他任務(wù)
ansible-playbook -t create_user,create_dir test_標(biāo)簽功能.yaml --- 執(zhí)行多個標(biāo)簽
# -t=tags
1.6.7 劇本忽略采集功能
[
root@m01 ansible_playbook]#vim test_忽略采集.yaml
- hosts: 172.16.1.41
gather_facts: no
tasks:
- name: 01:安裝軟件
yum: name=rsync state=installed
ignore_errors: yes
- name: 02:創(chuàng)建用戶
user: name=rsync create_home=no shell=/sbin/nologin
ignore_errors: yes
tags: create_user
- name: 03:創(chuàng)建目錄
file: path=/backup state=directory
tags: create_dir
當(dāng)劇本采集大量主機(jī)信息時,可能會變得卡,慢,影響劇本后面的操作執(zhí)行的效率。所以在這個時候,可以忽略采集功能,提高效率,在hosts下面添加 gather_facts: no 如果劇本中有判斷功能,不能使用此參數(shù),因為采集的信息會與判讀信息對比
1.6.8 劇本信息觸發(fā)功能
編寫劇本
[root@m01 ansible_playbook]#vim test_觸發(fā)功能.yaml
- hosts: 172.16.1.41
tasks:
- name: 01:傳輸配置文件
copy: src=/etc/ansible/ansible_playbook/rsyncd.conf dest=/etc/
notify: rsync_restart
- name: 02:啟動服務(wù)程序
service: name=rsyncd state=started
handlers:
- name: rsync_restart
service: name=rsyncd state=restarted
handlers:一般用于配置文件修改時,才會進(jìn)行觸發(fā)功能,對服務(wù)進(jìn)行重啟 notify:傳輸配置文件過來,notify通知rsync_restart這個觸發(fā)器。然后handlers會進(jìn)行重啟服務(wù)說明: 整體任務(wù)執(zhí)行完畢,才會執(zhí)行觸發(fā)功能
1.7 編寫劇本練習(xí)題
要求:
(1)在172.16.1.41主機(jī)上操作: ①將定時任務(wù)服務(wù)停止 ②創(chuàng)建一個/etc/目錄軟連接 在/opt目錄中生成 ③將本地/etc/hosts文件分發(fā)給41主機(jī) 保存到/tmp目錄中(2)在172.16.1.31主機(jī)上操作: ①將防火墻服務(wù)開機(jī)自動運(yùn)行 ②將主機(jī)上安裝keepalived軟件
實踐:
編寫劇本文件
[root@m01 ansible_playbook]#vim test.yaml
- hosts: 172.16.1.41
tasks:
- service: name=crond state=stopped
- file: src=/etc path=/opt/etc_link state=link
- copy: src=/etc/hosts dest=/tmp
- hosts: 172.16.1.31
tasks:
- service: name=firewalld enabled=yes
- yum: name=keepalived state=installed
劇本語法檢查
# 語法檢查劇本文件
[root@m01 ansible_playbook]#ansible-playbook --syntax-check test.yaml
playbook: test.yaml
劇本模擬執(zhí)行
[root@m01 ansible_playbook]#ansible-playbook -C test.yaml
PLAY [172.16.1.41] ***********************************************************************************
TASK [Gathering Facts] *******************************************************************************
ok: [172.16.1.41]
TASK [service] ***************************************************************************************
ok: [172.16.1.41]
TASK [file] ******************************************************************************************
ok: [172.16.1.41]
TASK [copy] ******************************************************************************************
ok: [172.16.1.41]
PLAY [172.16.1.31] ***********************************************************************************
TASK [Gathering Facts] *******************************************************************************
ok: [172.16.1.31]
TASK [service] ***************************************************************************************
ok: [172.16.1.31]
TASK [yum] *******************************************************************************************
ok: [172.16.1.31]
PLAY RECAP *******************************************************************************************
172.16.1.31 : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
172.16.1.41 : ok=4 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
劇本真實執(zhí)行
[root@m01 ansible_playbook]#ansible-playbook test.yaml
PLAY [172.16.1.41] ***********************************************************************************
TASK [Gathering Facts] *******************************************************************************
ok: [172.16.1.41]
TASK [service] ***************************************************************************************
ok: [172.16.1.41]
TASK [file] ******************************************************************************************
ok: [172.16.1.41]
TASK [copy] ******************************************************************************************
ok: [172.16.1.41]
PLAY [172.16.1.31] ***********************************************************************************
TASK [Gathering Facts] *******************************************************************************
ok: [172.16.1.31]
TASK [service] ***************************************************************************************
ok: [172.16.1.31]
TASK [yum] *******************************************************************************************
ok: [172.16.1.31]
PLAY RECAP *******************************************************************************************
172.16.1.31 : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
172.16.1.41 : ok=4 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
補(bǔ)充:
如果系統(tǒng)中裝有cowsay軟件,在執(zhí)行命令時,會產(chǎn)生圖案信息,影響查閱結(jié)果,可以關(guān)閉。
[root@m01 ansible]#vim ansible.cfg
# don't like cows? that's unfortunate.
# set to 1 if you don't want cowsay support or export ANSIBLE_NOCOWS=1
# nocows = 1
把# nocows = 1 中的 # 去掉即可。
1.8 ansible劇本實現(xiàn)rsync一鍵化部署
第一個歷程: 按照模塊方式,完成服務(wù)每個步驟部署
第一步:服務(wù)端配置
# 安裝軟件程序
ansible rsync -m yum -a "name=rsync state=installed"
# 編寫配置文件:要在批量管理主機(jī)上提前寫好,然后推送給服務(wù)端
# 在管理端準(zhǔn)備好服務(wù)配置文件
ansible rsync_server -m copy -a "src=/etc/ansible/conf_file/rsyncd.conf dest=/etc/"
# 創(chuàng)建虛擬用戶
ansible rsync_server -m user -a "name=rsync create_home=no shell=/sbin/nologin"
# 創(chuàng)建密碼文件 (授權(quán)600)
ansible rsync_server -m copy -a "content='rsync_backup:oldboy123' dest=/etc/rsync.password mode=600"
# 創(chuàng)建備份目錄 (授權(quán) 屬主 屬組)
ansible rsync_server -m file -a "path=/backup state=directory owner=rsync group=rsync"
@ 啟動程序服務(wù)
ansible rsync_server -m service -a "name=rsyncd state=started enabled=yes"
第二步:客戶端配置
# 創(chuàng)建密鑰文件 (授權(quán)600)
ansible rsync_client -m copy -a "content='oldboy123' dest=/etc/rsync.password mode=600"
# 批量測試傳輸文件
ansible rsync_client -m shell -a "rsync -avz /etc/hosts rsync_backup@172.16.1.41::backup --password-file=/etc/rsync.password"
第二個歷程: 編寫劇本信息
[root@m01 ansible_playbook]#vim rsync_auto.yaml
- hosts: rsync_server
tasks:
- name: 01:install rsync
yum: name=rsync state=installed
- name: 02:copy conf file
copy: src=/etc/ansible/conf_file/rsyncd.conf dest=/etc/
- name: 03:create rsync user
user: name=rsync create_home=no shell=/sbin/nologin
- name: 04:create password file
copy: content='rsync_backup:oldboy123' dest=/etc/rsync.password mode=600
- name: 05:create backup dir
file: path=/backup state=directory owner=rsync group=rsync
- name: 06:boot rsync server
service: name=rsyncd state=started enabled=yes
- hosts: rsync_client
tasks:
- name: 01:create password file
copy: content='oldboy123' dest=/etc/rsync.password mode=600
恢復(fù)環(huán)境劇本
[root@m01 ansible_playbook]#vim rsync_backup.yaml
- hosts: rsync_server
tasks:
- name: 01:delete conf file
file: path=/etc/rsyncd.conf state=absent
- name: 02:delete rsync user
user: name=rsync state=absent
- name: 03:delete password file
file: path=/etc/rsync.password state=absent
- name: 04:delete backup dir
file: path=/backup/ state=absent
- name: 05:boot rsync server
service: name=rsyncd state=stopped enabled=no
- hosts: rsync_client
tasks:
- name: 01:delete password file
file: path=/etc/rsync.password state=absent
1.9 ansible劇本實現(xiàn)nfs一鍵化部署
第一個歷程: 按照模塊方式,完成服務(wù)每個步驟部署
服務(wù)端配置
01. 安裝部署軟件程序: rpcbind nfs-utile
ansible nfs_server -m yum -a "name=rpcbind state=installed"
ansible nfs_server -m yum -a "name=nfs-utile state=installed"
02. 編寫配置文件:配置文件要提前寫好
# 批量管理主機(jī)寫好的配置文件推送給服務(wù)端/etc/ansible-playbook/nfs.conf
ansible nfs_server -m copy -a "src=/etc/ansible/ansible_playbook/nfs.conf dest=/etc/exports"
03. 創(chuàng)建共享目錄:
ansible nfs_server -m file -a "path=/data/ state=directory owner=nfsnobody group=nfsnobody"
04. 啟動程序服務(wù):
ansible nfs_server -m service -a "name=rpcbind state=started enabled=yes"
ansible nfs_server -m service -a "name=nfs state=started enabled=yes"
客戶端配置:
01. 安裝部署軟件
ansible nfs_client -m yum -a "name=nfs-utile state=installed"
02. 掛載共享目錄
ansible nfs_client -m mount -a "src=172.16.1.31:/data/ path=/mnt fstype=nfs state=mounted"
第二個歷程編寫劇本:
[root@m01 ansible_playbook]#vim nfs_auto.yaml
- hosts: nfs_server
tasks:
- name: 1:install rpcbind nsf-utils
yum:
name:
- rpcbind
- nfs-utils
state: installed
- name: 2:copy conf file
copy: src=/etc/ansible/ansible_playbook/nfs.conf dest=/etc/exports
- name: 3:create data dir
file: path=/data/ state=directory owner=nfsnobody group=nfsnobody
- name: 4:boot server rcbind
service: name=rpcbind state=started enabled=yes
- name: 4:boot server nfs
service: name=nfs state=restarted enabled=yes
- hosts: nfs_client
tasks:
- name: 1:install nfs
yum: name=nfs-utils state=installed
- name: 2:mount data dir
mount: src=172.16.1.31:/data/ path=/mnt fstype=nfs state=mounted
恢復(fù)環(huán)境劇本
[root@m01 ansible_playbook]#vim nfs_backup.yaml
- hosts: nfs_server
tasks:
- name: 01:install rpcbind nfs-utils
yum:
name:
- rpcbind
- nfs-utils
state: removed
- name: 02:copy conf file
shell: echo "" >/etc/exports
- name: 03:create data dir
file: path=/data/ state=absent
- hosts: nfs_client
tasks:
- name: 01:install nfs
yum: name=nfs-utils state=removed
- name: 02:mount data dir
mount: src=172.16.1.31:/data/ path=/mnt fstype=nfs state=unmounted
優(yōu)化劇本:
[root@m01 ansible_playbook]#vim nfs_auto.yaml
- hosts: nfs_server
vars:
conf_file: exports
data_dir: /data
tasks:
- name: 01:install nfs rpcbind
yum:
name: ['nfs-utils', 'rpcbind']
state: installed
- name: 02:copy conf file
copy: src=/etc/ansible/ansible_playbook/nfs.conf dest=/etc/{{ conf_file }}
notify:
- nfs_restart
- name: 03:create data dir
file: path={{ data_dir }} state=directory owner=nfsnobody group=nfsnobody
- name: 04:boot server rpcbind
service: name={{ item.name }} state={{ item.state }} enabled={{ item.enabled }}
with_items:
- {name: "rpcbind", state: "started", enabled: "yes"}
- {name: "nfs", state: "started", enabled: "yes"}
handlers:
- name: nfs_restart
service: name=nfs state=reloaded
- hosts: nfs_client
vars:
data_dir: /data
tasks:
- name: 01:install nfs
yum: name=nfs-utils state=installed
- name: 02:mount data dir
mount: src=172.16.1.31:{{ data_dir }} path=/mnt fstype=nfs state=mounted
- name: 03:check mount info
shell: df -h|grep mnt
register: mount_info
- debug: msg={{ mount_info.stdout_lines }}
1.10 ansible劇本實現(xiàn)sersync一鍵化部署
第一個歷程: 按照模塊方式,完成服務(wù)每個步驟部署配置hosts主機(jī)清單
[server_server]
172.16.1.31
[server_client]
172.16.1.41
#安裝rsync
ansible backup_server -m yum -a "name=rsync state=installed"
#在批量管理主機(jī)上下載sersync,解壓發(fā)送給客戶端
ansible backup_server -m file -a "src=/usr/local/sersync_installdir_64bit/sersync dest=/usr/local"
#在批量管理主機(jī)上寫好sersync配置文件,發(fā)送給客戶端
ansible backup_server -m copy -a "src=/usr/local/sersync_installdir_64bit/sersync/conf/confxml.xml dest=/usr/local/sersync/conf/"
#給sersync加上執(zhí)行權(quán)限
ansible backup_server -m file -a "path=/usr/local/sersync/bin/sersync mode=a+x"
#給sersync創(chuàng)建軟鏈接
ansible backup_server -m file -a "src=/usr/local/sersync/bin/sersync path=/usr/local/sbin/sersync state=link"
#啟動sersync 測試實時同步
ansible backup_server -m shell -a "sersync -dro /usr/local/sersync/conf/confxml.xml"
第二個歷程,編寫劇本
[root@m01 ansible_playbook]#vim sersync_auto.yaml
- hosts: sersync_server
tasks:
- name: 安裝rsync
yum: name=rsync state=installed
- name: 將sersync傳輸?shù)娇蛻舳?
file: src=/usr/local/sersync_installdir_64bit/sersync/ dest=/usr/local
- name: 將寫好的配置文件傳輸?shù)娇蛻舳?
copy: src=/usr/local/sersync_installdir_64bit/sersync/conf/confxml.xml dest=/usr/local/sersync/conf/
- name: 加上執(zhí)行權(quán)限
file: path=/usr/local/sersync/bin/sersync mode=a+x
- name: 創(chuàng)建軟鏈接
file: src=/usr/local/sersync/bin/sersync path=/usr/local/sbin/sersync state=link
- name: 啟動sersync 測試實時同步
shell: sersync -dro /usr/local/sersync/conf/confxml.xml
恢復(fù)環(huán)境劇本
[root@m01 ansible_playbook]#cat sersync_backup.yaml
- hosts: sersync_server
tasks:
- name: 卸載rsync
yum: name=rsync state=removed
- name: 刪除sersync
file: path=/usr/local/sersync
2 多個劇本如何進(jìn)行整合
第一個歷程: 確保每個劇本執(zhí)行成功第二個歷程: 進(jìn)行劇本整合方法一:不建議使用
[root@m01 ansible_playbook]#vim zhenghe.yaml # ---角色里使用
- hosts: all
remote_user: root
tasks:
- include_tasks: nfs_auto.yml
- include_tasks: rsync_auto.yml
# 不寫hosts信息,只寫任務(wù)信息
方法二:在以后的ansible中可能會取消include功能
[root@m01 ansible_playbook]#vim zhenghe.yaml
- include:nfs_auto.yml
- include:rsync_auto.yml
方法三:建議使用這個方法
[root@m01 ansible_playbook]#vim zhenghe.yaml
- import_playbook: nfs_auto.yaml
- import_playbook: rsync_auto.yaml
3 ansible劇本編寫方式:角色
(1)規(guī)范ansible程序目錄結(jié)構(gòu)(2)匯總劇本中有定義的主機(jī)信息
3.1 角色調(diào)用流程圖
3.2 nfs服務(wù)角色編寫
第一個歷程: 創(chuàng)建角色目錄結(jié)構(gòu)
cd roles/;mkdir {nfs,rsync,web,sersync}
cd nfs/{vars,tasks,templates,handlers,files}
# vars: 定義變量信息
# tasks: 定義任務(wù)信息
# templates: 定義模板文件(jinja2模板文件)
# handlers: 定義觸發(fā)器信息
# files: 定義需要分發(fā)的文件
第二個歷程: 編寫文件信息 tasks: 任務(wù)信息編寫方式一: nfs服務(wù)編寫
vim main.yaml
- name: 01:install nfs rpcbind
yum:
name: ['nfs-utils', 'rpcbind']
state: installed
- name: 02:copy conf file
copy: src=/etc/ansible/ansible_playbook/nfs.conf dest=/etc/{{ conf_file }}
notify:
- nfs_restart
- name: 03:create data dir
file: path={{ data_dir }} state=directory owner=nfsnobody group=nfsnobody
- name: 04:boot server rpcbind
service: name={{ item.name }} state={{ item.state }} enabled={{ item.enabled }}
with_items:
- {name: "rpcbind", state: "started", enabled: "yes"}
- {name: "nfs", state: "started", enabled: "yes"}
- name: 01:install nfs
yum: name=nfs-utils state=installed
- name: 02:mount data dir
mount: src=172.16.1.31:{{ data_dir }} path=/mnt fstype=nfs state=mounted
- name: 03:check mount info
shell: df -h|grep mnt
register: mount_info
- debug: msg={{ mount_info.stdout_lines }}
tasks: 任務(wù)信息編寫方式二: tasks:定義任務(wù)信息
cd tasks
vim main.yaml
vim nfs_boot.yaml
vim nfs_conf.yaml
vim nfs_datadir.yaml
vim nfs_install.yaml
vim nfs_mount.yaml
#########################
vim main.yaml
- include_tasks: nfs_install.yaml
- include_tasks: nfs_conf.yaml
- include_tasks: nfs_datadir.yaml
- include_tasks: nfs_boot.yaml
- include_tasks: nfs_mount.yaml
vars:定義變量信息
vim main.yaml
conf_file: exports
data_dir: /data
files:定義需要分發(fā)的文件
[root@m01 files]# ll
total 4
-rw-r--r-- 1 root root 42 Jul 29 10:34 nfs.conf
handlers:定義觸發(fā)器信息
vim main.yaml
- name: nfs_restart
service: name=nfs state=reloaded
總結(jié)
以上所述是小編給大家介紹的ansible批量管理服務(wù) ,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!
您可能感興趣的文章:- 在alpine鏡像中添加ansible服務(wù)的方法
- python ansible服務(wù)及劇本編寫