[ansible] playbook运用

一、复习playbook剧本

---
- name: first play for install nginx   #设置play的名称gather_facts: false                  #设置不收集facts信息hosts: webservers:dbservers          #指定执行此play的远程主机组remote_user: root                    #指定执行此play的用户tasks:                               #指定此play的任务列表- name: disabled firewalldservice: name=firewalld  state=stopped  enabled=no- name: disable selinuxcommand: '/sbin/setenforce 0'ignore_errors: yes- name: disabled selinux foreverreplace: path=/etc/selinux/config  regexp=enforcing  replace=disabled  after=loaded- name: mount cdrommount: src=/dev/sr0 path=/mnt fstype=iso9660 state=mounted- name: install pkgsyum: name=pcre-devel,zlib-devel,openssl-devel,gcc,gcc-c++,make state=latest- name: create nginx useruser: name=nginx create_home=no shell=/sbin/nologin- name: unarchive nginx packageunarchive: copy=yes src=/etc/ansible/nginx/nginx-1.24.0.tar.gz dest=/opt/- name: install nginx by sourceshell: chdir=/opt/nginx-1.24.0/ ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module && make && make install- name: create link file for nginxfile: state=link src=/usr/local/nginx/sbin/nginx path=/usr/local/sbin/nginx- name: create nginx service filecopy: src=nginx.service dest=/lib/systemd/system/nginx.service- name: start nginxservice: name=nginx state=started enabled=yes

 

 

二、playbook的定义、引用变量

2.1 基础变量的定义与引用

在yaml文件中,我们可以在初始配置的模块中用var去定义变量的存在,变量的格式为key:value,以此来确定该变量在剧本中的存在

vim test1.yaml
---
- name: this is a play for testing variableshosts: dbserversremote_user: rootvars:filename: abc.txttasks:- name: touch a test filefile: path=/opt/{{filename}} state=touchansible-playbook test1.yaml

 

2.2 引用fact信息中的变量  

首先我们知道  使用 ansible 组  -m setup   可以收集该组中所有的节点信息 ,

所以setup中fact'信息,有时候会剧本编写中需要,而fact的信息也是可以通过变量的方式进行调用

vim test2.yaml
---
- name: this is a playbook for quote variatehosts: dbserversremote_user: roottasks:- name: reading setup fact variatedebug: msg={{ansible_date_time.weekday}}
~                                                 

 

三、playbook中的when条件判断和变量循环使用 

3.1 when条件判断 

#选用filter=ansible_default_ipv4中的address作为when条件进行测试
ansible all -m setup -a 'filter=ansible_default_ipv4'

vim test3.yaml
---
- name: this is when test playbookhosts: allremote_user: roottasks:- name: test whendebug: msg='判断位置'when: ansible_default_ipv4.address == "192.168.136.198"ansible-playbook test3.yaml

 

除此之外 when条件还可以通过 !=(不等于条件来进行判断) 

vim test3.yaml
---
- name: this is when test playbookhosts: allremote_user: roottasks:- name: test whendebug: msg='判断位置'when: ansible_default_ipv4.address != "192.168.136.198"
ansible-playbook test3.yaml

四、变量循环 

(1)with_item 单循环输出
vim test4.yaml
---
- name: item testhosts: dbserversremote_user: rootgather_facts: notasks:- debug:msg: "{{item}}"with_items: [a, b, c, d]ansible-playbook test4.yaml

 

 当列表为两个时。with_item的输出方式:

vim test4.yaml
---
- name: item testhosts: dbserversremote_user: rootgather_facts: notasks:- debug:msg: "{{item}}"with_items:- [a, b, c, d]- [1 ,2, 3, 4]
ansible-playbook test4.yaml                      

 

(2)with_list  每组列表一起循环的输出 
---
- name: item testhosts: dbserversremote_user: rootgather_facts: notasks:- debug:msg: "{{item}}"with_list:- [a, b, c, d]- [1 ,2, 3, 4]
~                                                                                                                                           
~                            

(3)with_together 同一列表位置数据组合输出的循环 

---
- name: item testhosts: dbserversremote_user: rootgather_facts: notasks:- debug:msg: "{{item}}"with_together:- [a, b, c, d]- [1 ,2, 3, 4]
~                        

 

---
- name: item testhosts: dbserversremote_user: rootgather_facts: notasks:- debug:msg: "{{item}}"with_together:- [a, b, c, d]- [1 ,2, 3, 4]- [A, B, C]

 

(4) with_nested 列表数据循环匹配的循环(根据列表个数定义有多少层的循环) 
---
- name: item testhosts: dbserversremote_user: rootgather_facts: notasks:- debug:msg: "{{item}}"with_nested:- [a, b, c, d]- [1 ,2, 3, 4]
~                      

 

四种迭代循环方式的总结

whith_items:  {{item}}会把所有的列表展开进行遍历输出,with_flattened也可以替代with_items

 with_list:    {{item}}会把每个列表当作一个整体输出。如果每个列表中只有一个值,则效果与with items一致。loop也可以替代ith

 with_together: {{item}}引用时会把每个列表相同位置的值对齐合并后输出

