一、复习playbook剧本
---
- name: first play for install nginx #设置play的名称gather_facts: false #设置不收集facts信息hosts: webservers:dbservers #指定执行此play的远程主机组remote_user: root #指定执行此play的用户tasks: #指定此play的任务列表- name: disabled firewalldservice: name=firewalld state=stopped enabled=no- name: disable selinuxcommand: '/sbin/setenforce 0'ignore_errors: yes- name: disabled selinux foreverreplace: path=/etc/selinux/config regexp=enforcing replace=disabled after=loaded- name: mount cdrommount: src=/dev/sr0 path=/mnt fstype=iso9660 state=mounted- name: install pkgsyum: name=pcre-devel,zlib-devel,openssl-devel,gcc,gcc-c++,make state=latest- name: create nginx useruser: name=nginx create_home=no shell=/sbin/nologin- name: unarchive nginx packageunarchive: copy=yes src=/etc/ansible/nginx/nginx-1.24.0.tar.gz dest=/opt/- name: install nginx by sourceshell: 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 nginxfile: state=link src=/usr/local/nginx/sbin/nginx path=/usr/local/sbin/nginx- name: create nginx service filecopy: src=nginx.service dest=/lib/systemd/system/nginx.service- name: start nginxservice: name=nginx state=started enabled=yes
二、playbook的定义、引用变量
2.1 基础变量的定义与引用
在yaml文件中,我们可以在初始配置的模块中用var去定义变量的存在,变量的格式为key:value,以此来确定该变量在剧本中的存在
vim test1.yaml
---
- name: this is a play for testing variableshosts: dbserversremote_user: rootvars:filename: abc.txttasks:- name: touch a test filefile: path=/opt/{{filename}} state=touchansible-playbook test1.yaml
2.2 引用fact信息中的变量
首先我们知道 使用 ansible 组 -m setup 可以收集该组中所有的节点信息 ,
所以setup中fact'信息,有时候会剧本编写中需要,而fact的信息也是可以通过变量的方式进行调用
vim test2.yaml
---
- name: this is a playbook for quote variatehosts: dbserversremote_user: roottasks:- name: reading setup fact variatedebug: 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 playbookhosts: allremote_user: roottasks:- name: test whendebug: msg='判断位置'when: ansible_default_ipv4.address == "192.168.136.198"ansible-playbook test3.yaml
除此之外 when条件还可以通过 !=(不等于条件来进行判断)
vim test3.yaml
---
- name: this is when test playbookhosts: allremote_user: roottasks:- name: test whendebug: msg='判断位置'when: ansible_default_ipv4.address != "192.168.136.198"
ansible-playbook test3.yaml
四、变量循环
(1)with_item 单循环输出
vim test4.yaml
---
- name: item testhosts: dbserversremote_user: rootgather_facts: notasks:- debug:msg: "{{item}}"with_items: [a, b, c, d]ansible-playbook test4.yaml
当列表为两个时。with_item的输出方式:
vim test4.yaml
---
- name: item testhosts: dbserversremote_user: rootgather_facts: notasks:- debug:msg: "{{item}}"with_items:- [a, b, c, d]- [1 ,2, 3, 4]
ansible-playbook test4.yaml
(2)with_list 每组列表一起循环的输出
---
- name: item testhosts: dbserversremote_user: rootgather_facts: notasks:- debug:msg: "{{item}}"with_list:- [a, b, c, d]- [1 ,2, 3, 4]
~
~
(3)with_together 同一列表位置数据组合输出的循环
---
- name: item testhosts: dbserversremote_user: rootgather_facts: notasks:- debug:msg: "{{item}}"with_together:- [a, b, c, d]- [1 ,2, 3, 4]
~
---
- name: item testhosts: dbserversremote_user: rootgather_facts: notasks:- debug:msg: "{{item}}"with_together:- [a, b, c, d]- [1 ,2, 3, 4]- [A, B, C]
(4) with_nested 列表数据循环匹配的循环(根据列表个数定义有多少层的循环)
---
- name: item testhosts: dbserversremote_user: rootgather_facts: notasks:- 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.j2vim /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/hosts192.168.73.106 www.test1.com
192.168.73.107 www.test2.com
(3)编写 playbook
mkdir /etc/ansible/templates
vim apache.yaml
---
- hosts: allremote_user: rootvars:- package: httpd- service: httpdtasks:- name: install httpd packageyum: name={{package}} state=latest- name: install configure filetemplate: src=/etc/ansible/httpd/httpd.conf.j2 dest=/etc/httpd/conf/httpd.confnotify:- restart httpd- name: create root dirfile: path=/etc/httpd/htdocs state=directory- name: start httpd serverservice: name={{service}} enabled=true state=startedhandlers:- name: restart httpdservice: name={{service}} state=restartedansiable-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 variableshosts: dbserversremote_user: rootvars:filename: abc.txttasks:- name: position 1debug:msg: 'ls /opt'tags:- only- name: position 2debug:msg: 'ls /mnt'ansible-playbook test1.yaml --tags="only"
6.2 多标签的运用
---
- name: this is a play for testing variableshosts: dbserversremote_user: rootvars:filename: abc.txttasks:- name: position 1debug:msg: '测试标签1'tags:- one- name: position 2debug:msg: '测试标签2'tags:- two- name: position 3debug:msg: '测试标签3'tags:- one
6.3 通用标签always的运用
---
- name: this is a play for testing variableshosts: dbserversremote_user: rootvars:filename: abc.txttasks:- name: position 1debug:msg: '测试标签1'tags:- one- name: position 2debug:msg: '测试通用标签always'tags:- always- name: position 3debug:msg: '测试标签3'tags:- one