标签:控制 setup 修改 ems template role serve 服务器 回滚
PlayBook介绍1、tasks:任务,即调用模块完成的操作
2、variables:变量
3、templates:模板
4、handlers:处理器,当条件满足时执行操作,通常前面使用notify声明。
5、roles:角色,分门别类管理playbook的一种方式,后面将详细介绍roles的用法。
vim /opt/test.yaml
- hosts: webserver //定义的主机组,即应用的主机
vars: //定义变量
http_port: 80 //此处自定义变量中变量写在yaml文件,也可写在ansible/hosts文件中应用主机后
max_clients: 200
remote_user: root //远程用户为root
tasks: //执行的任务
- name: ensure apache is at the latest version //下面即将执行的步骤名称
yum: pkg=httpd state=latest
- name: write the apache config file //下面即将执行的步骤名称
template: src=/srv/httpd.j2 dest=/etc/httpd.conf //模板文件,src为路径为控制端,dest为被控制端
notify: //声明需要执行的操作
- restart apache //操作名称
- name: ensure apache is running
service: name=httpd state=started
handlers: //处理器,处理名称需要对应notify的名称,才能自动执行
- name: restart apache
service: name=httpd state=restarted
ansible-playbook test.yaml --syntax-check #检查yaml文件的语法是否正确
ansible-playbook test.yaml --list-task #检查tasks任务
ansible-playbook test.yaml --list-hosts #检查生效的主机
ansible-playbook test.yaml --start-at-task=‘Copy Nginx.conf‘ #指定从某个task开始运行
--- #表示该文件是YAML文件,非必须
- hosts: webserver #指定主机组,可以是一个或多个组。
remote_user: root #指定远程主机执行的用户名
还可以为每个任务定义远程执行用户:
---
- hosts: mysql
remote_user: root
tasks:
- name: test connection
ping:
remote_user: mysql #指定远程主机执行tasks的运行用户为mysql
ansible-playbook ping.yaml
---
- hosts: mysql
remote_user: root
become: yes #2.6版本以后的参数,之前是sudo,意思为切换用户运行
become_user: mysql #指定sudo用户为mysql
Handlers也是一些task的列表,和一般的task并没有什么区别。是由通知者进行的notify,如果没有被notify,则Handlers不会执行,假如被notify了,则Handlers被执行,不管有多少个通知者进行了notify,等到play中的所有task执行完成之后,handlers也只会被执行一次。
- hosts: webserver
remote_user: root
tasks:
- name: install httpd package
yum: name=httpd state=latest
- name: install configuration file for httpd
copy: src=/opt/httpd.conf dest=/etc/httpd/conf/httpd.conf
notify: //声明后面Handlers的名称
-restart httpd //注意该名称为notify的子集,需要靠空格隔开
- name: start httpd service
service: enabled=true name=httpd state=started
handlers:
- name: restart httpd
service: name=httpd state=restarted
也可以引入变量
---
- hosts: webserver
remote_user: root
vars: //引入变量
- package: httpd //变量名称
- service: httpd
tasks:
- name: install httpd package
yum: name={{package}} state=latest //引入变量方式
- name: install configuration file for httpd
copy: src=/opt/httpd.conf dest=/etc/httpd/conf/httpd.conf
notify:
-restart httpd
- name: start httpd service
service: enabled=true name={{service}} state=started
handlers:
- name: restart httpd //handlers执行的操作必须是notify中声明的操作名称,若此处执行的操作名称notify中没有声明,那么handlers不会执行
service: name={{service}} state=restarted
在引入变量过程中,若针对一个组里不同主机需要引入的变量值不相同时,比如IP地址等,则需要在hosts文件中,在不同主机后面跟上变量名称与值,yaml文件中执行操作时引入变量名称即可。
vim /etc/ansible/hosts
[webserver]
192.168.144.110 httpd_port="192.168.144.110:80" server_name="www.yun01.com:80"
192.168.144.111 httpd_port="192.168.144.111:80" server_name="www.yun02.com:80"
Jinja是基于Python的模板引擎。template类是jinja的另一个重要组件,可以看做是编译过的模板文件,用来生产目标文件,传递Python的变量给模板去替换模板中的标记。
cp /etc/httpd/conf/httpd.conf /opt/httpd.conf.j2 复制模板,注意文件格式为j2结尾
vim /opt/httpd.conf.j2
Listen {{httpd_port}} //为需要修改的部分设定变量
...
ServerName {{server_name}}
vim /etc/ansible/hosts
[webserver]
192.168.144.110 httpd_port="192.168.144.110:80" server_name="www.yun01.com:80"
192.168.144.111 httpd_port="192.168.144.111:80" server_name="www.yun02.com:80"
vim /opt/httpd.yaml
- hosts: webserver //针对webserver组执行
vars:
package: httpd //变量名称
server: httpd
tasks:
- name: install httpd
yum: name={{package}} state=latest
- name: write the apache config file
template: src=/opt/httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf //注意模板文件位置
notify:
restart httpd
- name: start httpd
service: name={{server}} enabled=true state=started
handlers:
- name: restart httpd
service: name={{server}} state=restarted
vi when.yml
---
- hosts: mysql
remote_user: root
tasks:
- name: "shutdown CentOS"
command: /sbin/shutdown -h now
when: ansible_distribution == "CentOS"
vi when.yml
---
- hosts: mysql
remote_user: root
tasks:
- name: "shut down CentOS 7 systems"
command: /sbin/shutdown -r now
when:
- ansible_distribution == "CentOS"
- ansible_distribution_major_version == "7"
vi when.yml
---
- hosts: mysql
remote_user: root
tasks:
- name: "shut down CentOS 6 and Debian 7 systems"
command: /sbin/shutdown -t now
when: (ansible_distribution == "CentOS" and ansible_distribution_major_version == "6") or
(ansible_distribution == "Debian" and ansible_distribution_major_version == "7")
vi when.yml
---
- hosts: all
vars:
exist: "True"
tasks:
- name: creaet file
command: touch /tmp/test.txt
when: exist | match("True")
- name: delete file
command: rm -rf /tmp/test.txt
when: exist | match("False")
---
- hosts: webserver
remote_user: root
tasks:
- name: "Install Packages"
yum: name={{ item }} state=latest
with_items:
- httpd
- mysql-server
- php
---
- hosts: webserver
remote_user: root
tasks:
- name: "Add users"
user: name={{ item.name }} state=present groups={{ item.groups }}
with_items:
- { name:‘test1‘, groups:‘wheel‘}
- { name:‘test2‘, groups:‘root‘}
vi hosts.yml
---
- hosts: webserver
remote_user: root
tasks:
- name: Copy hosts file
copy: src=/etc/hosts dest=/etc/hosts
tags:
- only
- name: touch file
file: path=/opt/hosts state=touch
执行命令:
ansible-playbook hosts.yml --tags="only"
ansible-playbook hosts.yml
vi hosts.yml
---
- hosts: webserver
remote_user: root
tasks:
- name: Copy hosts file
copy: src=/etc/hosts dest=/etc/hosts
tags:
- only
- name: copy test file
copy: src=/etc/test.txt dest=/opt/test.txt
- name: touch file
file: path=/opt/hosts state=touch
tags:
- always
执行命令:
ansible-playbook hosts.yml --tags="only"
标签:控制 setup 修改 ems template role serve 服务器 回滚
原文地址:http://blog.51cto.com/13659253/2155021