33-5 ansible playbook组件:任务列表handlers案例
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了33-5 ansible playbook组件:任务列表handlers案例相关的知识,希望对你有一定的参考价值。
管理端:192.168.1.131 Centos7.2
node1: 1.121 Centos6.7
node2: 1.122 Centos6.7
node3: 1.123 Centos6.7
[[email protected] ~]# yum -y install ansible #需要安装EPEL源
[[email protected] ~]# ssh-keygen -t rsa -P ‘‘
[[email protected] ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub 192.168.1.131 #管理本机
[[email protected] ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub 192.168.1.121
[[email protected] ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub 192.168.1.122
[[email protected] ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub 192.168.1.123
[[email protected] ~]# cd /etc/ansible/
[[email protected] ansible]# cp hosts{,.bak}
[[email protected] ansible]# vim hosts
添加
[websrvs]
192.168.1.121
192.168.1.122
[dbsrvs]
192.168.1.123
测试
1、在指定主机组上:创建nginx组、创建nginx用户、复制文件
[[email protected] ~]# vim nginx.yml
- hosts: websrvs
remote_user: root
tasks:
- name: create ninx group
group: name=nginx system=yes gid=208
- name: create nginx
user: name=nginx uid=208 group=nginx system=yes
- hosts: dbsrvs
remote_user: root
tasks:
- name: copy file to dbsrvs
copy: src=/etc/inittab dest=/tmp/inittab.ansible
[[email protected] ~]# ansible-playbook nginx.yml
2、在指定主机组上:安装apahce、修改配置文件、启动apache服务
[[email protected] ~]# mkdir conf
[[email protected] ~]# cp /etc/httpd/conf/httpd.conf conf/
[[email protected] ~]# vim conf/httpd.conf
修改
Listen 80
为
Listen 8080
[[email protected] ~]# vim apache.yml
- hosts: websrvs
remote_user: root
tasks:
- name: install httpd package
yum: name=httpd state=latest
- name: install configuration file for httpd
copy: src=/root/conf/httpd.conf dest=/etc/httpd/conf/httpd.conf
- name: start httpd service
service: enabled=true name=httpd state=started
[[email protected] ~]# ansible-playbook apache.yml
3、执行上面操作后,将配置文件作了更改
[[email protected] ~]# vim apache.yml
- hosts: websrvs
remote_user: root
tasks:
- name: install httpd package
yum: name=httpd state=latest
- name: install configuration file for httpd
copy: src=/root/conf/httpd.conf dest=/etc/httpd/conf/httpd.conf
notify:
- restart httpd
- name: start httpd service
service: enabled=true name=httpd state=started
handlers:
- name: restart httpd
service: name=httpd state=restarted
[[email protected] ~]# ansible-playbook apache.yml
4、引入变量(功能同上)
[[email protected] ~]# vim apache.yml
- hosts: websrvs
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=/root/conf/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
service: name=httpd state=restarted
[[email protected] ~]# ansible-playbook apache.yml
5、使用ansible内置变量
[[email protected] ~]# vim test.yml
- hosts: websrvs
remote_user: root
tasks:
- name: copy file
copy: content="{{ ansible_all_ipv4_addresses }}" dest=/tmp/vars.ansi
[[email protected] ~]# ansible-playbook test.yml
6、自定义变量(主机内部变量)
[[email protected] ~]# vim /etc/ansible/hosts
修改后内容为:
[websrvs]
192.168.1.121 testvar="1.121"
192.168.1.122 testvar="1.122"
192.168.1.131
[dbsrvs]
192.168.1.123
[[email protected] ~]# vim test.yml
- hosts: websrvs
remote_user: root
tasks:
- name: copy file
copy: content="{{ ansible_all_ipv4_addresses }}, {{ testvar }}" dest=/tmp/vars.ansi
[[email protected] ~]# ansible-playbook test.yml
7、条件测试(向符合条件的主机添加用户)
[[email protected] ~]# vim cond.yml
- hosts: all
remote_user: root
vars:
- username: user10
tasks:
- name: create {{ username }} user
user: name={{ username }}
when: ansible_fqdn == "node1"
[[email protected] ~]# ansible-playbook cond.yml
8、templates示例
[[email protected] ~]# mkdir templates
[[email protected] ~]# cp conf/httpd.conf templates/
[[email protected] ~]# mv templates/httpd.conf templates/httpd.conf.j2
[[email protected] ~]# vim templates/httpd.conf.j2
修改
Listen 80
为
Listen {{ http_port }}
修改
MaxClients 256
为
MaxClients {{ maxClients }}
修改
#ServerName www.example.com:80
为
ServerName {{ ansible_fqdn }}
[[email protected] ~]# vim /etc/ansible/hosts
添加以下内容
[websrvs]
192.168.1.121 http_port=80 maxClients=100
192.168.1.122 http_port=8080 maxClients=100
[[email protected] ~]# vim apache.yml
- hosts: websrvs
remote_user: root
vars:
- package: httpd
- service: httpd
tasks:
- name: install httpd package
yum: name={{ package }} state=latest
- name: install configuration file for httpd
template: src=/root/templates/httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf
notify:
- restart httpd
- name: start httpd service
service: enabled=true name={{ service }} state=started
handlers:
- name: restart httpd
service: name=httpd state=restarted
[[email protected] ~]# ansible-playbook apache.yml
9、tags示例
[[email protected] ~]# vim apache.yml
添加tags标签
- hosts: websrvs
remote_user: root
vars:
- package: httpd
- service: httpd
tasks:
- name: install httpd package
yum: name={{ package }} state=latest
- name: install configuration file for httpd
template: src=/root/templates/httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf
tags:
- conf
notify:
- restart httpd
- name: start httpd service
service: enabled=true name={{ service }} state=started
handlers:
- name: restart httpd
service: name=httpd state=restarted
[[email protected] ~]# vim /etc/ansible/hosts
修改主机配置文件内容
[websrvs]
192.168.1.121 http_port=80 maxClients=150
192.168.1.122 http_port=8080 maxClients=180
[[email protected] ~]# ansible-playbook apache.yml --tags="conf"
10、roles示例
[[email protected] ~]# mkdir -pv ansible_playbooks/roles/{websrvs,dbsrvs}/{tasks,files,templates,meta,handlers,vars}
[[email protected] ~]# tree ansible_playbooks/
ansible_playbooks/
└── roles
├── dbsrvs
│?? ├── files
│?? ├── handlers
│?? ├── meta
│?? ├── tasks
│?? ├── templates
│?? └── vars
└── websrvs
├── files
├── handlers
├── meta
├── tasks
├── templates
└── vars
15 directories, 0 files
[[email protected] ~]# cd ansible_playbooks/
[[email protected] ansible_playbooks]# cd roles/websrvs/
[[email protected] websrvs]# cp /etc/httpd/conf/httpd.conf files/
[[email protected] websrvs]# vim tasks/main.yml
- name: install httpd package
yum: name=httpd
- name: install configuration file
copy: src=httpd.conf dest=/etc/httpd/conf/httpd.conf
tags:
- conf
notify:
- restart httpd
- name: start httpd
service: name=httpd state=started
[[email protected] websrvs]# vim handlers/main.yml
- name: restart httpd
service: name=httpd state=restarted
[[email protected] websrvs]# cd ../..
[[email protected] ansible_playbooks]# vim site.yml
- hosts: 192.168.1.121
remote_user: root
roles:
- websrvs
- hosts: 192.168.1.122
remote_user: root
roles:
- dbsrvs
- hosts: 192.168.1.123
remote_user: root
roles:
- websrvs
- dbsrvs
[[email protected] ~]# cd ansible_playbooks/roles/dbsrvs/
[[email protected] dbsrvs]# cp /etc/my.cnf files/
[[email protected] dbsrvs]# vim tasks/main.yml
- name: install mysql-server package
yum: name=mysql-server state=latest
- name: install configuration file
copy: src=my.cnf dest=/etc/my.cnf
tags:
- myconf
notify:
- restart mysqld
- name: start mysqld service
service: name=mysqld enabled=true state=started
[[email protected] dbsrvs]# vim handlers/main.yml
- name: restart mysqld
service: name=mysqld state=restarted
[[email protected] ansible_playbooks]# ansible-playbook site.yml
本文出自 “追梦” 博客,请务必保留此出处http://sihua.blog.51cto.com/377227/1851047
以上是关于33-5 ansible playbook组件:任务列表handlers案例的主要内容,如果未能解决你的问题,请参考以下文章