当前位置: 首页 > news >正文

ansible剧本playbook

Palybook组层部分

tasks 任务包含要在目标主机上执行的操作,使用模块定义这些操作,每个任务都是一个模块的调用
variables变量:存储和传递数据,变量可以自定义,可以在palybook当中定义为全局变量,也可以在外部传参
templates模版:用于生产配置文件,模版是包含占位符的文件,占位符由ansible在执行时转换为变量值
handlers处理器:当需要有变更的时候,可以执行触发器
roles角色:是一种组织和封装palybook的,允许把相关的任务变量,模版和处理器组织成一个可复用的单元
- name: first play
#一个name就是一个任务名,名字可以不写,gather_facts: false
#是否收集目标主机的系统信息,false就是不收集,最好不写。hosts: 192.168.233.12
#执行的目标主机remote_user: root
#在目标主机执行的用户tasks:- name: ping testping:- name: close selinuxcommand: '/sbin/setenforce 0'ignore_errors: True- name: close firewalldservice: name=firewalld state=stopped- name: install httpdyum: name=httpd- name: start httpdservice: enabled=true name=httpd state=started- name: editon index.htmlshell: echo "this is httpd" > /var/www/html/index.htmlnotify: restart httpdhandlers:- name: restart httpdservice: name=httpd state=restarted[root@docker1 opt]# ansible-playbook test1.yaml --syntax-check
#检查配置文件是否有错误
[root@docker1 opt]# ansible-playbook test1.yaml --list-task
#检查生效的目标主机
[root@docker1 opt]# ansible-playbook test1.yaml
#运行剧本文件
[root@docker1 opt]# ansible-playbook test1.yaml --start-at-task='install httpd'
#指定运行剧本第几行如需要切换用户在配置文件中写入
remote_user: dn
become: yes
become_ser: root
vim /etc/ansible/ansible.cfg
71行取消注释
vim /etc/ansible/hosts
[dbservers]
192.168.233.12 ansible_user=root ansible_password=123
需要声明ip地址与主机名ansible-playbook test1.yaml -u root -k
#密码需要手动输入
- hosts: 192.168.233.12remote_user: rootvars:groupname: guoqiusername: wangdefutasks:- name: create groupgroup:name: "{{ groupname }}"system: yesgid: 111- name: create useruser:name: "{{ username }}"uid: 1011group: "{{ groupname }}"shell: /sbin/nologin- name: copy filecopy:content: "{{ hostvars[inventory_hostname]['ansible_default_ipv4']['address']}}"dest: /opt/ky32.txt
#获取目标主机的ip地址,然后打印出来,这里是否获取主机的信息否被删除掉,如果无法获取主机的信息,就会报错
[root@docker1 opt]# ansible-playbook test2.yaml -e 'username=yst groupname=ymr'
#在外面传参
playbook的条件判断
when是一个比较常见的应用场景,实现满足条件即执行,不满足条件即跳过的任务
when是满足条件即执行,不满足不执行格式
- hosts: 192.168.233.12
#可以用主机的ip地址,也可以是用组名,也可以用allremote_user: roottasks:- name: test whendebug:msg: '位置判断'when: ansible_default_ipv4.address == '192.168.233.20'#when: inventory_hostname !== '192.168.233.20'
#作用相同
#debug=echo  msg=输出的内容,用于脚本的调试,在正式脚本中可以去除练习
条件1 ip=10安装nginx ,条件2 ip=20安装httpd
版本1- hosts: allremote_user: roottasks:- name: nginxyum: name=nginxwhen: ansible_default_ipv4.address == '192.168.233.12'- name: httpdyum: name=httpdwhen: ansible_default_ipv4.address == '192.168.233.13'版本2- hosts: allremote_user: roottasks:- name: nginxyum: name=nginx- name: nginx ifodebug:msg: "安装nginx"when: ansible_default_ipv4.address == '192.168.233.12'- name: httpdyum: name=httpd- name: httpd infodebug:msy: "安装httpd"when: ansible_default_ipv4.address == '192.168.233.13'
ansible有多种循环格式,with_items 循环遍历
- hosts: 192.168.233.12remote_user: rootgather_facts: falsetasks:- debug:msg: "{{ item }}"with_items: [a,b,c,d]
#声明变量item,playbook的内置变量,with_item,会把item的值,遍历列表当中的a,b,c,d- hosts: 192.168.233.12remote_user: rootgather_facts: falsetasks:- debug:msg: "{{ item }}"with_items:- [a,b,c,d]- [1,2,3,4]
#这里会被当成一个整体,虽然声明的列表是两个,但是wiith——items还是把两个列表当成整体进行遍历- hosts: 192.168.233.12remote_user: rootgather_facts: falsetasks:- debug:msg: "{{ item }}"with_list:- [a,b,c,d]- [1,2,3,4]
#这里会被分组打印,一个列表打印一组- hosts: 192.168.233.12remote_user: rootgather_facts: falsetasks:- name: create filefile:path: "{{ item }}"state: touchwith_items:- [/opt/a,/opt/b,/opt/c,/opt/d]- [/opt/1,/opt/2,/opt/3,/opt/4]
#分组创建文件- hosts: 192.168.233.12remote_user: rootgather_facts: falsetasks:- debug:msg: "{{ item }}"with_together:- [a,b,c,d]- [1,2,3,4]
#组合输出,一一对应,列表组循环,如果没有组合会输出null
#列表里面的元素定义了循环的次数,第二层列表,相当于内循环
with_items:最常用的
with_list:列别分组循环
with_together:列表对应的列,数据结合的方式循环
with_nested:相当于双层循环,第一层定义了循环的次数,第二层表示第一次的每个元素会循环几次
#基于循环,创建文件,目录,和用户组
- name: play1hosts: 192.168.233.12gather_facts: falsetasks:- name: create groupgroup:name: "{{ item }}"state: presentwith_items:- 'dn1'- 'dn2'- name: create useruser:name: "{{ item.name }}"state: presentgroups: "{{ item.groups }}"with_items:- {name: 'test1', groups: 'dn1'}- {name: 'test2', groups: 'dn2'}
yum 一键安装多个软件  tree sl nginx httpd vsftpd dhcp- name: play2hosts: 192.168.233.12gather_facts: falsetasks:- name: create tree sl nginx httpd vsftpd dhcpyum:name: "{{ item }}"with_list:- tree- sl- nginx- httpd- vsftpd- dhcp