with nested:{ {item}}引用时会把每个列表的值两两组合循环输出

五、Templates 模块

Jinja是基于Python的模板引擎。Template类是Jinja的一个重要组件,可以看作是一个编译过的模板文件,用来产生目标文本,传递Python的变量给模板去替换模板中的标记。 

 本次我们以改变apche的配置文件为例,来展现Templates模块的运用

(1)先准备一个以 .j2 为后缀的 template 模板文件,设置引用的变量 
#如果没有相关的httpd的配置文件,可以先yum按住一个httpd的服务,取其主配置文件
cp httpd.conf /etc/ansible/httpd/httpd.conf.j2vim /etc/ansible/httpd/httpd.conf.j2
Listen {{http_port}}				#42行,修改
ServerName {{server_name}}			#95行,修改
DocumentRoot "{{root_dir}}"          #119行,修改

(2) 修改主机清单文件,使用主机变量定义一个变量名相同,而值不同的变量 
vim /etc/ansible/hosts       
[webservers]
192.168.136.197 http_port=192.168.136.197:80 server_name=www.test1.com:80 root_dir=/etc/httpd/htdocs[dbservers]
192.168.136.198 http_port=192.168.136.198:80 server_name=www.test2.com:80 root_dir=/etc/httpd/htdocs

 此外如果没有做DNS解析域名,还需要对主机名进行映射 :

vim /etc/hosts192.168.73.106 www.test1.com
192.168.73.107 www.test2.com
(3)编写 playbook 
mkdir /etc/ansible/templates
vim apache.yaml
---
- hosts: allremote_user: rootvars:- package: httpd- service: httpdtasks:- name: install httpd packageyum: name={{package}} state=latest- name: install configure filetemplate: src=/etc/ansible/httpd/httpd.conf.j2 dest=/etc/httpd/conf/httpd.confnotify:- restart httpd- name: create root dirfile: path=/etc/httpd/htdocs state=directory- name: start httpd serverservice: name={{service}} enabled=true state=startedhandlers:- name: restart httpdservice: name={{service}} state=restartedansiable-playbook apache.yaml

 

​​​​​​​

六、Tags 

可以在一个playbook中为某个或某些任务定义“标签”,在执行此playbook时通过ansible-playbook命令使用--tags选项能实现仅运行指定的tasks。
playbook还提供了一个特殊的tags为always。作用就是当使用always作为tags的task时,无论执行哪一个tags时,定义有always的tags都会执行。

6.1 单标签的使用

vim test10.yaml
---
- name: this is a play for testing variableshosts: dbserversremote_user: rootvars:filename: abc.txttasks:- name: position 1debug:msg: 'ls /opt'tags:- only- name: position 2debug:msg: 'ls /mnt'ansible-playbook test1.yaml --tags="only"

6.2 多标签的运用

---
- name: this is a play for testing variableshosts: dbserversremote_user: rootvars:filename: abc.txttasks:- name: position 1debug:msg: '测试标签1'tags:- one- name: position 2debug:msg: '测试标签2'tags:- two- name: position 3debug:msg: '测试标签3'tags:- one

 

6.3 通用标签always的运用  

---
- name: this is a play for testing variableshosts: dbserversremote_user: rootvars:filename: abc.txttasks:- name: position 1debug:msg: '测试标签1'tags:- one- name: position 2debug:msg: '测试通用标签always'tags:- always- name: position 3debug:msg: '测试标签3'tags:- one

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/690849.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【Java万花筒】选择最适合您的数据序列化格式:比较 Avro、Protocol Buffers、JSON、XML、MessagePack和BSON

选择最佳数据序列化格式:找到适合您的解决方案 前言 在当今数据驱动的世界中,高效地处理和传输数据变得至关重要。选择合适的数据序列化格式对于数据存储、通信和处理的性能至关重要。本文将介绍并比较几种常用的数据序列化格式,包括Apache…

python绘制k线图均线图

AAPL.csv 数据文件 Date,Close,Volume,Open,High,Low 06/23/2023,$186.68,53117000,$185.55,$187.56,$185.01 06/22/2023,$187.00,51245330,$183.74,$187.045,$183.67 06/21/2023,$183.96,49515700,$184.90,$185.41,$182.5901 06/20/2023,$185.01,49799090,$184.41,$1…

2.19C语言学习

P8597 [蓝桥杯 2013 省 B] 翻硬币 因为把相邻两个硬币翻转两次相当于不翻,所以最优方案中同一组硬币最多只会翻转一次,故翻转顺序无后效性,考虑贪心:从前往后比较,发现一个不同的硬币就把它和他后面的硬币翻转&#x…

单例模式的几种实现方式

在Java中,实现单例模式主要有几种方式:懒汉式、饿汉式、双重检查锁定、静态内部类和枚举。每种方式都有其特点和适用场景。 1. 饿汉式(线程安全) 饿汉式是最简单的一种实现方式,通过静态初始化实例,保证了…

springboot 事务管理 详细介绍

