-
ansible劇本擴(kuò)展功能(續(xù)day38)
-
ansible劇本整合功能
-
ansible劇本角色配置
1.ansible劇本擴(kuò)展功能
d.劇本編寫循環(huán)功能
編寫方式一:列表方式設(shè)置循環(huán)
- hosts: 172.16.1.41
tasks:
- name: install software
yum: name={{ item }} state=installed
with_items:
- rsync
- nfs-utils
- telnet-server
編寫方式二:字典方式設(shè)置循環(huán)
- hosts: 172.16.1.41
tasks:
- name: create user
user: name={{ item.old01 }} uid={{ item.old02 }} shell={{ item.old03 }}
with_items:
- {old01: 'oldboy01', old02: '5001', old03: '/sbin/nologin'}
- {old01: 'oldboy02', old02: '5002', old03: '/sbin/nologin'}
ps:with_items也可以換成loop 調(diào)用時(shí)依然使用item調(diào)用
e.劇本忽略錯(cuò)誤功能(劇本中shell模塊使用時(shí)錯(cuò)誤問題)
[root@m01 ansible_playbook]# cat test_忽略錯(cuò)誤配置.yml
- hosts: 172.16.1.41
tasks:
- name: install software
shell: yum install -y htop
- name: create user
shell: useradd oldboy
ignore_errors: yes 開啟忽略錯(cuò)誤功能
- name: boot server
shell: systemctl start rsyncd
有時(shí)使用shell萬能模塊會(huì)出現(xiàn)的問題:
01.實(shí)現(xiàn)批量管理操作會(huì)更加麻煩
02.實(shí)現(xiàn)劇本任務(wù)功能忌栅,不具有冪等性
f.劇本編寫標(biāo)簽功能(調(diào)試劇本)
- name: create user
user: name=rsync shell=/sbin/nologin create_home=no
tags: oldboy01
ansible-playbook test_標(biāo)簽功能配置.yml -t oldboy01 --- 只執(zhí)行標(biāo)記任務(wù)
ansible-playbook test_標(biāo)簽功能配置.yml --skip-tags oldboy01 --- 跳過標(biāo)記任務(wù)
g.劇本提高執(zhí)行效率
取消劇本收集主機(jī)信息功能
- hosts: 172.16.1.41
gather_facts: no ---提升劇本執(zhí)行效率
tasks:
ps:取消主機(jī)收集信息功能旺嬉,判斷功能則也無法使用
劇本執(zhí)行慢的可能原因:
01.SSH遠(yuǎn)程連接優(yōu)化沒有配置(關(guān)閉認(rèn)證功能袄友,關(guān)閉DNS反向解析功能)
02.使用yum模塊下載軟件(使用本地yum倉庫)
03.劇本執(zhí)行收集信息慢
04.劇本執(zhí)行過程必須保證完整
例如yum任務(wù)在執(zhí)行時(shí),ctrl+c中斷劇本任務(wù)很可能導(dǎo)致yum進(jìn)程還在
h.劇本觸發(fā)器功能配置
[root@m01 ansible_playbook]# cat test_觸發(fā)功能配置.yml
- hosts: 172.16.1.41
tasks:
- name: push config file
copy: src=/tmp/rsyncd.conf dest=/etc/
notify: rsync_restart
- name: boot server
service: name=rsyncd state=started
handlers:
- name: rsync_restart
service: name=rsyncd state=restarted
PS: 觸發(fā)器任務(wù)會(huì)在所有任務(wù)執(zhí)行完畢之后才執(zhí)行
劇本編寫擴(kuò)展功能: https://docs.ansible.com/ansible/latest/user_guide/playbooks.html
2.ansible劇本整合功能
方式一:include_tasks:f1.yml
- hosts: all
remote_user: root
tasks:
- include_tasks: f1.yml
- include_tasks: f2.yml
方式二:include: f1.yml
- include:f1.yml
- include:f2.yml
方式三:- import_playbook: 推薦
[root@m01 ansible-playbook]# cat main.yml
- import_playbook: base.yml
- import_playbook: rsync.yml
- import_playbook: nfs.yml