码迷,mamicode.com
首页 > 其他好文 > 详细

33-5 ansible playbook组件:任务列表、handlers、案例

时间:2016-09-09 19:03:32      阅读:314      评论:0      收藏:0      [点我收藏+]

标签:ansible   linux   playbook   

管理端:192.168.1.131 Centos7.2

node1: 1.121 Centos6.7

node2: 1.122 Centos6.7

node3: 1.123 Centos6.7

[root@server ~]# yum -y install ansible #需要安装EPEL源

[root@server ~]# ssh-keygen -t rsa -P ‘‘

[root@server ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub 192.168.1.131 #管理本机

[root@server ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub 192.168.1.121

[root@server ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub 192.168.1.122

[root@server ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub 192.168.1.123

[root@server ~]# cd /etc/ansible/

[root@server ansible]# cp hosts{,.bak}

[root@server ansible]# vim hosts

添加

[websrvs]

192.168.1.121

192.168.1.122


[dbsrvs]

192.168.1.123


测试

1、在指定主机组上:创建nginx组、创建nginx用户、复制文件

[root@server ~]# 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

[root@server ~]# ansible-playbook nginx.yml 

2、在指定主机组上:安装apahce、修改配置文件、启动apache服务

[root@server ~]# mkdir conf

[root@server ~]# cp /etc/httpd/conf/httpd.conf conf/

[root@server ~]# vim conf/httpd.conf 

修改

Listen 80

Listen 8080

[root@server ~]# 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

[root@server ~]# ansible-playbook apache.yml

3、执行上面操作后,将配置文件作了更改

[root@server ~]# 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

[root@server ~]# ansible-playbook apache.yml

4、引入变量(功能同上)

[root@server ~]# 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

[root@server ~]# ansible-playbook apache.yml

5、使用ansible内置变量

[root@server ~]# vim test.yml

- hosts: websrvs

 remote_user: root

 tasks:

 - name: copy file

copy: content="{{ ansible_all_ipv4_addresses }}" dest=/tmp/vars.ansi

[root@server ~]# ansible-playbook test.yml

6、自定义变量(主机内部变量)

[root@server ~]# 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


[root@server ~]# vim test.yml 

- hosts: websrvs

 remote_user: root

 tasks:

 - name: copy file

copy: content="{{ ansible_all_ipv4_addresses }}, {{ testvar }}" dest=/tmp/vars.ansi


[root@server ~]# ansible-playbook test.yml

7、条件测试(向符合条件的主机添加用户)

[root@server ~]# vim cond.yml

- hosts: all

 remote_user: root

 vars:

 - username: user10

 tasks:

 - name: create {{ username }} user

user: name={{ username }}

when: ansible_fqdn == "node1"

[root@server ~]# ansible-playbook cond.yml

8、templates示例

[root@server ~]# mkdir templates

[root@server ~]# cp conf/httpd.conf templates/

[root@server ~]# mv templates/httpd.conf templates/httpd.conf.j2

[root@server ~]# vim templates/httpd.conf.j2 

修改 

Listen 80

Listen {{ http_port }}

修改 

MaxClients       256

MaxClients       {{ maxClients }}

修改

#ServerName www.example.com:80

ServerName {{ ansible_fqdn }}


[root@server ~]# vim /etc/ansible/hosts

添加以下内容

[websrvs]

192.168.1.121 http_port=80 maxClients=100

192.168.1.122 http_port=8080 maxClients=100

[root@server ~]# 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

[root@server ~]# ansible-playbook apache.yml 


9、tags示例

[root@server ~]# 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


[root@server ~]# vim /etc/ansible/hosts

修改主机配置文件内容

[websrvs]

192.168.1.121 http_port=80 maxClients=150

192.168.1.122 http_port=8080 maxClients=180


[root@server ~]# ansible-playbook apache.yml --tags="conf"

10、roles示例

[root@server ~]# mkdir -pv ansible_playbooks/roles/{websrvs,dbsrvs}/{tasks,files,templates,meta,handlers,vars}

[root@server ~]# tree ansible_playbooks/

ansible_playbooks/

└── roles

    ├── dbsrvs

    │?? ├── files

    │?? ├── handlers

    │?? ├── meta

    │?? ├── tasks

    │?? ├── templates

    │?? └── vars

    └── websrvs

        ├── files

        ├── handlers

        ├── meta

        ├── tasks

        ├── templates

        └── vars


15 directories, 0 files

[root@server ~]# cd ansible_playbooks/

[root@server ansible_playbooks]# cd roles/websrvs/

[root@server websrvs]# cp /etc/httpd/conf/httpd.conf files/

[root@server 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

[root@server websrvs]# vim handlers/main.yml

- name: restart httpd

 service: name=httpd state=restarted

[root@server websrvs]# cd ../..

[root@server 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


[root@server ~]# cd ansible_playbooks/roles/dbsrvs/

[root@server dbsrvs]# cp /etc/my.cnf files/

[root@server 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 

[root@server dbsrvs]# vim handlers/main.yml

- name: restart mysqld

 service: name=mysqld state=restarted  

[root@server ansible_playbooks]# ansible-playbook site.yml   


本文出自 “追梦” 博客,请务必保留此出处http://sihua.blog.51cto.com/377227/1851047

33-5 ansible playbook组件:任务列表、handlers、案例

标签:ansible   linux   playbook   

原文地址:http://sihua.blog.51cto.com/377227/1851047

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!