1.Ansible-playbooks由哪些部分组成
- Tasks:任务,即通过 task 调用 ansible 的模板将多个操作组织在一个 playbook 中运行
- Variables:变量
- Templates:模板
- Handlers:处理器,当changed状态条件满足时,(notify)触发执行的操作
- Roles:角色
2.自己编写playbooks
2.1编写一个http的安装剧本
vim test1.yaml
--- #yaml文件以---开头,以表明这是一个yaml文件,可省略
- name: first play #定义一个play的名称,可省略
gather_facts: false #设置不进行facts信息收集,这可以加快执行速度,可省略
hosts: webservers #指定要执行任务的被管理主机组,如多个主机组用冒号分隔
remote_user: root #指定被管理主机上执行任务的用户
tasks: #定义任务列表,任务列表中的各任务按次序逐个在hosts中指定的主机上执行
- name: test connection #自定义任务名称
ping: #使用 module: [options] 格式来定义一个任务
- name: disable selinux
command: '/sbin/setenforce 0' #command模块和shell模块无需使用key=value格式
ignore_errors: True #如执行命令的返回值不为0,就会报错,tasks停止,可使用ignore_errors忽略失败的任务
- name: disable firewalld
service: name=firewalld state=stopped #使用 module: options 格式来定义任务,option使用key=value格式
- name: install httpd
yum: name=httpd state=latest
- name: install configuration file for httpd
copy: src=/opt/httpd.conf dest=/etc/httpd/conf/httpd.conf #这里需要一个事先准备好的/opt/httpd.conf文件
notify: "restart httpd" #如以上操作后为changed的状态时,会通过notify指定的名称触发对应名称的handlers操作
- name: start httpd service
service: enabled=true name=httpd state=started
handlers: #handlers中定义的就是任务,此处handlers中的任务使用的是service模块
- name: restart httpd #notify和handlers中任务的名称必须一致
service: name=httpd state=restarted
##Ansible在执行完某个任务之后并不会立即去执行对应的handler,而是在当前play中所有普通任务都执行完后再去执行handler,这样的好处是可以多次触发notify,但最后只执行一次对应的handler,从而避免多次重启。
2.2 编写nginx的yum安装剧本
---
- name: nginx play
hosts: webservers
remote_user: root
gather_facts: false
tasks:
- name: disable firewalld
service: name=firewalld state=stopped
- name: disable selinux
command: '/usr/sbin/setenforce 0'
ignore_errors: true
- name: copy epel.repo file
copy: src=/etc/yum.repos.d/epel.repo dest=/etc/yum.repos.d/epel.repo
- name: copy nginx.repo file
copy: src=/etc/yum.repos.d/nginx.repo dest=/etc/yum.repos.d/nginx.repo
- name: install nginx service
yum: name=nginx state=latest
- name: copy nginx configuration file
copy: src=/etc/nginx/nginx.conf dest=/etc/nginx/nginx.conf
notify: "reload nginx"
- name: start nginx service
service: name=nginx state=started enabled=yes
handlers:
- name: reload nginx
service: name=nginx state=reloaded
2.3 运行playbook
ansible-playbook test1.yaml
但是我们在运行剧本的时候会先使用其他命令来检测yaml文件
ansible-playbook test1.yaml --syntax-check #检查yaml文件的语法是否正确
ansible-playbook test1.yaml --list-task #检查tasks任务
ansible-playbook test1.yaml --list-hosts #检查生效的主机
3.定义、引用变量
- name: second play
hosts: dbservers
remote_user: root
vars: #定义变量
- groupname: mysql #格式为 key: value
- username: nginx
tasks:
- name: create group
group: name={{groupname}} system=yes gid=306 #使用 {{key}} 引用变量的值
- name: create user
user: name={{username}} uid=306 group={{groupname}}
- name: copy file
copy: content="{{ansible_default_ipv4}}" dest=/opt/vars.txt #在setup模块中可以获取facts变量信息
4.指定远程主机sudo切换用户
---
- name: test play
hosts: dbservers
remote_user: lzq
become: yes
become_user: root
vars:
- dirname: /opt/ky31
- filename: /opt/lzq.txt
tasks:
- name: create dir
file: path={{dirname}} state=directory
- name: create file
file: path={{filename}} state=touch
- name: copy file
copy: content="{{ansible_default_ipv4}}" dest=/opt/ansible.txt
5.如何使用when做条件判断
在Ansible中,提供的唯一一个通用的条件判断是when指令,当when指令的值为true时,则该任务执行,否则不执行该任务。
when一个比较常见的应用场景是实现跳过某个主机不执行任务或者只有满足条件的主机执行任务
---
- name: secend play
hosts: all
remote_user: root
vars:
- dirname: /var/www/html
tasks:
- name: create dir
file: path={{dirname}} state=directory
when: ansible_default_ipv4.address == "192.168.223.101"
#when进行判断如果这个变量等于192.168.223.101的话则创建dirname表示的目录
---
- name: secend play
hosts: all
remote_user: root
vars:
- dir1: /var/www/html
- dir2: /var/lzq/html
tasks:
- name: create dir in 101
file: path={{dir1}} state=directory
when: ansible_default_ipv4.address == "192.168.223.101"
- name: create dir in 102 and 103
file: path={{dir2}} state=directory
when: inventory_hostname != "192.168.223.101"
- name: create lzq file
copy: content="lzq like 111" dest=/var/www/html/lzq.html
when: ansible_default_ipv4.address == "192.168.223.101"
- name: create yhy file
copy: content="yhy like 111" dest=/var/lzq/html/yhy.html
when: ansible_default_ipv4.address == "192.168.223.102"
- name: create sys file
copy: content="sys like 111" dest=/var/lzq/html/sys.html
when: ansible_default_ipv4.address == "192.168.223.103"
6.迭代
Ansible提供了很多种循环结构,一般都命名为with_items,作用等同于 loop 循环。
---
- name: diedai play
hosts: dbservers
remote_user: root
tasks:
- name: create file
file: path={{item}} state=touch
with_items: ["/opt/a", "/opt/b", "/opt/c", "/opt/d"]
---
- name: diedai play
hosts: dbservers
remote_user: root
vars:
- myfiles: ["/opt/a", "/opt/b", "/opt/c", "/opt/d"]
- mydirs:
- /opt/lzq1
- /opt/lzq2
- /opt/lzq3
- /opt/lzq4
tasks:
- name: create file
file: path={{item}} state=touch
with_items: "{{myfiles}}"
- name: create dir
file: path={{item}} state=directory
with_items: "{{mydirs}}"
编写 ansible-playbook 安装 nginx 在三台主机上 agent01、agent02、agent03要求每台主机都创建 /var/www/ky31/ /var/www/ky32/ /var/www/ky33/ 目录并且要求每台主机在目录中创建 index.html 且内容各不相同
---
- name: nginx play
hosts: webservers
remote_user: root
gather_facts: false
tasks:
- name: disable firewalld
service: name=firewalld state=stopped
- name: disable selinux
command: '/usr/sbin/setenforce 0'
ignore_errors: true
- name: copy epel.repo file
copy: src=/etc/yum.repos.d/epel.repo dest=/etc/yum.repos.d/epel.repo
- name: copy nginx.repo file
copy: src=/etc/yum.repos.d/nginx.repo dest=/etc/yum.repos.d/nginx.repo
- name: install nginx service
yum: name=nginx state=latest
- name: copy nginx configuration file
copy: src=/etc/nginx/nginx.conf dest=/etc/nginx/nginx.conf
notify: "reload nginx"
- name: start nginx service
service: name=nginx state=started enabled=yes
- name: create dirs
file: path={{item}} state=directory
with_items: ["/var/www/ky31", "/var/www/ky32", "/var/www/ky33"]
- name: create file
copy: content={{item.neirong}} dest={{item.file}}
with_items:
- neirong: 111
file: /var/www/ky31/index.html
- neirong: 222
file: /var/www/ky32/index.html
- neirong: 333
file: /var/www/ky33/index.html
handlers:
- name: reload nginx
service: name=nginx state=reloaded