ansible剧本之playbook操作
ansible 剧本
yaml介绍:
是一个编程语言
文件后缀名 yaml yml
数据对应格式:
字典: key: value
列表: [] -
ansible-playbook命令格式
执行顺序: 从上往下
特性:幂等性 不管执行多少遍,结果都是一样的
#命令格式:ansible-playbook [options] playbook.yml [playbook2 ...]
#参数:
-C, --check # 检查,白跑,干跑
-f FORKS, --forks=FORKS #用来做并发
--list-hosts # 列出主机列表
--syntax-check # 语法检查
-e #指定参数
简单用法
#vi p1.yml 写入如下内容:
- hosts: web #主机列表
tasks: #任务
- name: creategroup #任务名称
group: name=alex10 #模块名:参数
- name: cretaeuser #任务名称
user: name=wusir10 #模块名:参数
#检查p1.yml语法的正确性
[root@localhost ~]# ansible-playbook --syntax-check p1.yml
playbook: p1.yml
#执行
[root@localhost ~]# ansible-playbook p1.yml
hosts: gb
tasks:
- name: 第san个姑娘
dong: 第san个姑娘
传参
- hosts: web
tasks:
- name: create{{ user }}
user: name={{ user}}
第一种方式
ansible-playbook -e 'user=alexsb10' p2.yml
第二种方式
[db]
192.168.107.132 user=alexsb11
192.168.107.133 user=alexsb12
第三种方式
[db:vars] #表示组的参数
user=alexsb13
第四种方式
- hosts: db
vars:
- user: alexsb14
tasks:
- name: create{{ user }}
user: name={{ user}}
第五种传参方式
- hosts: db
tasks:
- name: sum
shell: echo 7+8|bc
register: user
- name: createuser
user: name={{user.stdout}}
传参方式的优先级
-e > playbook vars > hosts文件
setup查看配置参数
[root@localhost ~]# ansible cache -m setup #查看cache组的配置
ansible_all_ipv4_addresses # ipv4的所有地址
ansible_all_ipv6_addresses # ipv6的所有地址
ansible_date_time # 获取到控制节点时间
ansible_default_ipv4 # 默认的ipv4地址
ansible_distribution # 系统
ansible_distribution_major_version # 系统的大版本
ansible_distribution_version # 系统的版本号
ansible_domain #系统所在的域
ansible_env #系统的环境变量
ansible_hostname #系统的主机名
ansible_fqdn #系统的全名
ansible_machine #系统的架构
ansible_memory_mb #系统的内存信息
ansible_os_family # 系统的家族
ansible_pkg_mgr # 系统的包管理工具
ansible_processor_cores #系统的cpu的核数(每颗)
ansible_processor_count #系统cpu的颗数
ansible_processor_vcpus #系统cpu的总个数=cpu的颗数*CPU的核数
ansible_python # 系统上的python
#命令:
ansible cache -m setup -a 'filter=*processor*' # 用来搜索
条件判断
适用于以下情况
- 不同的系统
- 不同的版本
- 不同的环境
- 不同的用户
#y2.yml文件内容如下:
- hosts: db
remote_user: root
tasks:
- name: createfile
copy: content="大弦嘈嘈如急雨" dest=/tmp/a.txt
when: a=="3"
- name: cratefile
copy: content="小弦切切如私语" dest=/tmp/a.txt
when: a=="4"
#执行命令
[root@localhost yaml]# ansible-playbook -e 'a="3"' p2.yml
Ubuntu 安装包的方式是apt-get
tags 单独执行某一条指令时
- hosts: web
tasks:
- name: installnginx
yum: name=nginx
- name: copyfile
copy: src=/etc/nginx/nginx.conf dest=/etc/nginx/nginx.conf
tags: copyfile
- name: start
service: name=nginx state=started
ansible-playbook -t copyfile p7.yml
循环 with_item
一次性创建多个
- hosts: web
tasks:
- name: crateuser
user: name={{item}}
with_items:
- feng20
- feng21
- feng22
~
- hosts: web
tasks:
- name: crateuser
user: name={{item}}
with_items:
- feng30
- feng31
- feng32
- name: crategroup
group: name={{item}}
with_items:
- wulaoshi20
- wulaoshi21
- wulaoshi22
~
嵌套循环
- hosts: web
tasks:
- name: crategroup
group: name={{item}}
with_items:
- wulaoshi30
- wulaoshi31
- wulaoshi32
- name: createuser
user: name={{item.name}} group={{item.group}}
with_items:
- {'name':feng40,'group':wulaoshi30}
- {'name':feng41,'group':wulaoshi31}
- {'name':feng42,'group':wulaoshi32}
template:
jinja2
- hosts: web
tasks:
- name: installredis
yum: name=redis
- name: copyfile
template: src=/etc/redis.conf dest=/etc/redis.conf
- name: start
service: name=redis state=started
配置文件: bind {{ ansible_default_ipv4.address }}
copy和tamplate的区别
- copy模块不替代参数
- template模块替代参数
- hosts: web
tasks:
- name: installredis
yum: name=redis
- name: copyfile
template: src=redis.conf dest=/etc/redis.conf
- name: start
service: name=redis state=started
ps:写相对路径: 在当前目录下新建一个templates目录,然后把redis.conf配置文件放在templates目录里面
handlers
修改配置文件,自动触发执行
- hosts: web
tasks:
- name: installredis
yum: name=redis
- name: copyfile
template: src=redis.conf dest=/etc/redis.conf
tags: copyfile
notify: restart
- name: start
service: name=redis state=started
handlers:
- name: restart #此name名称和notify后面的名称保持一致
service: name=redis state=restarted
#命令:
[root@localhost yaml]# ansible-playbook -t copyfile p7.yml
ansible剧本之playbook操作的更多相关文章
- 自动化运维工具——ansible剧本playbook(三)
一.Playbook--Ansible剧本 playbook是由一个或多个 "play"组成的列表 play的主要功能在于将事先归并为一组的主机装扮成事先通过ansible中的ta ...
- Ansible 自动化运维——剧本(playbook)
Ansible 自动化运维--剧本(playbook) 1.playbook介绍: playbook是ansible用于配置,部署,和管理被控节点的剧本.通过playbook的详细描述,执行其中的ta ...
- Ansible剧本介绍及使用演示(week5_day2)--技术流ken
Ansible剧本编写说明 一. 缩进 yaml 的缩进要求比较严格.一定不能使用tab键 注意:编写yaml文件,就忘掉shell的tab吧. 二. 冒号 每个冒号后面一定要有一个空格 注意:1. ...
- ansible核心模块playbook介绍
ansible的playbook采用yaml语法,它简单地实现了json格式的事件描述.yaml之于json就像markdown之于html一样,极度简化了json的书写.在学习ansible pla ...
- Ansible剧本介绍及使用演示(3)
Ansible剧本编写说明 一. 缩进 yaml 的缩进要求比较严格.一定不能使用tab键 注意:编写yaml文件,就忘掉shell的tab吧. 二. 冒号 每个冒号后面一定要有一个空格 注意:1. ...
- Linux中级之ansible配置(playbook)
一.playbooks 如果用模块形式一般有幂等性,如果用shell或者command没有幂等性 playbooks相当于是shell脚本,可以把要执行的任务写到文件当中,一次执行,方便调用 task ...
- ansible编译httpd playbook示例
以下是playbook的内容.它的处理流程是: 1.先在本地下载apr,apr-util,httpd共3个.tar.gz文件. 2.解压这3个文件. 3.安装pcre和pcre-devel依赖包. 4 ...
- Git+Gitlab+Ansible剧本实现一键部署动态网站(二)--技术流ken
项目前言 之前已经写了一篇关于git和ansible的博客<Git+Gitlab+Ansible剧本实现一键部署Nginx--技术流ken>.关于git,gitliab,ansible在我 ...
- Git+Gitlab+Ansible剧本实现一键部署动态网站(5)
项目前言 之前已经写了一篇关于git和ansible的博客<Git+Gitlab+Ansible剧本实现一键部署Nginx–技术流ken>.关于git,gitliab,ansible在我以 ...
随机推荐
- MongoDB 学习笔记之 手动预先分片
手动预先分片: 目的:手动预先分片是为了防止未来chunk的移动,减少IO. sh.shardCollection("shop.users",{"userId" ...
- Android系统介绍与框架
一.Andriod是什么? Android系统是Google开发的一款开源移动OS,Android中文名被国内用户俗称“安卓”.Android操作系统基于Linux内核设计,使用了Google公司自己 ...
- CSS ellipsis 与 padding 结合时的问题
CSS 实现的文本截断 考察如下代码实现文本超出自动截断的样式代码: .truncate-text-4 { overflow: hidden; text-overflow: ellipsis; dis ...
- Window下的VScode快捷键
转载自4ark 全局 Ctrl + Shift + P, F1 显示命令面板 Ctrl + P 快速打开Ctrl + Shift + N 打开新窗口Ctrl + Shift + W 关闭窗口 基本 C ...
- Paid Roads POJ - 3411
A network of m roads connects N cities (numbered from 1 to N). There may be more than one road conne ...
- SQL SERVER数据库,按年、月、日、时、分、秒计算两个时间字段之间的间隔时间样例
使用DATEDIFF(取值,时间字段1,时间字段2) 举例: SELECT DATEDIFF(YEAR,DRYSJ,DCYSJ),* FROM YXHIS2019..TBZYBR2019 --SQL ...
- 蓝牙TWS耳机IBRT的原理初分析
最近在倒腾TWS对耳的一些东西,看到一些源码,发现一个新概念,IBRT没有搞清楚,抱着吾将上下而求索的态度,详细看了一些代码,查了一些资料,还是发现了不少有价值的信息的.至少,我突然感觉自己懂了一些什 ...
- django渲染高阶
08.16自我总结 django渲染高阶 一.利用母版渲染 1.创建母版文件 如:stamper.html <!DOCTYPE html> <html lang="en&q ...
- cobalt strike和metasploit结合使用(互相传递shell会话
攻击机 192.168.5.173 装有msf和cs 受害机 192.168.5.179 win7 0x01 msf 派生 shell 给 Cobalt strike Msfvenom生成木马上线: ...
- veil-Evasion免杀使用
Veil-Evasion 是 Veil-Framework 框架的一部分,也是其主要的项目.利用它我们可以生成绕过杀软的 payload kali 上并未安装,下面我们来进行简单的安装.我们直接从 ...