目录
- palybooks 组成
- 示例:
- 运行palybook
- 定义,引用变量
- when 条件判断
- 循环
- Templates模块
- tags 模块
palybooks 组成
- Tasks:任务,即通过 task 调用 ansible 的模板将多个操作组织在一个 playbook 中运行
- Variables:变量
- Templates:模板
- Handlers:处理器,当changed状态条件满足时,(notify)触发执行的操作
- Roles:角色
示例:
vim test1.yaml
---
- name: first play gather_facts: false hosts: webservers remote_user: root tasks: - name: test connection ping: - name: disable selinuxcommand: '/sbin/setenforce 0' ignore_errors: True - name: disable firewalldservice: name=firewalld state=stopped - name: install httpdyum: name=httpd state=latest- name: install configuration file for httpdcopy: src=/opt/httpd.conf dest=/etc/httpd/conf/httpd.conf notify: "restart httpd" - name: start httpd serviceservice: enabled=true name=httpd state=startedhandlers: - name: restart httpd service: name=httpd state=restarted
运行palybook
ansible-playbook test1.yaml
//补充参数:
-k(–ask-pass):用来交互输入ssh密码
-K(-ask-become-pass):用来交互输入sudo密码
-u:指定用户
ansible-playbook test1.yaml --syntax-check
ansible-playbook test1.yaml --list-task
ansible-playbook test1.yaml --list-hosts
ansible-playbook test1.yaml --start-at-task='install httpd'
定义,引用变量
- name: second playhosts: dbserversremote_user: rootvars: - groupname: mysql - username: nginxtasks:- name: create groupgroup: name={{groupname}} system=yes gid=306 - name: create useruser: name={{username}} uid=306 group={{groupname}} - name: copy filecopy: content="{{ansible_default_ipv4}}" dest=/opt/vars.txt ansible-playbook test1.yaml -e "username=nginx"
//指定远程主机sudo切换用户
---
- hosts: dbserversremote_user: zhangsan become: yes become_user: root
执行playbook时:ansible-playbook test1.yml -k -K
when 条件判断
//when条件判断
在Ansible中,提供的唯一一个通用的条件判断是when指令,当when指令的值为true时,则该任务执行,否则不执行该任务。//when一个比较常见的应用场景是实现跳过某个主机不执行任务或者只有满足条件的主机执行任务
vim test2.yaml
---
- hosts: allremote_user: roottasks:- name: shutdown host command: /sbin/shutdown -r nowwhen: ansible_default_ipv4.address == "192.168.80.12"
或 when: inventory_hostname == "<主机名>"ansible-playbook test2.yaml
循环
//迭代
Ansible提供了很多种循环结构,一般都命名为with_items,作用等同于 loop 循环。
vim test3.yaml
---
- name: play1hosts: dbserversgather_facts: falsetasks: - name: create filefile:path: "{{item}}"state: touchwith_items: [ /opt/a, /opt/b, /opt/c, /opt/d ]- name: play2hosts: dbserversgather_facts: false vars:test:- /tmp/test1- /tmp/test2- /tmp/test3- /tmp/test4tasks: - name: create directoriesfile:path: "{{item}}"state: directorywith_items: "{{test}}"- name: play3hosts: dbserversgather_facts: falsetasks:- name: add usersuser: name={{item.name}} state=present groups={{item.groups}}with_items:- name: test1groups: wheel- name: test2groups: root
或with_items:- {name: 'test1', groups: 'wheel'}- {name: 'test2', groups: 'root'}ansible-playbook test3.yaml
Templates模块
Jinja是基于Python的模板引擎。Template类是Jinja的一个重要组件,可以看作是一个编译过的模板文件,用来产生目标文本,传递Python的变量给模板去替换模板中的标记。1.先准备一个以 .j2 为后缀的 template 模板文件,设置引用的变量
cp /etc/httpd/conf/httpd.conf /opt/httpd.conf.j2vim /opt/httpd.conf.j2
Listen {{http_port}}
ServerName {{server_name}}
DocumentRoot "{{root_dir}}" 2.修改主机清单文件,使用主机变量定义一个变量名相同,而值不同的变量
vim /etc/ansible/hosts
[webservers]
192.168.80.11 http_port=192.168.80.11:80 server_name=www.accp.com:80 root_dir=/etc/httpd/htdocs[dbservers]
192.168.80.12 http_port=192.168.80.12:80 server_name=www.benet.com:80 root_dir=/etc/httpd/htdocs3.编写 playbook
vim apache.yaml
---
- hosts: allremote_user: rootvars:- package: httpd- service: httpdtasks:- name: install httpd packageyum: name={{package}} state=latest- name: install configure filetemplate: src=/opt/httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf notify:- 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=restartedansible-playbook apache.yaml
tags 模块
可以在一个playbook中为某个或某些任务定义“标签”,在执行此playbook时通过ansible-playbook命令使用--tags选项能实现仅运行指定的tasks。
playbook还提供了一个特殊的tags为always。作用就是当使用always作为tags的task时,无论执行哪一个tags时,定义有always的tags都会执行。vim webhosts.yaml
---
- hosts: webserversremote_user: roottasks:- name: Copy hosts filecopy: src=/etc/hosts dest=/opt/hoststags:- only - name: touch filefile: path=/opt/testhost state=touchtags:- always ansible-playbook webhosts.yaml --tags="only"vim dbhosts.yaml
---
- hosts: dbserversremote_user: roottasks:- name: Copy hosts filecopy: src=/etc/hosts dest=/opt/hoststags:- only- name: touch filefile: path=/opt/testhost state=touchansible-playbook dbhosts.yaml --tags="only"
//分别去两台被管理主机上去查看文件创建情况