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

[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

 

相关文章:

[ansible] playbook运用

一、复习playbook剧本 --- - name: first play for install nginx #设置play的名称gather_facts: false #设置不收集facts信息hosts: webservers:dbservers #指定执行此play的远程主机组remote_user: root #指定执行此play的用…...

MSSQL运用

做过的事情,隔几年又再做相同的事情,做一下记录。 角色与权限 创建账号与设定执行存储过程权限 Use testDB CREATE LOGIN acct WITH PASSWORDp1 CREATE USER acct FOR LOGIN acct GO GRANT EXECUTE ON SP_Test TO acct; GO 存储过程 调用写好的SQL语…...

linux命令--pidof

文章目录 linux命令--pidof linux命令–pidof pidof 是Linux系统中用来查找正在运行进程的进程号(pid)的工具,功能类似pgrep和ps。 pidof命令用于查找指定名称的进程的进程号id号。 语法 pidof(选项)(参数) 选项 -s:仅返回一个进程号&…...

计算机视觉发展的方向和潜在机会

计算机视觉发展的方向 文章目录 计算机视觉发展的方向计算机视觉发展的方向潜在机会 计算机视觉发展的方向 未来计算机视觉发展的方向可能包括以下几个方面: 深度学习和神经网络:深度学习已经成为计算机视觉领域的重要技术,未来将继续深入研…...

Java Web(六)--XML

介绍 官网:XML 教程 为什么需要: 需求 1 : 两个程序间进行数据通信?需求 2 : 给一台服务器,做一个配置文件,当服务器程序启动时,去读取它应当监听的端口号、还有连接数据库的用户名和密码。spring 中的…...

智慧城市的新宠儿:会“思考”的井盖

在城市化飞速发展的今天,我们或许未曾过多地关注那些平凡却至关重要的井盖。它们无声地矗立在城市的每个角落,守护着深藏于地下的城市生命线,然而,这些井盖并未满足于传统的角色,它们正逐步融入智慧城市的宏大画卷中&a…...

Linux限定网络和工具环境下时间同步

使用curl或wget工具同步某网站时间到本地环境。 使用curl工具 #!/bin/bash# Replace example.com with the domain you want to query url"http://example.com"# Fetch HTTP header and extract Date field date_str$(curl -sI "$url" | grep -i "^…...

SQL Server查询计划(Query Plan)——文本查询计划

​​​​​ 6.4.1. 文本查询计划 SQL Server中,除了通过GUI工具获取图形查询计划外,我们还可以通过相关命令获取文本格式的查询计划,这里惯称其为文本查询计划。文本查询计划中,SQL Server通过单独的一行来表示查询计划中的每个操作符,通过缩进格式和竖线(字符“|”)来…...

2024年2月的TIOBE指数,go语言排名第8,JAVA趋势下降

二月头条:go语言进入前十 本月,go在TIOBE指数前10名中排名第8。这是go有史以来的最高位置。当谷歌于2009年11月推出Go时,它一炮而红。在那些日子里,谷歌所做的一切都是神奇的。在Go出现的几年前,谷歌发布了GMail、谷歌…...

机器人十大前沿技术(2023-2024年)

2023-2024年机器人十大前沿技术 1. 具身智能与垂直大模型 具身智能是指拥有自主感知、交互和行动能力的智能体,能够与环境进行实时互动,从而实现对环境的理解和适应。 “大模型”是指在深度学习和人工智能领域中,使用大量参数和数据进行训…...

Spring: MultipartFile和File的区别

文章目录 一、MultipartFile和File对比1、 MultipartFile:2、File: 一、MultipartFile和File对比 MultipartFile 和 File 是用于处理文件上传的两种不同类型,主要在不同的编程环墨境中使用。 1、 MultipartFile: - MultipartFi…...

ncnn之三(补充):window环境下vs2022安装ncnn+protobuf

启动VS2022 下面的 x64 Native Tools Command Prompt for VS2022 protobuf git clone gitgithub.com:protocolbuffers/protobuf.git# 或者 下载 https://github.com/google/protobuf/archive/v3.11.2.zip cmake -G"NMake Makefiles" -DCMAKE_BUILD_TYPERelease -D…...

第五篇【传奇开心果系列】Python文本和语音相互转换库技术点案例示例:详细解读pyttsx3的`preprocess_text`函数文本预处理。

传奇开心果短博文系列 系列短博文目录Python文本和语音相互转换库技术点案例示例系列 短博文目录前言一、pyttsx3的preprocess_text函数文本预处理基本用法示例代码二、实现更复杂的文本预处理逻辑示例代码三、去除停用词、词干提取示例代码四、词形还原、拼写纠正示例代码五、…...

logback实践

1:日志区分环境 2:debug info warn error日志文件不一样 3: 文件滚动日志 4:启动可带参数 --spring.profiles.activedev --log.levelinfo 5:可从配置文件中获取上下文参数 logback-spring.xml 放在 classpath 下面 <configuration scan"false" scanPer…...

深入理解java虚拟机---自动内存管理

2.2 运行时数据区域 Java虚拟机在执行Java程序的过程中会把它所管理的内存划分为若干个不同的数据区域。这些区域有各自的用途&#xff0c;以及创建和销毁的时间&#xff0c;有的区域随着虚拟机进程的启动而一直存在&#xff0c;有些区域则是依赖用户线程的启动和结束而建立和销…...

粉笔规范词积累(文化发展)

活态保护/活态传承 基本释义 是在文化遗产生成发展的环境当中进行保护和传承&#xff0c;在人民群众生产生活过程中进行传承与发展。 应用场景 当资料中出现“让文化遗产不仅‘活’在历史中&#xff0c;更‘活’在人们的生产生活中”等类似表述&#xff0c;可概括为“活态保…...

如何在Ubuntu部署Emlog,并将本地博客发布至公网可远程访问

文章目录 前言1. 网站搭建1.1 Emolog网页下载和安装1.2 网页测试1.3 cpolar的安装和注册 2. 本地网页发布2.1 Cpolar临时数据隧道2.2.Cpolar稳定隧道&#xff08;云端设置&#xff09;2.3.Cpolar稳定隧道&#xff08;本地设置&#xff09; 3. 公网访问测试总结 前言 博客作为使…...

Axios

Axios简介 axios框架全称&#xff08;ajax – I/O – system&#xff09;&#xff1a; 基于promise用于浏览器和node.js的http客户端&#xff0c;因此可以使用Promise API 一、axios是干啥的 说到axios我们就不得不说下Ajax。在旧浏览器页面在向服务器请求数据时&#xff0…...

数据仓库选型建议

1 数仓分层 1.1 数仓分层的意义 **数据复用&#xff0c;减少重复开发&#xff1a;**规范数据分层&#xff0c;开发一些通用的中间层数据&#xff0c;能够减少极大的重复计算。数据的逐层加工原则&#xff0c;下层包含了上层数据加工所需要的全量数据&#xff0c;这样的加工方…...

每日一题——LeetCode1470.重新排列数组

方法一 把数组的前n项看做一个数组&#xff0c;后n项看做一个数组&#xff0c;两个数组循环先后往res里push元素 var shuffle function(nums, n) {let res[]for(let i0;i<n;i){res.push(nums[i])res.push(nums[in])}return res }; 消耗时间和内存情况&#xff1a; 方法二…...

基于FPGA的PID算法学习———实现PID比例控制算法

基于FPGA的PID算法学习 前言一、PID算法分析二、PID仿真分析1. PID代码2.PI代码3.P代码4.顶层5.测试文件6.仿真波形 总结 前言 学习内容&#xff1a;参考网站&#xff1a; PID算法控制 PID即&#xff1a;Proportional&#xff08;比例&#xff09;、Integral&#xff08;积分&…...

Python:操作 Excel 折叠

💖亲爱的技术爱好者们,热烈欢迎来到 Kant2048 的博客!我是 Thomas Kant,很开心能在CSDN上与你们相遇~💖 本博客的精华专栏: 【自动化测试】 【测试经验】 【人工智能】 【Python】 Python 操作 Excel 系列 读取单元格数据按行写入设置行高和列宽自动调整行高和列宽水平…...

土地利用/土地覆盖遥感解译与基于CLUE模型未来变化情景预测;从基础到高级,涵盖ArcGIS数据处理、ENVI遥感解译与CLUE模型情景模拟等

&#x1f50d; 土地利用/土地覆盖数据是生态、环境和气象等诸多领域模型的关键输入参数。通过遥感影像解译技术&#xff0c;可以精准获取历史或当前任何一个区域的土地利用/土地覆盖情况。这些数据不仅能够用于评估区域生态环境的变化趋势&#xff0c;还能有效评价重大生态工程…...

MySQL用户和授权

开放MySQL白名单 可以通过iptables-save命令确认对应客户端ip是否可以访问MySQL服务&#xff1a; test: # iptables-save | grep 3306 -A mp_srv_whitelist -s 172.16.14.102/32 -p tcp -m tcp --dport 3306 -j ACCEPT -A mp_srv_whitelist -s 172.16.4.16/32 -p tcp -m tcp -…...

视觉slam十四讲实践部分记录——ch2、ch3

ch2 一、使用g++编译.cpp为可执行文件并运行(P30) g++ helloSLAM.cpp ./a.out运行 二、使用cmake编译 mkdir build cd build cmake .. makeCMakeCache.txt 文件仍然指向旧的目录。这表明在源代码目录中可能还存在旧的 CMakeCache.txt 文件,或者在构建过程中仍然引用了旧的路…...

高考志愿填报管理系统---开发介绍

高考志愿填报管理系统是一款专为教育机构、学校和教师设计的学生信息管理和志愿填报辅助平台。系统基于Django框架开发&#xff0c;采用现代化的Web技术&#xff0c;为教育工作者提供高效、安全、便捷的学生管理解决方案。 ## &#x1f4cb; 系统概述 ### &#x1f3af; 系统定…...

【Linux】Linux安装并配置RabbitMQ

目录 1. 安装 Erlang 2. 安装 RabbitMQ 2.1.添加 RabbitMQ 仓库 2.2.安装 RabbitMQ 3.配置 3.1.启动和管理服务 4. 访问管理界面 5.安装问题 6.修改密码 7.修改端口 7.1.找到文件 7.2.修改文件 1. 安装 Erlang 由于 RabbitMQ 是用 Erlang 编写的&#xff0c;需要先安…...

yaml读取写入常见错误 (‘cannot represent an object‘, 117)

错误一&#xff1a;yaml.representer.RepresenterError: (‘cannot represent an object’, 117) 出现这个问题一直没找到原因&#xff0c;后面把yaml.safe_dump直接替换成yaml.dump&#xff0c;确实能保存&#xff0c;但出现乱码&#xff1a; 放弃yaml.dump&#xff0c;又切…...

接口 RESTful 中的超媒体:REST 架构的灵魂驱动

在 RESTful 架构中&#xff0c;** 超媒体&#xff08;Hypermedia&#xff09;** 是一个核心概念&#xff0c;它体现了 REST 的 “表述性状态转移&#xff08;Representational State Transfer&#xff09;” 的本质&#xff0c;也是区分 “真 RESTful API” 与 “伪 RESTful AP…...

react更新页面数据,操作页面,双向数据绑定

// 路由不是组件的直接跳转use client&#xff0c;useEffect&#xff0c;useRouter&#xff0c;需3个结合&#xff0c; use client表示客户端 use client; import { Button,Card, Space,Tag,Table,message,Input } from antd; import { useEffect,useState } from react; impor…...