Ansible playbooks(任务、角色、模板、变色器、)
playbooks配置文件:

[root@ansible ~]# vim /etc/ansible/hosts [test01]
192.168.200.114 [test02]
192.168.200.115
[root@ansible ~]# vim /etc/ansible/test.yaml ---
- hosts: test01 #要操作的主机
remote_user: root #登入的用户
tasks: #tasks表示任务
- name: adduser #任务名
user: name=user2 state=present #创建一个用户
tags: #设置一个标签
- testaaa
- name: addgroup
group: name=root system=yes
tags:
- testbbb
- hosts: test02
remote_user: root
tasks:
- name: xxx
copy: src=/etc/passwd dest=/home
tags:
- testccc
...

用法:
playbook文件定义的任务需要通过nasible-playbook命令调用并执行,ansible-playbook命令用法如下:
用法:ansible-playbook[option]/PATH/TO/PLAYBOOK.yaml

实验案例:
1:语法检查:
[root@ansible ~]# ansible-playbook --syntax-check /etc/ansible/test.yml playbook: /etc/ansible/test.yml
2:预测试:
[root@ansible ~]# ansible-playbook -C /etc/ansible/test.yml PLAY [test01] *************************************************************************************************************************** TASK [Gathering Facts] ******************************************************************************************************************
ok: [192.168.200.114] TASK [adduser] **************************************************************************************************************************
changed: [192.168.200.114] TASK [addgroup] *************************************************************************************************************************
ok: [192.168.200.114] PLAY [test02] *************************************************************************************************************************** TASK [Gathering Facts] ******************************************************************************************************************
ok: [192.168.200.115] TASK [xxx] ******************************************************************************************************************************
changed: [192.168.200.115] PLAY RECAP ******************************************************************************************************************************
192.168.200.114 : ok= changed= unreachable= failed=
192.168.200.115 : ok= changed= unreachable= failed=
3:列出主机:
[root@ansible ~]# ansible-playbook --list-hosts /etc/ansible/test.yml playbook: /etc/ansible/test.yml play # (test01): test01 TAGS: []
pattern: [u'test01']
hosts ():
192.168.200.114 play # (test02): test02 TAGS: []
pattern: [u'test02']
hosts ():
192.168.200.115
4:列出任务:
[root@ansible ~]# ansible-playbook --list-tasks /etc/ansible/test.yml playbook: /etc/ansible/test.yml play # (test01): test01 TAGS: []
tasks:
adduser TAGS: [testaaa]
addgroup TAGS: [testbbb] play # (test02): test02 TAGS: []
tasks:
xxx TAGS: [testccc]
5:列出标签:
[root@ansible ~]# ansible-playbook --list-tags /etc/ansible/test.yml playbook: /etc/ansible/test.yml play # (test01): test01 TAGS: []
TASK TAGS: [testaaa, testbbb] play # (test02): test02 TAGS: []
TASK TAGS: [testccc]
6:执行任务:
[root@ansible ~]# ansible-playbook /etc/ansible/test.yml PLAY [test01] *************************************************************************************************************************** TASK [Gathering Facts] ******************************************************************************************************************
ok: [192.168.200.114] TASK [adduser] **************************************************************************************************************************
ok: [192.168.200.114] TASK [addgroup] *************************************************************************************************************************
ok: [192.168.200.114] PLAY [test02] *************************************************************************************************************************** TASK [Gathering Facts] ******************************************************************************************************************
ok: [192.168.200.115] TASK [xxx] ******************************************************************************************************************************
ok: [192.168.200.115] PLAY RECAP ******************************************************************************************************************************
192.168.200.114 : ok= changed= unreachable= failed=
192.168.200.115 : ok= changed= unreachable= failed=
7:测试查看:
[root@ansible ~]# ansible test01 -m shell -a "tail -l /etc/passwd"
192.168.200.114 | SUCCESS | rc= >>
user2:x::::/home/user2:/bin/bash
[root@ansible ~]# ansible test02 -m command -a "ls -l /home"
192.168.200.115 | SUCCESS | rc=0 >>
总用量 4
drwx------. 3 crushlinx crushlinx 78 10月 28 09:53 crushlinx
-rw-r--r--. 1 root root 2304 10月 28 16:57 passwd
drwx------. 3 room room 78 4月 11 2018 room
8:执行输出:
我们在用playbook进行ansible模块操作的时候,并没有命令的执行结果输出,默认被隐藏了,可以通过refister模块追加命令输出的结果
[root@ansible ~]# vim /etc/ansible/test.yaml ---
- hosts: test01
remote_user: root
tasks:
- name: adduser
user: name=user2 state=present
register: print_result
tags:
- testaaa
- debug: var=print_result
- name: addgroup
group: name=root system=yes
tags:
- testbbb - debug: var=print_result
- hosts: test02
remote_user: root
tasks:
- name: xxx
copy: src=/etc/passwd dest=/home
tags:
- testccc
...
[root@ansible ~]# ansible-playbook /etc/ansible/test.yaml PLAY [test01] *************************************************************************************************************************** TASK [Gathering Facts] ******************************************************************************************************************
ok: [192.168.200.114] TASK [adduser] **************************************************************************************************************************
ok: [192.168.200.114] TASK [debug] ****************************************************************************************************************************
ok: [192.168.200.114] => {
"print_result": {
"append": false,
"changed": false,
"comment": "",
"failed": false,
"group": 1001,
"home": "/home/user2",
"move_home": false,
"name": "user2",
"shell": "/bin/bash",
"state": "present",
"uid": 1001
}
} TASK [addgroup] *************************************************************************************************************************
ok: [192.168.200.114] TASK [debug] ****************************************************************************************************************************
ok: [192.168.200.114] => {
"print_result": {
"append": false,
"changed": false,
"comment": "",
"failed": false,
"group": 1001,
"home": "/home/user2",
"move_home": false,
"name": "user2",
"shell": "/bin/bash",
"state": "present",
"uid": 1001
}
} PLAY [test02] *************************************************************************************************************************** TASK [Gathering Facts] ******************************************************************************************************************
ok: [192.168.200.115] TASK [xxx] ******************************************************************************************************************************
ok: [192.168.200.115] PLAY RECAP ******************************************************************************************************************************
192.168.200.114 : ok= changed= unreachable= failed=
192.168.200.115 : ok= changed= unreachable= failed=
client机安装httpd
[root@client2 ~]# yum install httpd httpd-server -y
[root@client2 httpd]# service httpd start
[root@client1 ~]# yum install httpd httpd-server -y
[root@client1 httpd]# service httpd start
2:触发器:

