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内 ...
随机推荐
- [CF]Round 516
A Make a triangle! 题意:给定三根线段,问最少要延长多少才能拼成一个三角形. 数学题. B Equations of Mathematical Magic 题意:求$a - (a \ ...
- HITCON-Training-Writeup
HITCON-Training-Writeup 原文链接M4x@10.0.0.55 项目地址M4x's github,欢迎star~ 更新时间5月16 复习一下二进制基础,写写HITCON-Train ...
- 用eclipse中自带的jetty启动项目
1.建立datasources.xml,创建jndi <jee:jndi-lookup id="datasource" jndi-name="jndiname&qu ...
- 2.8 (显示、隐式、线程休眠) selenium 等待方式 ❀
http://blog.csdn.net/pf20050904/article/details/20052485 http://www.cnblogs.com/hellokitty1/p/629584 ...
- 8.1.1 IO
IO对象无拷贝或赋值.进行IO操作的函数通常以引用的方式传递和返回流,且该引用不能是const的 确定一个流对象是否处于良好状态的最简单的方法是将它作为一个条件来使用 while (cin >& ...
- Custom LED Keychain, Small And Surefire Gifts
The LED Keychain makes it easy for people to carry their keys with them and carry them with th ...
- 每天进步一点点------Allegro 手工布线时控制面板各选项说明
在进行手工布线过程中,最重要的就是对控制面板中的各个选项进行设置,因此首先介绍控制面板中各个选项的含义. 手工布线的命令为Route->connect,执行命令后,右侧控制面板如图8.14所示. ...
- springboot中druid监控的配置(DruidConfiguration)
当数据库连接池使用druid 时,我们进行一些简单的配置就能查看到sql监控,web监控,url监控等等. 以springboot为例,配置如下 import com.alibaba.druid.su ...
- poj 1611 :The Suspects经典的并查集题目
Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, was recognized ...
- ASP.NET Core Web API中实现全局异常捕获与处理
处理全局异常 HANDLING ERRORS GLOBALLY 在上面的示例中,我们的 action 内部有一个 try-catch 代码块.这一点很重要,我们需要在我们的 action 方法体中处理 ...