在Spring Boot中,事务管理是一种关键的特性,它确保了数据的完整性和一致性。Spring Boot提供了对事务管理的内置支持,使得在应用程序中使用事务变得非常简单。 事务管理的基本概念 事务是指一系列操作,这些操作要…

15-55V输入自动升降压 光伏MPPT自动跟踪充电方案 大功率300瓦

1.MPPT原理--简介 MPPT,全称为Maximum Power Point Tracking,即最大功点跟踪,它是一种通过调节电气模块的工作状态,使光伏板能够输出更多电能的电气系统能够将太阳能电池板发出的直流电有效地贮存在蓄电池中,可有效地…

【蓝桥杯】算法模板题(Floyd算法)

一.弗洛伊德算法 用途:用来求解多源点最短路径问题。 思想:Floyd算法又称为插点法,是一种利用动态规划的思想寻找给定的加权图中多源点之间最短路径的算法。 主要步骤: 1)初始化:使用邻接矩阵初始化dis…

第十四届“中关村青联杯”全国研究生数学建模竞赛-A题:无人机在抢险救灾中的优化运用

目录 摘 要: 1 问题重述 1.1 问题背景 1.2 待解决的问题 2 模型假设及符号说明

GitHub仓库文件部署

目录 软件下载和安装 git创建仓库 Github仓库配置 git管理软件配置 Git管理 软件下载和安装 首先需要下载git,以及git管理软件,对其进行安装。 git创建仓库 首先需要创建仓库,在本地仓库文件夹cmd之后输入以下指令创建git仓库文件。 …

解决laravel-admin安装报错1071 Specified key was too long问题

在执行php artisan admin:install命令安装laravel-admin的时候,如果你使用的数据库是MySQL v5.7.7以下版本就会报下面的错: SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 1000 bytes (SQL:…

课时39:表达式_运算符_简单计算

3.1.2 简单计算 学习目标 这一节,我们从 [ ] 、 l e t 、 ( ( ) ) 、 []、let、(())、 []、let、(())、(())、小结 五个方面来学习。 $[] 简介 $[]方法,常用于整数计算场景,适合不太复杂的计算,运算结果是小数的也会自动取整…

递归读取文件夹下的所有文件

水一篇文章 🐶 代码 package file;import org.apache.commons.lang3.StringUtils; import org.junit.Test;import java.io.File; import java.util.Objects;/*** FlattenDirFiles** author allens* date 2024/2/19*/ public class FlattenDirFiles {// 文件数量pri…

小米空气净化器2s使用体验

这个产品最早上市是2017年,我买回来实际上只用了1年就弃用了,性能不行,使用体验也不好。 打算买新的空气净化器,抽空吐槽一下。 这个净化器发售价是899,在当时来说算中下水平的,小米的,有米家…

什么是跨端,常用的跨端技术

跨平台是跨操作系统,跨端是指客户端 常见的客户端有,web、android、ios 等,客户端的特点是有界面、由逻辑,所以包含逻辑跨端和渲染跨端。 常用的跨端技术方案 React Native: 由 Facebook 推出的开源框架,…

Spring Boot与LiteFlow:轻量级流程引擎的集成与应用含完整过程

点击下载《Spring Boot与LiteFlow:轻量级流程引擎的集成与应用含完整过程》添加链接描述 1. 前言 本文旨在介绍Spring Boot与LiteFlow的集成方法,详细阐述LiteFlow的原理、使用流程、步骤以及代码注释。通过本文,读者将能够了解LiteFlow的特…

数据分析师SQL面试准备(part1)

1. SQL 万能框架 2. SQL的书写顺序,跟程序真的执行顺序不同 3. 4. 5. 6. 7. case when utilization 8. 9. 10. 11.

OpenHarmony—UIAbility组件间交互(设备内)

UIAbility是系统调度的最小单元。在设备内的功能模块之间跳转时,会涉及到启动特定的UIAbility,该UIAbility可以是应用内的其他UIAbility,也可以是其他应用的UIAbility(例如启动三方支付UIAbility)。 本章节将从如下场…

多维时序 | Matlab实现LSTM-Mutilhead-Attention长短期记忆神经网络融合多头注意力机制多变量时间序列预测模型

多维时序 | Matlab实现LSTM-Mutilhead-Attention长短期记忆神经网络融合多头注意力机制多变量时间序列预测模型 目录 多维时序 | Matlab实现LSTM-Mutilhead-Attention长短期记忆神经网络融合多头注意力机制多变量时间序列预测模型预测效果基本介绍程序设计参考资料 预测效果 基…

01.Redis中间件实战1

Redis中间件实战1 Redis各种数据结构实战 字符串 需求:将用户信息存储至缓存中,实现每次前端请求获取用户个人详情时直接从缓存中获取。来演示字符串的写入与读取。 技术方案:为了实现这个需求,首先需要建立用户对象实体,里面包含用户个人的各种信息,包括ID、年龄、姓…

Windows 命令提示符(CMD)操作(三):用户管理

三、用户管理 3.1 net user 管理用户帐户,如创建、删除用户 一般语法: net user [username [password | *] [options]] [/domain] [/add] [/delete] [/expires:{date | never}] [/active:{yes | no}]常见选项和参数包括: [username]&…