hanglers触发器的使用示例如下:
[root@ansible ~]# ansible test01 -m shell -a 'netstat -anpt | grep :80'
192.168.200.114 | SUCCESS | rc= >>
tcp 0.0.0.0: 0.0.0.0:* LISTEN /nginx: maste
[root@ansible ~]# vim /etc/ansible/httpd.yaml ---
- hosts: crushlinux
remote_user: root
tasks:
- name: change port
command: sed -i 's/Listen 80/Listen 8080/g' /etc/httpd/conf/httpd.conf
notify:
- restart httpd server
handlers:
- name: restart httpd server
service: name=httpd state=restarted
...

测试语法:
[root@ansible ~]# ansible-playbook -C /etc/ansible/httpd.yaml PLAY [crushlinux] *********************************************************************************************************************** TASK [Gathering Facts] ******************************************************************************************************************
ok: [192.168.200.115]
ok: [192.168.200.114] TASK [change port] **********************************************************************************************************************
skipping: [192.168.200.115]
skipping: [192.168.200.114] PLAY RECAP ******************************************************************************************************************************
192.168.200.114 : ok= changed= unreachable= failed=
192.168.200.115 : ok= changed= unreachable= failed=
查看配置文件内容并执行:
[root@ansible ~]# ansible "test01" -m shell -a 'grep ^Listen /etc/httpd/conf/httpd.conf'
192.168.200.114 | SUCCESS | rc=0 >>
Listen 80
[root@ansible ~]# ansible-playbook /etc/ansible/httpd.yaml
PLAY [crushlinux] ***********************************************************************************************************************
TASK [Gathering Facts] ******************************************************************************************************************
ok: [192.168.200.115]
ok: [192.168.200.114]
TASK [change port] **********************************************************************************************************************
[WARNING]: Consider using template or lineinfile module rather than running sed
可以忽略或使用模板lineinfile模块而不是运行sed
changed: [192.168.200.115]
changed: [192.168.200.114]
RUNNING HANDLER [restart httpd server] **************************************************************************************************
changed: [192.168.200.115]
changed: [192.168.200.114]
PLAY RECAP ******************************************************************************************************************************
192.168.200.114 : ok=3 changed=2 unreachable=0 failed=0
192.168.200.115 : ok=3 changed=2 unreachable=0 failed=0
查看结果:
[root@ansible ~]# ansible test01 -m shell -a 'netstat -anpt | grep httpd'
192.168.200.114 | SUCCESS | rc=0 >>
tcp6 0 0 :::8080 :::* LISTEN 9209/httpd
[root@ansible ~]# ansible crushlinux -m shell -a 'netstat -anpt | grep httpd'
192.168.200.114 | SUCCESS | rc=0 >>
tcp6 0 0 :::8080 :::* LISTEN 9209/httpd
192.168.200.115 | SUCCESS | rc=0 >>
tcp6 0 0 :::8080 :::* LISTEN 12029/httpd
基于Ansible playbook配置zabbix agent端:zabbix是一种监控器,用于监控client端
[root@ansible ~]# vim zabbix-agent.sh #!/bin/bash
NH=$(hostname)
if [ ! -f /etc/yum.repos.d/zabbix.repo ]
then
rpm -Uvh
http://repo.zabbix.com/zabbix/3.2/rhel/7/x86_64/zabbix-release-3.2-1.el7.noarch.rpm
fi
rpm -q zabbix-agent &>/dev/null
[ $? -ne ] && yum -y -q install zabbix-agent
cp /etc/zabbix/zabbix_agentd.conf{,-$(date +%F%T)}
sed -i 's/Server=127.0.0.1/Server=192.168.200.113/g' /etc/zabbix/zabbix_agentd.conf
sed -i 's/ServerActive=127.0.0.1/ServerActive=192.168.200.113/g'
/etc/zabbix//zabbix_agentd.conf
sed -i 's/Hostname=Zabbix server/Hostname=$HN/g' /etc/zabbix/zabbix_agentd.conf
systemctl | restart zabbix-agent
3:角色



