一、复习playbook剧本
---
- name: first play for install nginx #设置play的名称
gather_facts: false #设置不收集facts信息
hosts: webservers:dbservers #指定执行此play的远程主机组
remote_user: root #指定执行此play的用户
tasks: #指定此play的任务列表
- name: disabled firewalld
service: name=firewalld state=stopped enabled=no
- name: disable selinux
command: '/sbin/setenforce 0'
ignore_errors: yes
- name: disabled selinux forever
replace: path=/etc/selinux/config regexp=enforcing replace=disabled after=loaded
- name: mount cdrom
mount: src=/dev/sr0 path=/mnt fstype=iso9660 state=mounted
- name: install pkgs
yum: name=pcre-devel,zlib-devel,openssl-devel,gcc,gcc-c++,make state=latest
- name: create nginx user
user: name=nginx create_home=no shell=/sbin/nologin
- name: unarchive nginx package
unarchive: copy=yes src=/etc/ansible/nginx/nginx-1.24.0.tar.gz dest=/opt/
- name: install nginx by source
shell: chdir=/opt/nginx-1.24.0/ ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module && make && make install
- name: create link file for nginx
file: state=link src=/usr/local/nginx/sbin/nginx path=/usr/local/sbin/nginx
- name: create nginx service file
copy: src=nginx.service dest=/lib/systemd/system/nginx.service
- name: start nginx
service: name=nginx state=started enabled=yes
二、playbook的定义、引用变量
2.1 基础变量的定义与引用
在yaml文件中,我们可以在初始配置的模块中用var去定义变量的存在,变量的格式为key:value,以此来确定该变量在剧本中的存在
vim test1.yaml
---
- name: this is a play for testing variables
hosts: dbservers
remote_user: root
vars:
filename: abc.txt
tasks:
- name: touch a test file
file: path=/opt/{{filename}} state=touch
ansible-playbook test1.yaml
2.2 引用fact信息中的变量
首先我们知道 使用 ansible 组 -m setup 可以收集该组中所有的节点信息 ,
所以setup中fact'信息,有时候会剧本编写中需要,而fact的信息也是可以通过变量的方式进行调用
vim test2.yaml
---
- name: this is a playbook for quote variate
hosts: dbservers
remote_user: root
tasks:
- name: reading setup fact variate
debug: msg={{ansible_date_time.weekday}}
~
三、playbook中的when条件判断和变量循环使用
3.1 when条件判断
#选用filter=ansible_default_ipv4中的address作为when条件进行测试
ansible all -m setup -a 'filter=ansible_default_ipv4'
vim test3.yaml
---
- name: this is when test playbook
hosts: all
remote_user: root
tasks:
- name: test when
debug: msg='判断位置'
when: ansible_default_ipv4.address == "192.168.136.198"
ansible-playbook test3.yaml
除此之外 when条件还可以通过 !=(不等于条件来进行判断)
vim test3.yaml
---
- name: this is when test playbook
hosts: all
remote_user: root
tasks:
- name: test when
debug: msg='判断位置'
when: ansible_default_ipv4.address != "192.168.136.198"
ansible-playbook test3.yaml
四、变量循环
(1)with_item 单循环输出
vim test4.yaml
---
- name: item test
hosts: dbservers
remote_user: root
gather_facts: no
tasks:
- debug:
msg: "{{item}}"
with_items: [a, b, c, d]
ansible-playbook test4.yaml
当列表为两个时。with_item的输出方式:
vim test4.yaml
---
- name: item test
hosts: dbservers
remote_user: root
gather_facts: no
tasks:
- debug:
msg: "{{item}}"
with_items:
- [a, b, c, d]
- [1 ,2, 3, 4]
ansible-playbook test4.yaml
(2)with_list 每组列表一起循环的输出
---
- name: item test
hosts: dbservers
remote_user: root
gather_facts: no
tasks:
- debug:
msg: "{{item}}"
with_list:
- [a, b, c, d]
- [1 ,2, 3, 4]
~
~
(3)with_together 同一列表位置数据组合输出的循环
---
- name: item test
hosts: dbservers
remote_user: root
gather_facts: no
tasks:
- debug:
msg: "{{item}}"
with_together:
- [a, b, c, d]
- [1 ,2, 3, 4]
~
---
- name: item test
hosts: dbservers
remote_user: root
gather_facts: no
tasks:
- debug:
msg: "{{item}}"
with_together:
- [a, b, c, d]
- [1 ,2, 3, 4]
- [A, B, C]
(4) with_nested 列表数据循环匹配的循环(根据列表个数定义有多少层的循环)
---
- name: item test
hosts: dbservers
remote_user: root
gather_facts: no
tasks:
- debug:
msg: "{{item}}"
with_nested:
- [a, b, c, d]
- [1 ,2, 3, 4]
~
四种迭代循环方式的总结
whith_items: {{item}}会把所有的列表展开进行遍历输出,with_flattened也可以替代with_items
with_list: {{item}}会把每个列表当作一个整体输出。如果每个列表中只有一个值,则效果与with items一致。loop也可以替代ith
with_together: {{item}}引用时会把每个列表相同位置的值对齐合并后输出
with nested:{ {item}}引用时会把每个列表的值两两组合循环输出
五、Templates 模块
Jinja是基于Python的模板引擎。Template类是Jinja的一个重要组件,可以看作是一个编译过的模板文件,用来产生目标文本,传递Python的变量给模板去替换模板中的标记。
本次我们以改变apche的配置文件为例,来展现Templates模块的运用
(1)先准备一个以 .j2 为后缀的 template 模板文件,设置引用的变量
#如果没有相关的httpd的配置文件,可以先yum按住一个httpd的服务,取其主配置文件
cp httpd.conf /etc/ansible/httpd/httpd.conf.j2
vim /etc/ansible/httpd/httpd.conf.j2
Listen {{http_port}} #42行,修改
ServerName {{server_name}} #95行,修改
DocumentRoot "{{root_dir}}" #119行,修改
(2) 修改主机清单文件,使用主机变量定义一个变量名相同,而值不同的变量
vim /etc/ansible/hosts
[webservers]
192.168.136.197 http_port=192.168.136.197:80 server_name=www.test1.com:80 root_dir=/etc/httpd/htdocs
[dbservers]
192.168.136.198 http_port=192.168.136.198:80 server_name=www.test2.com:80 root_dir=/etc/httpd/htdocs
此外如果没有做DNS解析域名,还需要对主机名进行映射 :
vim /etc/hosts
192.168.73.106 www.test1.com
192.168.73.107 www.test2.com
(3)编写 playbook
mkdir /etc/ansible/templates
vim apache.yaml
---
- hosts: all
remote_user: root
vars:
- package: httpd
- service: httpd
tasks:
- name: install httpd package
yum: name={{package}} state=latest
- name: install configure file
template: src=/etc/ansible/httpd/httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf
notify:
- restart httpd
- name: create root dir
file: path=/etc/httpd/htdocs state=directory
- name: start httpd server
service: name={{service}} enabled=true state=started
handlers:
- name: restart httpd
service: name={{service}} state=restarted
ansiable-playbook apache.yaml
六、Tags
可以在一个playbook中为某个或某些任务定义“标签”,在执行此playbook时通过ansible-playbook命令使用--tags选项能实现仅运行指定的tasks。
playbook还提供了一个特殊的tags为always。作用就是当使用always作为tags的task时,无论执行哪一个tags时,定义有always的tags都会执行。
6.1 单标签的使用
vim test10.yaml
---
- name: this is a play for testing variables
hosts: dbservers
remote_user: root
vars:
filename: abc.txt
tasks:
- name: position 1
debug:
msg: 'ls /opt'
tags:
- only
- name: position 2
debug:
msg: 'ls /mnt'
ansible-playbook test1.yaml --tags="only"
6.2 多标签的运用
---
- name: this is a play for testing variables
hosts: dbservers
remote_user: root
vars:
filename: abc.txt
tasks:
- name: position 1
debug:
msg: '测试标签1'
tags:
- one
- name: position 2
debug:
msg: '测试标签2'
tags:
- two
- name: position 3
debug:
msg: '测试标签3'
tags:
- one
6.3 通用标签always的运用
---
- name: this is a play for testing variables
hosts: dbservers
remote_user: root
vars:
filename: abc.txt
tasks:
- name: position 1
debug:
msg: '测试标签1'
tags:
- one
- name: position 2
debug:
msg: '测试通用标签always'
tags:
- always
- name: position 3
debug:
msg: '测试标签3'
tags:
- one