当前位置:网站首页>ansible——playbook剧本概念及示例
ansible——playbook剧本概念及示例
2022-08-06 19:43:00 【爱看square dancing的老奶奶】
目录
一、playbook
playbook是剧本的意思
通过 task 调用 ansible 的模块将多个 play 组织在一 个playbook中运行。
playbook本身由以下各部分组成:
- Tasks: 任务,即调用模块完成的某操作
- Variables: 变量
- Templates: 模板
- Handlers: 处理器,当某条件满足时,触发执行的操作
- Roles: 角色
playbook yaml语法是换行空两格,-和:后必须空一格
YAML:是一种非标记语言。是用来写配置文件的语言,非常简洁合强大;
YAML语法和其他语言类似,也可以表达散列表、标量等数据结构
结构通过空格来展示,序列里配置项通过 - 来表示;Map里的键值用:来分隔;YAML的扩展名为yaml
1.1 yaml基本语法规则
- 大小写敏感
- 使用缩进表示层级关系
- 缩进时不允许使用tab键、只允许使用空格
- 缩进的空格数目不重要,只要相同层级的元素左侧对齐即可
| hosts | 定义节点,可以是组 |
| remote_user | 是你以什么用户身份进行登陆 |
| tasks | 是你的任务 |
| become:yes | 表示切换用户 |
| become_user: mysql | 表示切换到mysql用户,配合上一条使用 |
| - name: | 为下面执行的操作起名 |
1.2 yaml支持的数据结构
- 对象:键值对的集合,又称为映射(mapping)/ 哈希(hashes)/ 字典(dictionary)
- 数组:一组按次序排列的值,又称为序列(sequence)/ 列表(list)
- 纯量:单个的、不可再分的值
二、Inventory中变量
Inventory是Ansible管理主机信息的配置文件,相当于系统HOSTS文件的功能,默认存放在/etc/ansible/hosts
1、主机变量
[web1]
www1.lic.com
www2.lic.com
2、组变量
[servers:vars]
3、组嵌套
[web1]
www1.lic.com
www2.lic.com
[web2]
www3.lic.com
www4.lic.com
[webservers]
web1
web2
2.1 inventor 变量参数
| 参数 | 说明 |
| ansible_ssh_host | 将要连接的远程主机名,与你想要设定的主机的别名不同的话,可通过此变量设置 |
| ansible_ssh_port ss | h端口号,如果不是默认的端口号,通过此变量设置 |
| ansible_ssh_user | 默认的ssh用户名 |
| ansible_ssh_pass | ssh密码(这种方式并不安全,我们强烈建议使用 --ask-pass或SSH密钥) |
| ansible_ssh_private_key_file | ssh使用的私钥文件,适用于有多个密钥,而你不想使用SSH代理的情况 |
| ansible_ssh_common_args | 此设置附加到sftp,scp和ssh的缺省命令行 |
| ansible_sftp_extra_args | 此设置附加到默认sftp命令行 |
| ansible_scp_extra_args | 此设置附加到默认scp命令行 |
| ansible_ssh_extra_args | 此设置附加到默认ssh命令行 |
| ansible_ssh_pipelining | 确定是否使用SSH管道。这可以覆盖ansible.cfg中得到设置 |
| ansible_shell_type | 目标系统的shell类型,默认情况下,命令的执行使用sh语法,可设置为csh 或 fish |
| ansible_python_interpreter | 目标主机的python路径,适用于的情况:系统中有多个python,或者命令路径不是“/usr/bin/python” |
| ansible_*_interpreter | 这里的*可以是ruby或perl或其他语言的解释器,作用和ansible_python_interpreter类似 |
| ansible_shell_executable | 这将设置ansible控制器将在目标机器上使用的shell,覆盖ansible.cfg中的配置,默认为/bin/sh |
三、playbook示例
基本命令介绍
ansible-playbook xxx.yaml --syntax-check #检查yaml文件的语法是否正确
ansible-playbook xxx.yaml --list-task #检查tasks任务
ansible-playbook xxx.yaml --list-hosts #检查生效的主机
ansible-playbook xxx.yaml --start-at-task='xxx' #指定从某个task开始运行
- hosts: webserver #指定主机组,可以是一个或多个组
remote_user: root #指定远程主机执行的用户名
| 参数 | 说明 |
| -k(-ask-pass) | 用来交互输入ssh密码 |
| -K(-ask-become-pass) | 用来交互输入sudo密码 |
| -u | 指定用户 |
| -e | 引入变量值 |
3.1 为每个任务定义远程执行用户
cd /opt
vim 1.yaml
- hosts: mysql
remote_user: root
tasks:
- name: test connection
ping:
remote_user: mysql
ansible mysql -m user -a 'name=mysql'
ansible mysql -m shell -a 'echo 123123 | passwd --stdin mysql'
ansible-playbook 1.yaml -k
123123
3.2 指定远程主机切换用户执行剧本
vim 2.yaml
- hosts: mysql
remote_user: root
become: yes
become_user: mysql
tasks:
- name: copy text
copy: src=/etc/fstab dest=/home/mysql/fstab.bak
ansible-playbook 2.yaml
3.3 tasks忽略错误,强制返回成功
- Play的主体部分是task列表,task列表中的各任务按次序逐个在hosts中指定的主机上执行,即在所有主机上完成第一个任务后再开始。在运行playbook时 (从上到下执行),如果一个host执行task失败, 整个tasks都会停止。
- 每一个task必须有一个名称 name,这样在运行playbook时,从其输出的任务执行信息中可以很好的辨别出是属于哪一个task的。
错误示例:遇到错误task自动停止,apache服务不会继续安装
vim 3.yaml
- hosts: webserver
remote_user: root
tasks:
- name: stop selinux
command: '/usr/sbin/setenforc 0'
- name: install httpd
yum: name=httpd
- name: start httpd
service: name=httpd state=started
ansible-playbook 3.yaml
加入ignore_errors: True 忽略错误,报错后继续执行
vim 3.yaml
- hosts: webserver
remote_user: root
tasks:
- name: stop selinux
command: '/usr/sbin/setenforc 0'
ignore_errors: True
- name: install httpd
yum: name=httpd
- name: start httpd
service: name=httpd state=started
ansible-playbook 3.yaml
3.4 针对多个主机节点执行剧本
vim 4.yaml
- hosts: webserver
remote_user: root
tasks:
- name: remove httpd
yum: name=httpd state=absent
- hosts: mysql
remote_user: root
tasks:
- name: copy file
copy: src=/etc/fstab dest=/opt/haha.txt
3.5 Handlers概述
Handlers也是一些task的列表, 和一般的task并没有什么区别。
是由通知者进行的notify,如果没有被notify,则Handlers不会执行,假如被notify了 ,则Handlers被执行不管有多少个通知者进行了notify,等到play中的所有task执行完成之后,handlers也只会被执行一次
vim 5.yaml
- hosts: webserver
remote_user: root
tasks:
- name: remove httpd
yum: name=httpd state=absent
- name: start firewalld
service: name=firewalld state=started
- name: setenforce 0 && install httpd
command: '/usr/sbin/setenforce 0'
notify:
- step one
- name: stop firewalld && start httpd
service: name=firewalld state=stopped
notify:
- step two
handlers:
- name: step one
yum: name=httpd
- name: step two
service: name=httpd state=started
ansible-playbook 5.yaml
3.6 引入变量
playbook引入变量有三种方式:
- 通过ansible命令参数-e传递
- 直接在yaml中定义
- 引用主机清单中定义的变量
3.6.1 通过ansible命令参数-e传递
vim 6_1.yaml
- hosts: mysql
remote_user: root
vars:
- user:
tasks:
- name: add user
user: name={
{user}}
ansible-playbook 6_1.yaml -e "user=wangwu"
ansible mysql -a 'tail -1 /etc/passwd'
3.6.2 直接在yaml中定义,或者内置变量
vim 6_2.yaml
- hosts: mysql
remote_user: root
vars:
- user: lisi
tasks:
- name: add user
user: name={
{user}}
ansible-playbook 6_2.yaml
ansible mysql -a 'tail -1 /etc/passwd'
vim 6_2.yaml
- hosts: mysql
remote_user: root
tasks:
- name: copy file
copy: content="{
{ansible_all_ipv4_addresses}}" dest=/opt/vars.txt
ansible-playbook 6_2.yaml
ansible mysql -a 'ls /opt'
ansible mysql -a 'cat /opt/vars.txt'
3.6.3 引用主机清单内自定义变量
vim /etc/ansible/hosts
[webserver]
192.168.184.20
[mysql]
192.168.184.30 user=zhaoliu
vim 6_3.yaml
- hosts: mysql
remote_user: root
tasks:
- name: add user
user: name={
{user}}
ansible-playbook 6_3.yaml
ansible mysql -a 'tail -1 /etc/passwd'
3.7 条件测试
如果需要根据变量、facts (setup) 或此前任务的执行结果来作为某task执行与否的前提时要用到条件测试,在Playbook中条件测试使用。在task后添加when子句即可使用条件测试: when子句支持 jinjia2 表达式或语法
3.7.1 单条件判断
vim 7_1.yaml
- hosts: mysql
remote_user: root
tasks:
- name: "shutdown CentOS"
command: /sbin/shutdown -h now
when: ansible_distribution == "CentOS"
ansible-playbook 7_1.yaml
3.7.2 多条件判断
vim 7_2.yaml
- hosts: mysql
remote_user: root
tasks:
- name: "shut down CentOS 7 systems"
command: /sbin/shutdown -r now
when:
- ansible_distribution == "CentOS"
- ansible_distribution_major_version == "7"
ansible-playbook 7_2.yaml
7.6.3 组条件判断
vim 7_3.yml
- hosts: mysql
remote_user: root
tasks:
- name: "shut down CentOS 6 and Debian 7 systems"
command: /sbin/shutdown -t now
when: (ansible_distribution == "CentOS" and ansible_distribution_major_version == "6") or (ansible_distribution == "Debian" and ansible_distribution_major_version == "7")
ansible-playbook 7_3.yaml
7.6.4 迭代
vim 7_5.yaml
- hosts: webserver
remote_user: root
tasks:
- name: install
yum: name={
{item}} state=latest
with_items:
- httpd
- rpcbind
- nfs-utils
ansible-playbook 7_5.yaml
ansible webserver -a 'rpm -q httpd'
ansible webserver -a 'rpm -q rpcbind'
ansible webserver -a 'rpm -q nfs-utils'
也可以自己定义item变量
vim 7_5.yaml
- hosts: webserver
remote_user: root
tasks:
- name: add user && join group
user: name={
{item.x}} state=present group={
{item.y}}
with_items:
- {x: 'qianqi', y: 'wheel'}
- {x: 'sicong', y: 'root'}
ansible-playbook 7_5.yaml
ansible webserver -a 'tail -2 /etc/passwd'
边栏推荐
- Day15:图像和办公文档处理
- Linear regression and logistic regression (logistic regression and linear regression)
- Functions and Objects in the Prototype Chain
- A dream in a cave, a thousand years in the capital of Fujian, the immersive performance of the tunnel of "Dream in the Capital of Mind" made a stunning appearance!
- PHP two-dimensional array to heavy
- WebRTC-NACK、Pacer和拥塞控制和FEC
- ceph主要组件介绍
- ceph-ansible5.0部署文档
- #yyds dry goods inventory# Interview must brush TOP101: determine whether a linked list is a palindrome structure
- mysql Varchar字符存储时报错
猜你喜欢