实验案例:
通过一个实验配置数据库角色,要求被管理端主机自动安装Mariadb,安装后上传提前装备好的配置文件到远端主机,重启服务,然后新建testdb数据库,并允许test用户对其拥有所有权限:
1:被管理端配置yum源:
[root@client2 ~]# cd /etc/yum.repos.d/
[root@client2 yum.repos.d]# ls
a epel.repo epel-testing.repo local.repo
[root@client1 ~]# cd /etc/yum.repos.d/
[root@client2 yum.repos.d]# ls
a epel.repo epel-testing.repo local.repo
2:配置数据库角色:
[root@ansible ~]# mkdir -pv /etc/ansible/roles/mariadb/{file,tasks,handlers}
mkdir: 已创建目录 "/etc/ansible/roles/mariadb"
mkdir: 已创建目录 "/etc/ansible/roles/mariadb/file"
mkdir: 已创建目录 "/etc/ansible/roles/mariadb/tasks"
mkdir: 已创建目录 "/etc/ansible/roles/mariadb/handlers"
[root@ansible ~]# cd /etc/ansible/
[root@ansible ansible]# vim /etc/ansible/mariadb.yaml
---
- hosts: crushlinux
remote_user: root
roles:
- mariadb
...
[root@ansible ansible]# cd /etc/ansible/roles/mariadb/
[root@ansible mariadb]# ls
file handlers tasks
[root@ansible mariadb]# cd tasks/
[root@ansible tasks]# vim main.yaml
---
- name: install mariadb #指定任务名称安装mariadb数据库
yum: name=mariadb-server state=present #执行yum模块安装mariadb
- name: move config file #指定任务名称移除原有配置文件
shell: "[ -e /etc/my.cnf ] && mv /etc/my.cnf /etc/my.cnf.bak" #进行判断,如果有就移除
- name: provide a new config file #创建一个新的配置文件
copy: src=my.cnf dest=/etc/my.cnf #src源会自动到file文件中查找my.cnf配置文件
- name: reload mariadb #指定任务名称重启mariadb
shell: systemctl restart mariadb #hsell添加模块重启mariadb
- name: create database testdb #指定要求添加mysql语句
shell: mysql -u root -e "create database testdb;grant all privileges on testdb.* to 'test@'192.168.200.%' identified by 'test123';flush privileges;"
notify: #通知触发器
- restart mariadb #重启mariadb
...
[root@ansible handlers]# vim main.yml
---
- name: restart mariadb #引用上面的触发器
server: name=mariadb state=restarted #触发后重启mariadb服务
...
[root@ansible handlers]# cd ../file/
[root@ansible file]# cp /etc/my.cnf /etc/ansible/roles/mariadb/file/
[root@ansible file]# ls
my.cnf #准备my.cnf文件
[root@ansible file]# cd /etc/ansible/
[root@ansible ansible]# tree
.
├── ansible.cfg
├── hosts
├── hosts.bak
├── httpd.yaml
├── mariadb.yaml
├── roles
│ └── mariadb
│ ├── file
│ │ └── my.cnf
│ ├── handlers
│ │ └── main.yaml
│ └── tasks
│ └── main.yaml
└── test.yaml
预执行:
[root@ansible mariadb]# ansible-playbook -C /etc/ansible/mariadb.yaml
Ansible playbooks(任务、角色、模板、变色器、)的更多相关文章
- 3、Ansible playbooks(Hosts、Users、tasks、handlers、变量、条件测试(when、迭代)、templates)
Ansible playbooks playbook是由一个或多个“play”组成的列表.play的主要功能在于将事先归并为一组的主机装扮成事先通过ansible中的task定义好的角色.从根本上来讲 ...
- Ansible Playbooks 介绍 和 使用 一
目录 Ansible Playbooks Playbooks 组成部分: YAML 介绍 YAML 语法 Ansible 基础元素 变量 facts registre 通过命令传递变量 通过roles ...
- Ansible Playbooks 介绍 和 使用 二
目录 handlers playbook 案例 2 handlers vars 变量 setup facts 变量使用 案例 inventory 中定义变量 案例 条件测试 when 语句 案例 迭代 ...
- Sql Server系列:Microsoft SQL Server Management Studio模板资源管理器
模板资源管理器是Microsoft SQL Server Management Studio的一个组件,可以用来SQL代码模板,使用模板提供的代码,省去每次都要输入基本代码的工作. 使用模板资源管理器 ...
- vert.x学习(四),使用模板解析器ClassLoaderTemplateResolver
在vert.x中使用模板解析,可以为我们带来很多方便.我这里学习了一下ClassLoaderTemplateResolver的简单使用.这次工程配置与上篇一样,不需要做任何多的配置.直接编写代码就可以 ...
- Knockout学习之模板绑定器
模板绑定器 如今页面结构越来越复杂,仅仅依靠foreach已经不足以我们的使用,这个时候我们就需要模板的存在,模板的优点自然很多,首先会让页面整洁,同时修改起来也可以方面的定位,最重要的是ko可以条件 ...
- CI 模板解析器类
模板解析器类可以解析你的视图文件中的伪变量.它可以解析简单的变量或者以变量作为标签的结构.如果你以前没有用过模板引擎,那么伪变量如下所示: <html><head><ti ...
- SQL模板资源管理器,你用了吗?
SQL Server Management Studio 有个模板资源管理器,不知你用过没有?使用模板创建脚本.自定义模板等功能能大大提高你的工作效率,如果没有尝试过,赶紧去试试吧.很多时候,我们习惯 ...
- Ansible_使用Ansible galaxy部署角色
一.介绍Anisble galaxy 1.介绍Ansibleu galaxy 1️⃣:Ansible Galaxy (官网:https://galaxy.ansible.com)是一个Ansible内 ...
随机推荐
- css实现移动端滚动条隐藏仍然可以滚动内容
.g-panel { height: calc(100% - 112px); overflow: auto; &::-webkit-scrollbar { display: none; // ...
- sqlserver数据库导入大批量数据
参考: https://www.cnblogs.com/zhangliangzlee/p/3585862.html
- Oracle的表空间、用户和模式
Oracle 的 表空间(Tablespace).用户(User).模式(Schema) 前面有整理了一篇 Oracle 数据库(database) 与 实例(instance) 的概念及关系整理 ...
- python 字符串是否包含某个子字符串
方法如下:以后再整理 if str1 in str2: 包含的话,True if str1.find(str2)>=0: 包含的话,返回第一次出现的位置,没有的话为负数 https://www. ...
- FFmpeg—— Bitstream Filters 作用
原文链接: https://stackoverflow.com/questions/32028437/what-are-bitstream-filters-in-ffmpeg Let me expla ...
- 执行ifconfig eth2 up命令报错eth2: unknown interface: No such device的解决思路
排查问题思路 一般出现这种状况都是网卡mac地址错误引起的!要么网卡配置文件中的mac地址不对,要么/etc/udev/rules.d/70-persistent-net.rules文件中的mac地址 ...
- 7_5 困难的串(UVa129)<回溯法:避免无用判断>
“超级氪因素大赛”(译注:英国的一档电视心智竞答节目)的主办方雇你来对付那些足智多谋的参赛选手.在比赛的一个环节中,节目主持人将念出一长串的字母来考验选手的记忆能力.因为许多选手都是分析字串模式的高手 ...
- Redis的安装与用法
Redis的使用方法 ( 命令行安装redis 1 wget http://download.redis.id/releases/redis-5.0.7.tar.gz 2 tar xf redis- ...
- python字典里面列表排序
#coding=utf8 #获取到的数据库ip,和负载数据,需要按照负载情况排序 a={u'1.8.1.14': [379, 368, 361, 358, 1363], u'9.2.4.3': [42 ...
- Spring-Bean的后置处理器
Spring——Bean 后置处理器 Bean 后置处理器允许在调用初始化方法前后对 Bean 进行额外的处理. BeanPostProcessor 接口定义回调方法,你可以实现该方法来提供自己的实例 ...