剧本 playbook, roles
发布日期:2022-03-30 05:03:19
浏览次数:34
分类:博客文章
本文共 3364 字,大约阅读时间需要 11 分钟。
1.yaml
格式要求 冒号后面必须有空格
等号后面不能有空格
-后面也要有空格
严格对齐
2.setup
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 # 系统上的pythonansible cache -m setup -a 'filter=*processor*' # 用来搜索
如果出现
- hosts: db tasks: - name: zzgbgn dong: zzdbgn when: zhanzhe - name: pzhegbgn dong: pzhedbgn when: pazhe
条件判断
when: 条件==“条件”
不同的系统
不同的版本
不同的环境
不同的用户
- 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"
Ubuntu安装包的方式是apt-get
标签 tags
- hosts: db tasks: - name: wadong tieqiao: wadong - name: tk dong: tk tags: tk
- 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: gb tasks: - name: gbqc dong: {{ item }} with_items: - qbqc - cyf - ssfj
- hosts: web tasks: - name: crateuser user: name={{item}} with_items: - alex20 - alex21 - alex22~
- hosts: web tasks: - name: crateuser user: name={{item}} with_items: - alex30 - alex31 - alex32 - 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':alex40,'group':wulaoshi30} - {'name':alex41,'group':wulaoshi31} - {'name':alex42,'group':wulaoshi32}
模版 template
- 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和template的区别
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
注意:写相对路径 在当前目录下新建一个templates目录,然后把文件放在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 service: name=redis state=restarted
3.roles
目录清晰
可以相互调用
roles文件夹
setenforce 0 #用来临时关闭selinuxiptables -F # 临时关闭防火墙/etc/selinux/config # 永久关闭
转载地址:https://www.cnblogs.com/chenxi67/p/10413502.html 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
不错!
[***.144.177.141]2024年04月12日 20时49分58秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
select下拉框分组展示插件的使用--(select-mania插件的使用)
2019-04-30
Java Lambda表达式的应用--Stream API操作集合框架
2019-04-30
Myslq连接(JDBC)url属性的参数的设置
2019-04-30
关于Spring MVC与前端的交互
2019-04-30
大厂经典面试题:Redis为什么这么快?
2019-04-30
Android之Retrofit基本用法篇
2019-04-30
Netty与网络协议资料整理
2019-04-30
golang实现大数据量文件的排序
2019-04-30
golang中的time包
2019-04-30
2019NOIP D4题 加工领奖
2019-04-30
2021.5.19 JS高级第二天
2019-04-30
SpringBoot内置Tomcat配置参数
2019-04-30
linux 根目录下文件夹分析
2019-04-30
linux 查看分区和文件大小
2019-04-30
技术转管理?这些“坑”你要绕道走
2019-04-30
领域驱动设计(DDD)前夜:面向对象思想
2019-04-30
Camera驱动调试小记
2019-04-30
四线触摸屏原理
2019-04-30