机器学习之聚类——DBSCAN演绎组织的形成

node的express和微信小程序实现即时通讯聊天

Star set sail: cross-border electricity try heddle area for the majority of sellers to provide more market opportunities for development

如何借助cpolar内网穿透连接本地树莓派(1)

线性回归与逻辑回归 (logistic regression and linear regression)

牛客面试刷题

61:第五章:开发admin管理服务:14:开发【友情链接列表查询,接口】;(核心是:理解MongoDB,查询数据的逻辑)

Open3D Airborne Point Cloud Powerline Extraction

C#事件订阅发布实现原理详解

超强力推!阿里全新微服务突击手册,把所有操作都写出来了|超清PDF
随机推荐
缓存系列:缓存一致性问题的解决思路
UNet语义分割网络
LaunchScreen.storyboard 颜色设置
"UnicodeDecodeError: 'gbk' codec can't decode byte 0xad in position 825: illegal multiby" appears in weditor installation
Functions and Objects in the Prototype Chain
Architecture Camp Graduation Summary
The optimization method to solve the slow loading of the website caused by Google AdSense
逐帧播放1
62:第五章:开发admin管理服务:15:开发【新增/修改友情链接,接口】的修改功能;(其实在60篇博客中,已经开发好了)(核心是:理解MongoDB,修改数据的逻辑)
Hudi(1.0、2.0)简介
Apifox和Apipost有什么区别?那个更有优势(接口工具)postman、jmeter等等、、、
[LeetCode]1403. 非递增顺序的最小子序列
Pytest学习-参数化parametrize
SMW0 SAP上传模板
PAT甲级:1049 Counting Ones
JVM学习之 内存结构
Day13 :进程和线程
rce code and command execution vulnerability and file inclusion
MySQL数据库成为瓶颈后,动态数据的查询要如何加速?
得不到你的心,就用“分布式锁”锁住你的人