[ansible] playbook运用

news/2025/4/26 12:13:21/

一、复习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.ppmy.cn/news/1357167.html

相关文章

【Linux】主机搭建 Linux服务器环境 笔记

目录 前言选择系统软件1. 用U盘装系统2. 安装 Centos7.93. 网络套件 应用软件1. ngnix2. 防火墙配置3. nodejs 后记 前言 过年买了个 mini 主机当玩具玩一下,这里记录下。 选择 已有主力机 (windows) 的情况下,使用过如下四种 Linux宿主环境。这里总…

算法-链表操作

题目 1)输入链表头节点,奇数长度返回中点,偶数长度返回上中点 2)输入链表头节点,奇数长度返回中点,偶数长度返回下中点 3)输入链表头节点,奇数长度返回中点前一个,偶数长…

Leetcode 3040. Maximum Number of Operations With the Same Score II

Leetcode 3040. Maximum Number of Operations With the Same Score II 1. 解题思路2. 代码实现 题目链接:3040. Maximum Number of Operations With the Same Score II 1. 解题思路 这一题的话思路就是一个动态规划,显然对于每一种情况都有3种可能的…

Linux归档命令cpio

cpio 是一种在 Unix 和类 Unix 系统(如 Linux)上用于创建和提取归档文件的工具。与 tar 命令类似,cpio 可以用来打包和解包文件和目录,但它在某些方面提供了不同的功能和选项。cpio 命令通常与 find 命令结合使用,以创…

【实战】一、Jest 前端自动化测试框架基础入门(一) —— 前端要学的测试课 从Jest入门到TDD BDD双实战(一)

文章目录 一、前端要学的测试课1.前端要学的测试2.前端工程化的一部分3.前端自动化测试的例子4.前端为什么需要自动化测试?5.课程涵盖内容6.前置技能7.学习收获 二、Jest 前端自动化测试框架基础入门1. 自动化测试背景及原理前端自动化测试产生的背景及原理 2.前端自…

[AIGC] Kong:一个强大的 API 网关和服务平台

Kong(Kong API Gateway)是一个开源 and 免费的 API 网关 and 服务平台,它可以用来管理和控制 API 的生命周期和流量。Kong 是一个可扩展的、可靠的 and 高性能的平台,支持 millions 个 API 和 billions 的请求。Kong 已经成为当今…

Java,SpringBoot中导出excel文件

依赖 <dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>4.1.2</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml<…

服务器遭受 DDoS 攻击的常见迹象有哪些?

服务器遭受 DDoS 攻击的现象很常见&#xff0c;并且有时不容易预防&#xff0c;有部分原因是它们的形式多种多样&#xff0c;而且黑客手段越来越隐蔽。如果您怀疑自己可能遭受 DDoS 攻击&#xff0c;可以寻找多种迹象。以下是 DDoS 攻击的5个常见迹象&#xff1a; 1.网络流量无…