(三)ansible playbook
一,YAML语法
- YAML的语法和其他高阶语言类似并且可以简单表达清单、散列表、标量等数据结构。(列表用横杆表示,键值对用冒号分割,键值对里又可以嵌套另外的键值对)
- YAML文件扩展名通常为.yaml或者.yml
- 一定要对齐,只能使用空格
name: tom
age: 21
gender: male
spourse:
name: lily
gender: female
children:
- name: susan
age: 2
gender: feamle
- name: sunny
age: 10
gender: male
二,ansible playbook 核心组件
- tasks:任务
- name
- notify 调用handlers
- 模块名称(shell,yum,script)
- variables:变量
- templates:模板
- handlers:处理器
- roles:角色
- host :执行对象
- remote_user:定义执行命令的远程用户
三,示例
示例一
#vim /root/first.yml - hosts: all
remote_user: root
vars: httpd_port=80 tasks:
- name: install httpd
yum: name=httpd state=present
- name: install php
yum: name=php state=present
- name: start httpd
service: name=httpd state=started enabled=true
vars(变量):
变量命名: 字母数字下划线组成,只能以字母开头
变量种类:
facts(内置变量)由远程主机发回的主机属性信息,这些信息被保存在ansible变量当中例如:ansible 192.168.238.170 -m setup 来获取远程主机上的属性信息,这些属性信息保存在facts中通过命令行传递通过命令行传递:ansible-playbook test.yml –extra-vars “host=www user=tom“(如果剧本中已有此处定义的变量则会被覆盖)通过roles传递主机变量在/etc/ansible/hosts中定义[web1]192.168.1.1 name=haha组变量[group_name:vars]foo=bar
示例二
#vim /root/second.yml
- hosts: web1
remote_user: root
vars:
username: bob
password: 123 tasks:
- name: add user
user: name={{ username }} state=present #
when: ansible_os_family == "Debian"
- name: set password
shell: echo {{ password }} |passwd --stdin {{ username }}
- name: install httpd php
yum: name={{ item }} state=present
with_items:
- httpd
- php
- name: add two users
user: name={{ item }} state=present groups={{ item.groups }}
with_items:
- { name: 'user1', groups: 'group1'}
- { name: 'user2', groups: 'group2'}
示例三
#vim /root/third.yml - hosts: web1
remote_user: root
vars:
httpd_port=80 tasks:
- name: install httpd
yum: name=httpd state=present
- name: install php
yum: name=php state=present
- name: copy config file
copy: src=/root/httpd.conf dest=/etc/httpd/conf/httpd.conf
notify: restart httpd
- name: start httpd
service: name=httpd state=started enabled=true handlers:
- name: restart httpd
service: name=httpd state=restarted
示例四
#/root/httpd.conf
Listen {{ http_port }}
#vim /root/fourth.yml - hosts: web1
remote_user: root
vars:
http_port: 80 tasks:
- name: install httpd
yum: name=httpd state=present
- name: copy config file
template: src=/root/httpd.conf dest=/etc/httpd/conf/httpd.conf #变量 http_port 将在/etc/httpd/conf/httpd.conf中生效
notify: restart httpd
- name: start httpd
service: name=httpd state=started enabled=true handlers:
- name: restart httpd
service: name=httpd state=restarted
示例五
roles的文件结构:
mkdir -p /opt/ansible/roles
cd /opt/ansible/roles
mkdir -p web/{files,templayes,tasks,handlers,vars,meta}
1,定义变量文件(web/vars/main.yml)
user: tom
group: tom
http_port: 8080
2,定义任务文件(web/tasks/main.yml)
- name: install httpd
yum: name=httpd state=present
- name: copy config file
template: src=httpd.conf dest=/etc/httpd/conf/httpd.conf
notify: restart httpd
tags: conf
- name: start httpd
service: name=httpd state=started enabled=true
#这里的template指的是相对路径-->web1/templates
#tags可以在运行时指定标签任务
3,定义handlers文件(web/handlers/main.yml)
handlers:
- name: restart httpd
service: name=httpd state=restarted
4,定义配置文件(web/templates/httpd.conf)
……
Listen {{ http_port }}
……
5,定义一个调用roles文件(/opt/ansible/web_install.yml)
- hosts: web1
remote_user: root
roles:
- web1
#- { role:web2, http_port:8080 }
#hosts:web1 指在/etc/ansible/hosts中定义的组,上面有定义
#roles: web1 指的是当前目录下的web1目录,也可通过role传递变量, 也可调用多个role
#这样只需更改hosts的主机就可以实现不同主机的代码重用了
四,运行playbook
ansible-playbook web1.yml
指定运行任务:
ansible-playbook -t conf web1.yml
(三)ansible playbook的更多相关文章
- Ansible playbook API 开发 调用测试
Ansible是Agentless的轻量级批量配置管理工具,由于出现的比较晚(13年)基于Ansible进行开发的相关文档较少,因此,这里通过一些小的实验,结合现有资料以及源码,探索一下Ansible ...
- ansible playbook实践(四)-如何调试写好的playbook文件
有时,我们写了一个长长,功能很强悍的yaml文件,但是,我们有可能会担心,写的yaml文件是否正确,是否有漏洞危机,毕竟是要修改线上的机器,那么,有可能我们可以从以下几个检查维度来进行,确保在大规模应 ...
- ansible playbook批量改ssh配置文件,远程用户Permission denied
最近手里的数百台服务器需要改/etc/ssh/sshd_config的参数,禁止root直接登陆,也就是说 [root@t0 ~]# cat /etc/ssh/sshd_config | grep R ...
- ansible笔记(11):初识ansible playbook(二)
ansible笔记():初识ansible playbook(二) 有前文作为基础,如下示例是非常容易理解的: --- - hosts: test211 remote_user: root tasks ...
- ansible笔记(10):初识ansible playbook
ansible笔记():初识ansible playbook 假设,我们想要在test70主机上安装nginx并启动,我们可以在ansible主机中执行如下3条命令 ansible test70 -m ...
- Ansible Playbook 详解
一.playbook 的简单使用 1.创建文件实例 (1)编辑配置文件 [root@tiejiangSRC1 ~]# cd /etc/ansible/ [root@tiejiangSRC1 ansib ...
- Ansible playbook基础组件介绍
本节内容: ansible playbook介绍 ansible playbook基础组件 playbook中使用变量 一.ansible playbook介绍 playbook是由一个或多个“pla ...
- ansible playbook基本操作
一.ansible playbook简单使用 相当于是把模块写入到配置文件里面 vim /etc/ansible/test.yml //写入如下内容: --- - hosts: 127.0.0.1 r ...
- ansible入门四(Ansible playbook基础组件介绍)
本节内容: ansible playbook介绍 ansible playbook基础组件 playbook中使用变量 一.ansible playbook介绍 playbook是由一个或多个“pla ...
随机推荐
- 关于晶体问题TCXO_14.7456MHZ
如何判断热点的晶体好不好,首先,看偏移,偏移为0的晶体一般就是温补晶体,当然偏移是500或者几百固定的也是温补,但是不是我们首选的温补晶体 因为偏移为0非常省事,这是系统默认的偏移0,因此设置好频率就 ...
- cocos2dx Android 使用ant 批量打包
参考文章: 例子:http://www.2cto.com/kf/201305/208139.html http://blog.csdn.net/ljb_blog/article/details/127 ...
- SwiftUI - iOS10本地推送通知教程UserNotifications在Swift中的实现方式
简介 消息推送相信在很多人的眼里都不陌生了吧?像即时聊天微信,好友发信息给你时会在顶部弹下小窗口提醒你.也像是在影院APP预订了电影票,在开场前一小时你也会收到提醒.这类推送是需要经过后端发送请求的, ...
- [ARC060D] 最良表現
题目 点这里看题目. 分析 由于 KMP 的失配数组有着天然的找循环节的功能,因此我们不难想到对原串进行两次 KMP ,一正一反. 可以发现如下的规律: 1. 原串无循环节,这个时候 ...
- eval5: TypeScript编写的JavaScript解释器
eval5是基于TypeScript编写的JavaScript解释器,100%支持ES5语法. 项目地址:https://github.com/bplok20010/eval5 使用场景 浏览器环境中 ...
- Codeforces Round #647 (Div. 2)
Problem A https://codeforces.com/contest/1362/problem/A 判断x/y是不是2的k次方, 如果是 k/3 + (k%3)/2 + (k%3%2)即为 ...
- pip超时问题解决
阿里云 http://mirrors.aliyun.com/pypi/simple/ 中国科技大学 https://pypi.mirrors.ustc.edu.cn/simple/ 豆瓣(douban ...
- Day7-微信小程序实战-天气预报小程序
前段时间在B站跟着一个视频,搞天气预报小程序 https://www.bilibili.com/video/BV1cJ411879s 但是因为这个调用的接口要money,太贵了就没买,就只是做了一些不 ...
- Spring系列.事务管理
Spring提供了一致的事务管理抽象.这个抽象是Spring最重要的抽象之一, 它有如下的优点: 为不同的事务API提供一致的编程模型,如JTA.JDBC.Hibernate和MyBatis数据库层 ...
- 国外的教授都说,用这个方式21天就能学会python,这是中国速度
你尝试过吗?按照这个方式,用21天就能学会python编程. 在今年的疫情期间,在家的时间何止21天,有这样一位做财务的朋友,为了提高自己的数据分析能力,在家通过这个方式,跟着21天的规划,坚 ...