http://www.lryc.cn/news/264322.html

相关文章:

  • .NET 中string类型的字符串内部化机制
  • 公共字段自动填充——后端
  • nginx upstream 6种负载均衡策略介绍
  • 基于Antd4 和React-hooks的项目开发
  • Spring中用到的设计模式
  • 常用网络接口自动化测试框架
  • 【重点】【贪心】55.跳跃游戏
  • 灰度化、二值化、边缘检测、轮廓检测
  • 基于JAVA的高校大学生创业管理系统 开源项目
  • 神经网络学习小记录76——Tensorflow2设置随机种子Seed来保证训练结果唯一
  • ai学习笔记-入门
  • workflow系列教程(5-1)HTTP Server
  • php-使用wangeditor实现富文本(完成图片上传)-npm
  • mysql查看数据库中所有的表的建表语句
  • 【Axure RP9】实现登入效验及实现左侧菜单栏跳转各页面
  • 76. 最小覆盖子串。优化官方题解!
  • 在国产GPU寒武纪MLU上快速上手Pytorch使用指南
  • 重生奇迹MU觉醒战士攻略
  • 美颜技术详解:深入了解视频美颜SDK的工作机制
  • 3D模型格式转换工具如何实现高性能数据转换?请看CAE系统开发实例!
  • 多级缓存:亿级流量的缓存方案
  • C语言——高精度乘法
  • 为什么C语言没有被C++所取代呢?
  • 基于Spring的枚举类+策略模式设计(以实现多种第三方支付功能为例)
  • 基于Linphone android sdk开发Android软话机
  • [论文分享]TimeDRL:多元时间序列的解纠缠表示学习
  • 分享一个好看的vs主题
  • 什么是云呼叫中心?
  • 还在用nvm?来试试更快的node版本管理工具——fnm
  • 【Hadoop精讲】HDFS详解