ansible脚本-Playbook(一)

Playbook组成部分:

  1. task 任务:包含目标主机上执行的操作,使用模块定义这些操作,每个任务都是一个模块的调用
  2. Variables变量:存储和传递数据,变量可以自定义,可以在playbook当中定义为全局变量,也可以是外部传参
  3. Templates模版:用于生成配置文件,模板是包含占位符的文件,占位符由ansible在执行是转换为变量值
  4. Hander处理器:当需要有变更的时候,可以执行触发器
  5. Roles:角色。类似于dockercompose。是一种封装和组织playbook的,允许把相关的任务,变量。模版和处理器组织成一个可复用的单元

今天主要介绍1-3

实例模板

检查yml语法是否正确

ansible-playbook test1.yml --syntax-check

查看任务数量

检查生效的目标主机

运行脚本

指定剧本执行

如何声明用户:(比较鸡肋)

怎么指定呢:

免密钥登录一定要关闭

声明用户名和密码

ansible-playbook test1.yml -K(大写)

指定用户执行:用小k

第二个脚本,在ansible当中如何定义变量,引用变量,以及在外面传参

注意:是否收集目标主机信息,这个不能在配置文件当中,否则无法执行copy模块,会有冲突

解释一下格式:

vars:

groupname: guoqi

username: wangdefu

字典方式:key-value

vars:

connect: "{{hostvars[inventory_hostname]['ansible_default_ipv4']['address']}}"

这是一个包含所有主机的变量的字典,简而言之就是复制主机的IP地址,然后复制到目标文件

inventory_hostname:目标主机名

ansible_default_ipv4:获取目标主机名

