ansibleplaybook的使用
1、简单格式要求
[root@ansibleserver ansible]# cat nagios.yml --- - hosts: nagiosserver tasks: - name: ensure nagios service stop service: name=nagios state=stopped - name: ensure nagios service start service: name=nagios state=started |
a、整体格式用---开始
b、在冒号之后,必须存在一个空格
c、name和service必须对齐
d、hosts和tasks必须对齐
e、在书写key和value的时候,不能存在空格
f、 在roles中的main.yml不能使用tasks关键词,主要是因为在目录结构中已经包含了此关键词,main.yml是存在于tasks目录中。
如果违反以上规定,那么就会出错,出错内容如下:
ERROR: Syntax Error while loading YAML script, nagios.yml Note: The error may actually appear before this position: line 3, column 1 - hosts: nagiosserver tasks: ^ |
2、运行playbook
对上面格式中的nagios.yml进行运行,运行命令如下:
[root@ansibleserver ansible]# ansible-playbook nagios.yml |
在上面的playbook中,存在两个任务,一个是停止nagios服务,一个是启动nagios服务,运行结果如下所示:
PLAY [nagiosserver] *********************************************************** GATHERING FACTS *************************************************************** ok: [192.168.1.20] TASK: [ensure nagios service stop] ******************************************** changed: [192.168.1.20] TASK: [ensure nagios service start] ******************************************* changed: [192.168.1.20] PLAY RECAP ******************************************************************** 192.168.1.20 : ok=3 changed=2 unreachable=0 failed=0 |
可以看到,定义为name的地方会在输出中进行显示,然后显示运行结果,也就是changed,从而在第一个任务中,表示关闭服务成功,第二个任务中,表示为启动服务成功,最后会显示结果,也就是OK为3,表示连接成功,关闭服务成功,启动服务成功,chenged为2,表示为进行了两次修改。
3、最佳实践
创建roles的组织目录结构可以用如下语句(一次性创建目录组织架构,修改common表示相关的roles):
mkdir -p roles/common/{tasks,handlers,templates,files,vars,defaults,meta} |
总体目录结构如下所示:
[root@ansibleserver kel]# ls -l total 28 drwxr-xr-x 2 root root 4096 Jan 27 06:23 group_vars -rw-r--r-- 1 root root 108 Jan 28 11:35 hosts drwxr-xr-x 2 root root 4096 Jan 28 11:34 host_vars -rw-r--r-- 1 root root 97 Jan 27 06:21 production.yml drwxr-xr-x 4 root root 4096 Jan 27 06:17 roles -rw-r--r-- 1 root root 53 Jan 28 11:27 site.yml -rw-r--r-- 1 root root 77 Jan 27 06:21 staging.yml |
在定级目录中,roles和site.yml和staging.yml和production.yml,group_vars和host_vars在同一级目录中,在变量的优先级中,host_vars的变量优先级比group_vars的优先级高,使用的变量的时候,如果名字相同,那么host_vars中变量的值会覆盖group_vars中变量的值,在roles中设置变量的时候,roles中变量的优先级又比host_vars高
在这里site.yml包含staging和production的playbook,从而在这里使用的是include,在staging和production中使用的是roles,从而需要注意的是roles里的目录结构,roles的目录结构如下所示:
[root@ansibleserver kel]# cd roles [root@ansibleserver roles]# ls -l total 8 drwxr-xr-x 3 root root 4096 Jan 27 06:18 adduser drwxr-xr-x 4 root root 4096 Jan 24 14:13 changepassword [root@ansibleserver roles]# cd changepassword/ [root@ansibleserver changepassword]# ls -l total 8 drwxr-xr-x 2 root root 4096 Jan 27 06:22 tasks drwxr-xr-x 2 root root 4096 Jan 28 11:43 vars [root@ansibleserver changepassword]# cd tasks/ [root@ansibleserver tasks]# ls -l total 4 -rw-r--r-- 1 root root 140 Jan 27 06:22 main.yml |
在roles中,包含的文件夹也就是staging中包含的roles,也就是一个文件夹,表示一个roles,在上面的例子中分为adduser和changepassword两个roles,在子目录中,包含tasks和vars,表示所进行的任务和变量,还有其他的文件夹没有进行创建,从而在最后是一个main.yml
分别的内容如下:
[root@ansibleserver tasks]# cat main.yml --- #- hosts: ansibleservers # use the variables kel to change the user root's passwrod - name: change the user root password shell: 'echo "{{kel}}"|passwd --stdin root > /dev/null 2>&1' |
此内容主要是用kel变量的值作为root用户的密码
[root@ansibleserver kel]# cat site.yml --- - include: staging.yml - include: production.yml |
此中的内容,主要就是使用include语句来包含另外两个playbook
[root@ansibleserver kel]# cat staging.yml --- - hosts: ansibleserver1 roles: - changepassword # - adduser |
此种的内容,主要是用来表示修改密码,使用的是roles,在使用roles的时候,注意目录结构,使用roles的方式也是推荐的一种方式。
执行的时候如下:
[root@ansibleserver kel]# ansible-playbook -i hosts site.yml SSH password: PLAY [ansibleserver1] ********************************************************* GATHERING FACTS *************************************************************** ok: [192.168.1.163] TASK: [changepassword | change the user root password] ************************ changed: [192.168.1.163] PLAY [ansibleserver2] ********************************************************* GATHERING FACTS *************************************************************** ok: [192.168.1.164] TASK: [changepassword | change the user root password] ************************ changed: [192.168.1.164] PLAY RECAP ******************************************************************** 192.168.1.163 : ok=2 changed=1 unreachable=0 failed=0 192.168.1.164 : ok=2 changed=1 unreachable=0 failed=0 |
发现执行成功,再最佳实践里推荐的就是用roles来执行相关的任务,然后将生产环境和测试环境进行分开。
更新一下github的地址,在里面写了相关模块的例子,从而可以进行查看,以后会根据相关的东西,从而写出相关的playbook
ansibleplaybook的使用的更多相关文章
- ansible-playbook
一.ansible-playbook介绍: playbook是由一个或多个"play"组成的列表.play的主要功能在于将事先归为一组的主机装扮成事先通过ansible中的task ...
- ansible-playbook相关
获取目标主机的信息 ansible all -m setup -a "filter=ansible_os_family" 不执行仅测试 ```sh 安装一个zabbix-agent ...
- ansible-playbook(nginx例)
一.创建目录结构 cd /etc/ansible/roles/ mkdir nginx/{files,templates,vars,handlers,meta,default,tasks} -pv 二 ...
- ansible-playbook用法
一.playbook用法 1.playbook的执行文件为YAML语言编写,所以文件名为xxx.yml.YAML语法可以参考https://docs.ansible.com/ansible/lates ...
- linux --- Ansible-playbook篇
Ansible-playbook简介 什么是playbook? 简单点说,playbook就是ansible用于配置,部署和管控节点机器的剧本,将一系列命令的集合归一使用,类似于shell脚本,不过更 ...
- ansible-playbook 进行批量安装tomcat8
ansible-playbook 进行安装tomcat操作 说明: get_url 中下载的内容直接到目的主机 安装的时候需要指定copy: no,说明需直接从目标主机进行安装包 [root@ans ...
- zabbix 应用监控作业笔记 ansible-playbook
目录 目录结构 zabbix-web.yaml zabbix-backup.yaml zabbix-nfs.yaml zabbix-mysql.yaml zabbix-server.yaml zabb ...
- ansible-playbook 实战案例 全网备份 实时备份
目录 ansible-playbook 基础介绍 1.YAML三板斧 2. ansible playbook 安装apache 示例 案例 全网备份 实时备份 环境规划 目录规划 base.yaml ...
- 2.ansible-playbook基本参数
ansible-playbook的参数--force-handlers run handlers even if a task fails 强制执行handler--list-tags list al ...
随机推荐
- USACO Section 2.2: Preface Numbering
搬了leetcode的代码 /* ID: yingzho1 LANG: C++ TASK: preface */ #include <iostream> #include <fstr ...
- LA 4255 Guess
题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...
- Ubuntu 14.10下安装深度音乐客户端
很多刚从windows系统投靠到ubuntu的机油,在听音乐时不是很舒心.毕竟ubuntu软件中心的很多影音软件都是国外的朋友编写的,所以很多时候国内的朋友用着很不舒服.今天给大家推荐的是国内开发者针 ...
- hibernate CascadeType属性说明
CascadeType.PERSIST //只有A类新增时,会级联B对象新增.若B对象在数据库存(跟新)在则抛异常(让B变为持久态) CascadeType.MERGE //指A类新增或者变化,会级联 ...
- MapReduce读取hdfs上文件,建立词频的倒排索引到Hbase
Hdfs上的数据文件为T0,T1,T2(无后缀): T0: What has come into being in him was life, and the life was the light o ...
- Java中的public、protected、default和private的区别
(1) 对于public修饰符,它具有最大的访问权限,可以访问任何一个在CLASSPATH下的类.接口.异常等.它往往用于对外的情况,也就是对象或类对外的一种接口的形式. (2) ...
- httpClient.execute之后一直等待
可能的原因就是之前执行过一次execute,但是没有释放资源. hrp = httpClient.execute(req); //这句释放资源 hrp.getEntity().consumeConte ...
- B/S 和 C/S
B/S最大优势为客户端免维护,适用于用户群庞大,或客户需求经长发生变化的情况. C/S功能强大,可以减轻服务器端压力,如果用户的需求特别复杂,用C/S. 全面: Client/Server是建立在局域 ...
- 用FireMonkey写QQ皮肤
这是运行在Windows平台的效果,同样不需要改一行代码就可以运行在Mac Os,并且效果完全相同: 用FireMonkey做界面速度非常快,其提供的Effect ,Filter,Animation等 ...
- CSS之切出横幅
简述 上节分享了clip-path来实现一个剪切横幅,本节通过另外一种方式来实现一个更经典的横幅. 简述 最终效果 小三角 效果 源码 阴影分割 效果 源码 合并 最终效果 我们先看一下最终要实现的效 ...