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   

时间: 2024-10-03 22:53:53

33-5 ansible playbook组件:任务列表、handlers、案例的相关文章

自动化运维工具ansible playbook和roles的使用

ansible的结构: Inventory 用来定义被控制端 Modules 定义被控制端可用的操作 Ad Hoc Commands 定义被控制端可以执行命令的 Playbook 批量运行的方式 Tasks: 任务:由各模块所支持执行的特定操作:可以通过ansible-doc module_name来查看帮助文档,非常详细 -m  user -a 'name= password=' Variables: 变量 Templates: 模板:(如执行httpd服务时,各节点上httpd的配置文件内容

ansible playbook使用总结

Ansible playbook的使用 playbooks概念 task: 任务:使用各模块所执行的特性操作任务,比如:-m user -a 'name= password=' Variables: 变量: Templates: 模板 在定义模板后可以实现各节点对应的变量来取代,表达式自身会根据当前节点所赋值做运算,之后生成的值则赋予这个参数,用于生产不同配置的配置文件,所以模板主要实现配置不同场景文本文件 而且这种使用模板语言来定义 模板语言中可以根据定义替换成特定主机的某些值 handler

Ansible PlayBook语法(4)

title: Ansible PlayBook语法(4) date: 2018-12-02 10:53:24 tags: Ansible categories: Ansible copyright: true --- Ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet.cfengine.chef.func.fabric)的优点,实现了批量系统配置.批量程序部署.批量运行命令等功能,ansible是基于模块工作的,本身没有批量部署的能力,真正具有批量部署

运维自动化之ansible playbook安装mysql

上次介绍了如何使用ansible playbook安装zabbix客户端(http://dl528888.blog.51cto.com/2382721/1436745),这次介绍一下如何使用playbook安装mysql. 下面是安装mysql的信息: mysql_basedir: /data/mysql/basedir                    源码目录 mysql_datadir: /data/mysql/datadir                    数据目录 mysql

运维自动化之ansible playbook安装ruby环境

本来不想打算写安装ruby的,但看几个puppet的群里有人对安装ruby比较茫然,所以这里简单介绍一下如何安装ruby. ps:话说现在也就gitlab.capistrano.puppet等软件使用ruby,最新2010年的软件好的都是python了,比如ansible.salt等. 下面是安装ruby的信息: ruby_version: 1.9.3 ruby_dir: /usr/local gem_version: 1.8.23 bundle_version: 1.6.3 可以看到ruby的

运维自动化之ansible playbook安装node环境

现在介绍如何使用ansible安装node. 下面是安装node的信息: node_dir: /data node_version: 0.10.21 node_port: 3301 可以看到node的版本是0.10.21,测试的node应用服务监听3301端口 备注:此playbook仅能对centos或者redhat的6.x版本进行安装. 下面是安装node的playbook结构 09:33:16 # tree node_* node_delete ├── files ├── handlers

运维自动化之ansible playbook结合docker安装smokeping

本次介绍ansible的paly book结合docker进行虚拟机里安装2.6.8版本smokeping(apache版本是2.4.7). docker版本 09:26:53 # docker version Client version: 0.11.1 Client API version: 1.11 Go version (client): go1.2.1 Git commit (client): fb99f99/0.11.1 Server version: 0.11.1 Server A

ansible使用二(ansible playbook)

ansible playbook ansible playbooks 是使用ansible简单的配置管理部署系统的基础,使用YAML语法格式的配置文件.每一个playbook中包含一系列的任务.使用ansible playbook可以更方便的管理.维护多态主机. 1.yaml语法    yaml是”YAML Ain't a Markup Language”的缩写,即不是一种标记性语言,它是一种直观的能够被电脑识别的数据序列化格式,是一个可读性高并且容易被人类阅读,容易和脚本语言交互,用来表达资料

Ansible playbook API 开发 调用测试

Ansible是Agentless的轻量级批量配置管理工具,由于出现的比较晚(13年)基于Ansible进行开发的相关文档较少,因此,这里通过一些小的实验,结合现有资料以及源码,探索一下Ansible的二次开发. 随笔的内容分为三个部分 playbook编辑执行 python 调用API执行playbook java调用python程序进行playbook的执行 实验的环境是centos6,ansible版本是1.9.4,python版本是2.6.6,jdk版本是1.7U79 一.playboo