'ansible_default_ipv4'\]\['address'\]:索引 ### 传参数 -e的方式 ![](https://file.jishuzhan.net/article/1739183856642166786/56061d0391ba97d6ac080746f18c22c4.webp) ![](https://file.jishuzhan.net/article/1739183856642166786/18e3aebbbaabaf3ea5fcd0ea6c357ae6.webp) 查看: ![](https://file.jishuzhan.net/article/1739183856642166786/17a5bdf82ee143eecb908ac507566c82.webp) ![](https://file.jishuzhan.net/article/1739183856642166786/8812a8f0c2fd3ce88c2ca0bf40b14b01.webp) ### ****Playbook的条件判断:**** When是一个比较常见的应用场景,实现满足条件即执行,不满足条件跳过执行 总之就是When是满足条件即执行。不满足不执行 ![](https://file.jishuzhan.net/article/1739183856642166786/f1d1e93459668b1f079476b1ebea1656.webp) Debug相当于echo,满足条件打印,不满足丢弃 ![](https://file.jishuzhan.net/article/1739183856642166786/4624c7e8e146ef90f34799867666ce91.webp) ![](https://file.jishuzhan.net/article/1739183856642166786/1cf1fdf320430196430add0235a9a911.webp) ![](https://file.jishuzhan.net/article/1739183856642166786/5da5a0c730b6606123e841822595d7ef.webp) 取反 ****when条件还可以通过**** ****!=**** ****(不等于条件来进行判断)****不等于的意思 ![](https://file.jishuzhan.net/article/1739183856642166786/089b29e4a7c8507e189a55b7d88c1c65.webp) 另一种写法 ![](https://file.jishuzhan.net/article/1739183856642166786/38662f619fd531ab1510ad3ed8374761.webp) ![](https://file.jishuzhan.net/article/1739183856642166786/871a8c726ef0cae16db4be174629d1a8.webp) ![](https://file.jishuzhan.net/article/1739183856642166786/579f20de6635e69d55259e864d6150ab.webp) ![](https://file.jishuzhan.net/article/1739183856642166786/fd71d7fbcd533051b79bf3da88e38c50.webp) ### 练习: ![](https://file.jishuzhan.net/article/1739183856642166786/a406508d99e0aafff2223d18c121face.webp) 方法一: ![](https://file.jishuzhan.net/article/1739183856642166786/a9227f858eb8abcef3a8c8f6c388c1b5.webp) 方法二: ![](https://file.jishuzhan.net/article/1739183856642166786/bc138d7c7defc36d7e4614704b6e2629.webp) 报错,不能同时使用debug和name ![](https://file.jishuzhan.net/article/1739183856642166786/c7242f096de90e7851dc4ce5a06fef5b.webp) 修正版 ![](https://file.jishuzhan.net/article/1739183856642166786/b1dd7a066b1edfd4ba18cb1de60192ae.webp) ![](https://file.jishuzhan.net/article/1739183856642166786/45209c52223227ffd52f251ddc93354a.webp) ![](https://file.jishuzhan.net/article/1739183856642166786/1d6fc058e7b8614d65cbd8b4ce122b6a.webp) ### ****迭代:playbook当中的循环**** Ansible有多种循环格式,最常用的有****with_items 循环遍历**** ![](https://file.jishuzhan.net/article/1739183856642166786/f72e792c556d5e02373f1f3b3e929c93.webp) ![](https://file.jishuzhan.net/article/1739183856642166786/f790ee305a0ae4ecc1eaf4b86a3a3cf4.webp) 声明多个: ![](https://file.jishuzhan.net/article/1739183856642166786/73394d2d51b939a7f6cce1adec48f57a.webp) ![](https://file.jishuzhan.net/article/1739183856642166786/f8c033106b7a3be98ea5b13a54da9a6b.webp) ![](https://file.jishuzhan.net/article/1739183856642166786/374e001aa921ae93fcac9b2857f69ca1.webp) 分组打印 ![](https://file.jishuzhan.net/article/1739183856642166786/73aa52cc664fdd4acdd7f2def8449cae.webp) ![](https://file.jishuzhan.net/article/1739183856642166786/a53f5f89151b0da046446c2608019bf1.webp) 遍历循环在主机上创建目录 ![](https://file.jishuzhan.net/article/1739183856642166786/1fc4d7341336e0d78b9fc5f38c6a8af4.webp) ![](https://file.jishuzhan.net/article/1739183856642166786/7fa601e7d42144aab68e67533b6cf57c.webp) 列表打印list模式 ![](https://file.jishuzhan.net/article/1739183856642166786/84311193823ad344cb6c6b3cb7a0e550.webp) ### Together循环: 组合输出,列表当中的值一一打印出来 ![](https://file.jishuzhan.net/article/1739183856642166786/6baf91e596772d5ef258812c62ebd404.webp) ![](https://file.jishuzhan.net/article/1739183856642166786/04ee6b87acbac4f1eb7d5cd765293b38.webp) 列表组训会,把同一组的值循环 ![](https://file.jishuzhan.net/article/1739183856642166786/f39f0b1dc13c69a097a16487b61bc690.webp) ![](https://file.jishuzhan.net/article/1739183856642166786/6fadf33bdd333aa7a367d52744c5b0b8.webp) ### Nested: 列表里面的元素定义了循环的次数,第二层列表,相当于内循环 ![](https://file.jishuzhan.net/article/1739183856642166786/9b2cd524dd4a721080db9cd5d3f772f2.webp) ![](https://file.jishuzhan.net/article/1739183856642166786/14066fb3a8b6187333d65ab6d2ec2e0a.webp) ### 小结四种循环方式: With_items 最常用的 With_list 列表分组循环 With_together 列表对应得列,数据结合的方式循环 With_nested 相当于与双循环,第一层定义了循环的次数,第二层表达式第一层的每个元素会循环几次 写一个需求: ![](https://file.jishuzhan.net/article/1739183856642166786/fa972b185ad6d623f0448adc1c48c940.webp) 习题: ![](https://file.jishuzhan.net/article/1739183856642166786/0491767fee2fc8d2c95ca4c2514d199a.webp) ![](https://file.jishuzhan.net/article/1739183856642166786/6f979298dfb5eda4af62242fd93c8365.webp) 换成列表声明 ![](https://file.jishuzhan.net/article/1739183856642166786/60087d3f7fa9eaacebe211f0fd18d67d.webp) 参考图: ![](https://file.jishuzhan.net/article/1739183856642166786/a83877e2028655ed100229316338f03a.webp) ![](https://file.jishuzhan.net/article/1739183856642166786/244d862b1e3823a57d40daed5744b307.webp) 例题:yum一键安装多个软件 tree nginx httpd vsftpd dhcp ![](https://file.jishuzhan.net/article/1739183856642166786/64237cde6391dda1e1e891f6c071adf9.webp) ![](https://file.jishuzhan.net/article/1739183856642166786/c680931ce72ffc9b99a8dc054d27a209.webp) ![](https://file.jishuzhan.net/article/1739183856642166786/f0d517a9679434bb502d3782c36a6d90.webp) 参考图: ![](https://file.jishuzhan.net/article/1739183856642166786/4d0cd8cf874d1f89c806909a463415cc.webp)

相关推荐
孙克旭_7 分钟前
day051-ansible循环、判断与jinja2模板
linux·运维·服务器·网络·ansible
leo__5203 天前
自动化运维:使用Ansible简化日常任务
运维·自动化·ansible
风清再凯8 天前
自动化工具ansible,以及playbook剧本
运维·自动化·ansible
IT乌鸦坐飞机8 天前
ansible部署数据库服务随机启动并创建用户和设置用户有完全权限
数据库·ansible·centos7
遇见火星21 天前
如何使用Ansible一键部署MinIO集群?
ansible
粥周粥21 天前
ANSIBLE
ansible
码农101号21 天前
Linux中ansible模块补充和playbook讲解
linux·运维·ansible
码农101号21 天前
Linux的Ansible软件基础使用讲解和ssh远程连接
ansible
烟雨书信22 天前
ANSIBLE运维自动化管理端部署
运维·自动化·ansible
碎碎-li22 天前
ANSIBLE(运维自动化)
运维·自动化·ansible