本節索引:
一、前期環境準備
二、Playbook中模板templates的用法
三、Playbook中邏輯語句的用法
四、Roles角色詳解
一、環境搭建:
前期準備:一臺虛擬機作為ansible,三臺虛擬機作為被控端node
主控端:
主機名:ansible
系統版本:CentOS6.9
被控端:
主機名:node1
CPU內核數:4
系統版本:CentSO7.4
主機名:node2
CPU內核數:2
系統版本:CentSO6.9
主機名:node3
CPU內核數:1
系統版本:CentSO6.9
/etc/ansible/hosts文件主機列表配置如下:
二、Playbook中模板templates的用法
templates模板:
功能:根據模塊文件動態生成對應的配置文件
使用方法:
(1)templates文件必須存放在templates目錄下,且以.j2為后綴
(2)templates模塊只能被playbook調用
(3)yam文件需和templates目錄平級,目錄結構如下:
./
├── temnginx.yml
└── templates
└── nginx.conf.j2
templates使用形式:
字符串:使用單引號或雙引號
數字:整數,浮點數
列表:[item1, item2, …]
元組:(item1, item2, …)
字典:{key1:value1, key2:value2, …}
布爾型:true/false
算術運算:+, -, *, /, //, %, **
比較操作:==, !=, >, >=, <, <=
邏輯運算:and, or, not
流表達式:For If When
示例1:使用template傳輸配置文件
cp /etc/nginx/nginx.conf? templates/nginx.conf.j2
vim testtemplate.yml
—
– hosts: os6
? remote_user: root
? tasks:
??? – name: install package
????? yum: name=nginx
??? – name: copy template
????? template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf
??? – name: start service
????? service: name=nginx state=started enabled=yes
執行結果:運行playbook后,我們發現work process進程數量與虛擬機cpu內核數量是一致的,接下來我
們將把配置模板中的work process進程數量與系統自帶變量結合起來引用。
示例2:template引用系統變量
ansible websrvs -m setup |grep ‘cpu’
vim templates/nginx.conf.j2
worker_processes {{ ansible_processor_vcpus+2 }};
vim testtemplate.yml
—
– hosts:os6
? remote_user: root
? tasks:
??? – name: install package
????? yum: name=nginx
??? – name: copy template
????? template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf
????? notify: restart service
??? – name: start service
????? service: name=nginx state=started enabled=yes
? handlers:
? ??– name: restart service
????? service:name=nginx state=restarted
執行結果:再次運行playbook后,我們發現worker process進程數量等于cpu核心數量加2,這樣template
就能幫我們實現根據不同主機性能定制相應的配置。
示例3:hosts文件普通變量修改nginx服務端口
vim /etc/ansible/hosts
192.168.30.101 httpd_port=81
192.168.30.102 httpd_port=82
vim templates/nginx.conf.j2
?server {
?????? listen?????? {{ http_port }} default server
?????? listen?????? [::]:{{ http_prot }} default server
? }
三、Playbook中邏輯語句的使用
When:
條件測試:如果需要根據變量、facts或此前任務的執行結果來做為某task執行與
否的前提時要用到條件測試,通過when語句實現,在task中使用,jinja2的語法
查看發行版本系統變量:
ansible srv -m setup filter=”*distribution”
示例1:
vim testtemplate.yml
—
– hosts: all
? remote_user: root
? tasks:
??? – name: install package
????? yum: name=nginx
??? – name: copy template for centos7
????? template: src=nginx.conf7.j2 dest=/etc/nginx/nginx.conf
????? when: ansible_distribution_major_version == “7”
????? notify: restart service
??? – name: copy template for centos6
????? template: src=nginx.conf6.j2 dest=/etc/nginx/nginx.conf
????? when: ansible_distribution_major_version == “6”
????? notify: restart service
??? – name: start service
????? service: name=nginx state=started enabled=yes
? handlers:
??? – name: restart service
????? service:name=nginx state=restarted
執行結果:當when語句不匹配時,將skipping直接跳過,僅執行與when語句匹配的語句內容,最終
CentOS6,7根據不同的版本號生成對應的配置并啟動服務。
with_items:迭代
迭代:當有需要重復性執行的任務時,可以使用迭代機制
對迭代項的引用,固定變量名為“item”
要在task中使用with_items給定要迭代的元素列表
列表格式:
字符串
字典
示例1:利用迭代一次創建多個文件,安裝多個命令包
vim testitem.yml
—
– hosts: all
? remote_user: root
? tasks:
??? – name: create some file
????? file: name=/data/{{ item}}? state=touch
????? when: ansible_distribution_major_version == “7”
????? with_items:
??????? – file1
??????? – file2
??????? – file3
??? – name: install some packages
????? yum: name={{ item }}
????? with_items:
??????? – htop
??????? – sl
??????? – hping3
執行結果:當系統為CentOS7版本時,在/data目錄下創建file1-3文件,安裝htop,sl,hping3命令
示例2:使用迭代創建組
vim testitem2.yml
—
– hosts: all
? remote_user: root
? tasks:
??? – name: create some groups
????? group: name={{ item }}
????? when: ansible_distribution_major_version == “7”
????? with_items:
??????? – g1
??????? – g2
??????? – g3
執行結果:當系統版本為CentOS7時,創建g1,g2,g3組
示例3:使用迭代配合字典創建用戶與組
vim testitem2.yml
—
– hosts: all
? remote_user: root
? tasks:
??? – name: create some groups
????? group: name={{ item }}
????? with_items:
??????? – g1
??????? – g2
??????? – g3
??? – name: create some users
????? user: name={{ item.name }} group={{ item.group }}
????? with_items:
??????? – { name: ‘user1’,group: ‘g1’ }
??????? – { name: ‘user2’,group: ‘g2’ }
??????? – { name: ‘user3’,group: ‘g3’ }
執行結果:所有主機上創建user1,user2,user3用戶,且主組為g1,g2,g3
?
for 與 ?if
示例1:template,for
vim for1.conf.j2
{% for port in ports %}
server{
???????? listen {{ port }}
}
{% endfor %}
vim testfor.yml
—
– hosts: websrvs
? remote_user: root
? vars:
??? ports:
????? – 81
????? – 82
????? – 83
? tasks:
??? – name: copy conf
? ? ? ?template: src=for1.conf.j2? dest=/data/for1.conf
執行結果:每臺主機生成for1.conf文件,內容如下
示例2:template,for,引用字典
vim for2.conf.j2
{% for port in ports %}
server{
???????? listen {{ port.listen_port }}
}
{% endfor %}
cp testfor.yml testfor2.yml
vim testfor2.yml
—
– hosts: websrvs
? remote_user: root
? vars:
??? ports:
????? – listen_port: 81
????? – listen_port: 82
????? – listen_port: 83
? tasks:
??? – name: copy conf
????? template:src=for2.conf.j2? dest/data/for2.conf
執行結果:每臺主機生成for2.conf文件,內容如下
示例3:for循環中調用字典
vim for3.conf.j2
{% for p in ports %}
server{
???????? listen {{ p.port }}
???????? servername {{ p.name }}
?? ?documentroot {{ p.rootdir }}
}
{% endfor %}
cp testfor2.yml testfor3.yml
vim testfor3.yml
—
– hosts: websrvs
? remote_user: root
? vars:
??? ports:
????? – web1:
??????? port: 81
??????? name: web1.magedu.com
? ? ? ? rootdir: /data/website1
????? – web2:
??????? port: 82
??????? name: web2.magedu.com
? ? ? ? rootdir: /data/website2
????? – web3:
??????? port: 83
??????? name: web3.magedu.com
? ? ? ? rootdir: /data/website3
? tasks:
??? – name: copy conf
????? template:src=for3.conf.j2? dest/data/for3.conf
執行結果:每臺主機生成for3.conf文件,內容如下
示例4:for循環中調用if
vim for4.conf.j2
{% for p in ports %}
server{
???????? listen {{ p.port }}
{% if p.name is defined %}
???????? servername {{ p.name }}
{% endif %}
??????? documentroot {{ p.rootdir }}
}
{% endfor %}
cp testfor3.yml testfor4.yml
vim testfor4.yml
—
– hosts: websrvs
? remote_user: root
? vars:
??? ports:
????? – web1:
??????? port: 81
??????? #name: web1.magedu.com
???????? rootdir: /data/website1
????? – web2:
??????? port: 82
??????? name: web2.magedu.com
???????? rootdir: /data/website1
????? – web3:
??????? port: 83
??????? #name: web3.magedu.com
???????? rootdir: /data/website1
? tasks:
??? – name: copy conf
????? template:src=for4.conf.j2? dest/data/for4.conf
執行結果:每臺主機生成for3.conf文件,內容如下,web1與web3的name沒賦值,所有跳過,web2的
name被賦值,文件中輸出結果
四、Roles角色
角色(roles):角色集合
roles/
? mysql/
? ?httpd/
? ?nginx/
? ?memcached/
/roles/project/ :項目名稱,有以下子目錄
files/ :存放由copy或script模塊等調用的文件
templates/:template模塊查找所需要模板文件的目錄
tasks/:定義task,role的基本元素,至少應該包含一個名為main.yml的文件;其它的文
件需要在此文件中通過include進行包含
handlers/:至少應該包含一個名為main.yml的文件;其它的文件需要在此文件中通過
include進行包含
vars/:定義變量,至少應該包含一個名為main.yml的文件;其它的文件需要在此文件
中通過include進行包含
meta/:定義當前角色的特殊設定及其依賴關系,至少應該包含一個名為main.yml的文
件,其它文件需在此文件中通過include進行包含
default/:設定默認變量時使用此目錄中的main.yml文件
建議:roles創建在ansible目錄
mkdir roles
mkdir roles/{httpd,mysql,memcache,nginx} -pv
示例1:定義nginx角色
思路:
niginx
1.group:nginx
2.user:nginx
3.yum:nginx
4.template:nigin.conf.j2
5.service:nginx
目錄結構如下:
cd nginx
mkdir tasks templates
cd tasks
vim group.yml
– name: create group
? group: name=nginx gid=80
vim user.yml
– name: create user
? user: name=nginx group=nginx uid=80 shell=/sbin/noligin
vim yum.yml
– name: install package
? yum: name=nginx
vim start.yml
– name: start service
? service: name=nginx state=started enabled=yes
vim restart.yml
– name: restart service
? service: name=nginx state=restarted
cp /etc/nginx/nginx.conf? template/nginx.conf.j2
vim template/nginx.conf.j2
worker_processes {{ ansible_processes_vcpus+2 }};
vim templ.yml
– name: copy conf
template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf
vim main.yml
– include: group.yml
– include: user.yml
– include: yum.yml
– include: templ.yml
– include: start.yml
調用角色的劇本要和roles目錄在同一文件夾
vim nginx_roles.yml
– hosts: websrvs
? romete_user: root
? roles:
? ? – role: nginx
ansible-playbook -C nginx_role.yml
執行結果如下:
示例2:增加httpd角色
結構目錄如下:
cd httpd/
mkdir tasks
cd tasks/
vim user.yml
– name: create user
? user: name=apache system=yes shell=/sbin/nologin
cd httpd/
mkdir files
cp httpd.conf ? files/
cd /tasks/
模擬編譯安裝yum
vim copyfile.yml
– name: copy files
? copy: src=httpd.conf dest=/data/ own=apache
vim main.yml
– incluse: user.yml
– incluse: copyfile.yml
vim httpd_role.yml
– hosts: websrvs
? romete_user: root
? roles:
? ? – role: httpd
執行結果如下:
示例3:同時調用兩個roles角色
目錄結構:
cp niginx_role.yml some_role.yml
vim some_role.yml
– hosts: websrvs
? romete_user: root
? roles:
? ? – role: httpd
? ? – role: nginx
執行結果如下:
示例4:一個roles角色調用另一個roles角色的task任務
目標:nginx調用httpd的copyfile
vim main.yml
– include: group.yml
– include: user.yml
– include: yum.yml
– include: templ.yml
– include: start.yml
– inclide: roles/httpd/tasks/copyfile.yml
示例5:roles playbook tags
目錄結構如下:
cp -r nginx/ app/???? 首先虛構一個app的role
vim some_role2.yml
– hosts: websrvs
? romete_user: root
? roles:
? ? – { role: httpd,tags:[‘web’,’httpd’]}
? ? – { role: nginx,tags:[‘web’,’nginx’]}
? ? – { role: app,tags:’app’}
ansible-playbook -t web some_role.yml
執行結果:只執行標簽為web的role
示例6:roles playbook tags when
cp -r nginx/ app/???? 虛構一個role
vim some_role3.yml
– hosts: all
? romete_user: root
? roles:
? ?– { role: httpd,tags:[‘web’,’httpd’]}
? ?– { role: nginx,tags:[‘web’,’nginx’],when: ansible_distribution_major_version==”7″}
? ?– { role: app,tags:’app’}
ansible-playbook -t web some_role.yml
執行結果:至執行tags標簽為web的roles,當主版本號為7時,才執行nginx的role
示例7:綜合演示
結構目錄:
rm -rf /app
mkdir app
cd app
mkdir tasks templates vars handlers files
cd tasks/
vim group.yml
– name: create group
? group: name=app system=yes gid=123
vim user.yml
– name: create user
? user: name=app group=app system=yes shell=/sbin/nologin uid=123
vim yum.yml
– name: install package
? ?yum: name=httpd
cp /etc/httpd/conf/httpd.conf /templates/httpd.conf.j2
vim temlates/httpd.conf.j2
Listen {{ ansible_processor_vcpus*10 }}
User {{ username }}
Group {{ groupname }}
vim /vars/main.yml
username: app
groupname: app
vim templ.yml
– name: copy conf
? temlplate: src=httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf
? notify: restart service
vim start.yml
– name: start service
? ?service: name=httpd state=started enabled=yes
vim handlers/main.yml
– name: restart service
? ?service: name=httpd state=restarted
touch files/vhosts.conf
vim copyfile.yml
– name: copy config
? ?copy: src=vhosts.conf? dest=/ect/httpd/conf.d/
vim main.yml
– include: group.yml
– include: user.yml
– include: yum.yml
– include: templ.yml
– include: copyfile.yml
– include: start.yml
cd ansible/
vim app_role.yml
– hosts: websrvs
? remote_user: root
? roles:
? ? – role: app
執行結果如下:
示例8:部署memcached,占用內存為物理內存1/4
yum install memcached
目錄結構:
cp /etc/sysconfig/memcached templates/memcached.j2
vim memcached.j2
CACHESIZE=”{{ ansible_memtotal_mb//4 }}”
vim tasks/yum.yml
– name: install package
? ?yum: name=memcached
vim templ.yum
– name: copy conf
? ?template: src=memcached.j2 dest=/etc/sysconfig/memcached
vim start.yml
– name: start service
? service: name=memcached state=started enabled=yes
vim main.yml
– include: yum.yml
– include: templ.yml
– inculde: start.yml
vim memcached_role.yml
– hosts: os6
? ?remote_user: root
? ?roles:
? ? – role: memcached
ansible-playbook memcached_role.yml
執行結果如下:
遠程查看配置文件,確認生效:
本文來自投稿,不代表Linux運維部落立場,如若轉載,請注明出處:http://www.www58058.com/99910