2.playbook剧本

文章目录

  • playbook剧本
    • 创建剧本
    • 运行剧本
    • 定义和引用变量
    • 指定远程主机sudo切换用户
    • when条件判断
    • 剧本格式
    • 迭代
      • with_items
      • with_list
      • with_flattened
      • with_together
      • with_cartesian
      • with_nested
    • Templates模块
    • tags模块

playbook剧本

  • playbooks 本身由以下各部分组成
    1. Tasks:任务,即通过 task 调用 ansible 的模板将多个操作组织在一个 playbook 中运行
    2. Variables:变量
    3. Templates:模板
    4. Handlers:处理器,当changed状态条件满足时,(notify)触发执行的操作
    5. Roles:角色

创建剧本

##示例:
vim test1.yaml


---     #yaml文件以---开头,以表明这是一个yaml文件,可省略

- name: first play       #定义一个play的名称,可省略
  gather_facts: false    #设置不进行facts信息收集,这可以加快执行速度,可省略
  hosts: webservers      #指定要执行任务的被管理主机组,如多个主机组用冒号分隔
  remote_user: root      #指定被管理主机上执行任务的用户
  tasks:                 #定义任务列表,任务列表中的各任务按次序逐个在hosts中指定的主机上执行
   - name: test connection    #自定义任务名称
     ping:               #使用 module: [options] 格式来定义一个任务
   - name: disable selinux
     command: '/sbin/setenforce 0'    #command模块和shell模块无需使用key=value格式
    
    ignore_errors: True          
#如执行命令的返回值不为0,就会报错,tasks停止,可使用 ignore_errors 忽略失败的任务

   - name: disable firewalld
   
     service: name=firewalld state=stopped   
     #使用 module: options 格式来定义任务,option使用key=value格式
     
   - name: install httpd
     yum: name=httpd state=latest
   - name: install configuration file for httpd
     copy: src=/opt/httpd.conf dest=/etc/httpd/conf/httpd.conf    
     #这里需要一个事先准备好的/opt/httpd.conf文件
     
     notify: "restart httpd"    
     #如以上操作后为changed的状态时,会通过notify指定的名称触发对应名称的handlers操作
     
   - name: start httpd service
     service: enabled=true name=httpd state=started
   
#handlers中定义的就是任务,此处handlers中的任务使用的是service模块
  handlers:    
   - name: restart httpd         #notify和handlers中任务的名称必须一致
     service: name=httpd state=restarted
     
     
##Ansible在执行完某个任务之后并不会立即去执行对应的handler,而是在当前play中所有普通任务都执行完后再去执行handler,这样的好处是可以多次触发notify,但最后只执行一次对应的handler,从而避免多次重启。
##创建安装apache服务的剧本

vim demo1.yaml
- name: the first play for install apache
  gather_facts: false
  hosts: dbservers
  remote_user: root
  tasks:
  - name: disable firewalld
    service: name=firewalld state=stopped enabled=no
  - name: disable selinux
    command: '/usr/sbin/setenforce 0'
    ignore_errors: true
  - name: disable selinux forever
    replace: path=/etc/selinux/config regexp='enforcing' replace='disabled'
  - name: mount cdrom
    mount: src=/dev/sr0 path=/mnt fstype=iso9660 state=mounted 
  - name: install apache
    yum: name=httpd state=latest
  - name: prepare httpd configuration file
    copy: src=/opt/httpd.conf dest=/etc/httpd/conf/httpd.conf
    notify: "reload httpd"
  - name: start httpd
    service: name=httpd state=started enabled=yes
  handlers:
  - name: reload httpd
    service: name=httpd state=reloaded    
##执行playbook剧本

ansible-playbook demo1.yaml

在这里插入图片描述

运行剧本

-k(–ask-pass):用来交互输入ssh密码

-K(-ask-become-pass):用来交互输入sudo密码

-u:指定用户

ansible-playbook demo1.yaml --syntax-check    
#检查yaml文件的语法是否正确

ansible-playbook demo1.yaml --list-task       
#检查tasks任务

ansible-playbook demo1.yaml --list-hosts     
#检查生效的主机

ansible-playbook demo1.yaml --start-at-task='install httpd'   
#指定从某个task开始运行

在这里插入图片描述

定义和引用变量

vim demo2.yaml
- name: second play
  hosts: dbservers
  reomte_user: root
  vars:
  - username: zhangsan
  - groupname: abc
  - filename: /opt/123.txt
  - uid: 9527
  gather_facts: true
  tasks:
  - name: create group
    group: name={{groupname}} gid=2800
  - name: create user join group
    user: name={{usernsme}} uid={{uid}} groups={{groupname}}
  - name: copy file
    copy: content="{{ansible_default_ipv4.address}}" dest={{filename}}
  - name: modify username and groupname of file
    file: path={{filename}} owner={{username}} group={{groupname}}

在这里插入图片描述

##运行playbook剧本

ansible-playbook demo2.yaml

在这里插入图片描述

###也可以在命令行中定义变量

ansible-playbook demo2.yaml -e "username=lisi" -e "groupname=nba" -e "filename=/opt/456.txt" -e "uid=9528"

指定远程主机sudo切换用户

##修改 ssh配置文件,禁止  root  用户直接登陆

vim /etc/ssh/sshd_config

#PermitRootLogin yes
##将这个配置项设置为no,就能够禁止root用户登陆

在这里插入图片描述

###设置用户密码

echo 123 | passwd --stdin zhangsan
###给登陆用户提权

vim /etc/sudoers

#添加

zhangsan ALL=ALL

在这里插入图片描述

##修改剧本文件

vim demo2.yaml
- name: second play
  hosts: dbservers
  remote_user: zhangsan
  become: yes             #2.6版本以后的参数,之前是sudo,意思为是否切换用户运行,就是是否提权
  become_user: root               #指定sudo用户为root
  vars:
  - username: zhangsan
  - groupname: abc
  - filename: /opt/123.txt
  - uid: 9527
  gather_facts: true
  tasks:
  - name: create group
    group: name={{groupname}} gid=2800
  - name: create user join group
    user: name={{username}} uid={{uid}} groups={{groupname}}
  - name: copy file
    copy: content="{{ansible_default_ipv4.address}}" dest={{filename}}
  - name: modify username and groupname of file
    file: path={{filename}} owner={{username}} group={{groupname}}

在这里插入图片描述

##运行剧本文件

ansible-playbook demo2.yaml -e "username=lisi" -e "groupname=nba" -e "filename=/opt/456.txt" -e "uid=9528" -k -K

SSH password:               ##输入SSH密码,但这里是配置文件中登陆的用户的密码

在这里插入图片描述

when条件判断

  • 在Ansible中,提供的唯一一个通用的条件判断是when指令,当when指令的值为true时,则该任务执行,否则不执行该任务
- name: third play
  hosts: all
  remote_user: root
  tasks:
  - name: touch file
    file: path=/opt/789.txt state=touch
    when: ansible_default_ipv4.address != "192.168.242.68"
    #when: inventory_hostname == "192.168.242.68"

在这里插入图片描述
在这里插入图片描述

剧本格式

- name: fouth play
  hosts: dbservers
  remote_user: root
  vars:
    myfile:
    - /opt/a
    - /opt/b
    - /opt/c
    - /opt/d
  tasks:
  - name: touch directory
    with_items: "{{myfile}}"
    file: path={{item}} state=directory

  - name: touch file
    with_items:
    - /root/a
    - /root/b
    - /root/c
    - /root/d
    file:
      path: "{{item}}"
      state: touch
task任务 模块语法格式:
横向格式:
模块名: 参数选项1=值  参数选项2={{变量名}}  ...

纵向格式:
模块名:
  参数选项1: 值
  参数选项2: "{{变量名}}"

迭代

with_items

with_items 和 变量 的语法格式
横向格式:
with_items: ["值1", "值2", "值3"]

值为对象(键值对字段)时:
with_items:
- {key1: value1, key2: value2, ...}
- {key1: value3, key2: value4, ...}

纵向格式:
with_items:
- 值1
- 值2
- 值3

值为对象(键值对字段)时:
with_items:
- key1: value1
  key2: value2
- key1: value3
  key2: value4
- name: fifth play
  hosts: dbservers
  remote_user: root
  tasks:
  - name: touch file
    with_items:
    - {filename: /opt/a, username: zhangsan, groupname: nba}
    - {filename: /opt/b, username: lisi, groupname: abc}
    file: path={{item.filename}}  owner={{item.username}} group={{item.groupname}} state=touch

  - name: create dir
    with_items:
    - filename: /opt/cd
      username: zhangsan
      groupname: nba
    - filename: /opt/ef
      username: lisi
      groupname: abc
    file:
      path: "{{item.filename}}"
      owner: "{{item.username}}"
      group: "{{item.groupname}}"
      state: directory

在这里插入图片描述

with_list

- hosts: dbservers
  remote_user: root
  gather_facts: no
  tasks:
  - debug:
      msg: "{{item}}"
    with_list:
    - [ 1, 2, 3 ]
    - [ a, b ]

在这里插入图片描述

with_flattened

- hosts: dbservers
  remote_user: root
  gather_facts: no
  tasks:
  - debug:
      msg: "{{item}}"
    with_flattened:
    - [ 1, 2, 3 ]
    - [ a, b ]

在这里插入图片描述

  • with_list、with_items、with_flattened之间的区别:
    • 在处理简单的单层列表时,他们没有区别,
    • 但是当处理嵌套的多层列表时,with_items与with_flattened会将嵌套列表”拉平展开”,循环的处理每个元素,
    • 而with_list只会处理最外层的列表,将最外层的列表中的每一项循环处理,loop可以替代with_list。

with_together

- hosts: dbservers
  remote_user: root
  gather_facts: no
  tasks:
  - debug:
      msg: "{{item}}"
    with_together:
    - [ 1, 2, 3 ]
    - [ a, b, c ]

在这里插入图片描述

  • with_together可以将两个列表中的元素”对齐合并”:
    • 第一个小列表中的第1个值会与第二个小列表中的第1个值合并在一起输出,
    • 第一个小列表中的第2个值会与第二个小列表中的第2个值合并在一起输出,
    • 第一个小列表中的第3个值会与第二个小列表中的第3个值合并在一起输出,如果第二个小列表中的第3个元素没有值则输出null

with_cartesian

- hosts: dbservers
  remote_user: root
  gather_facts: no
  tasks:
  - debug:
      msg: "{{item}}"
    with_cartesian:
    - [ a, b, c ]
    - [ test1, test2 ]
  • with_cartesian的作用就是将每个小列表中的元素按照两两组合循环的处理每个组合,比如第一个小列表中的每个元素与第二个小列表中的每个元素都两两组合在了一起。
    在这里插入图片描述

with_nested

- hosts: dbservers
  remote_user: root
  gather_facts: no
  vars:
    test:
      - a
      - b
      - c  
    demo:
      - test1 
      - test2
  tasks:
  - debug:
      msg: "{{ item[0] }},{{ item[1] }}" 
    with_nested:             
    - "{{test}}"
    - "{{demo}}"

在这里插入图片描述

Templates模块

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


就是复制httpd配置文件,进行修改,创建模板文件

Listen {{ip_port}}
ServerName {{host_name}}
DocumentRoot "{{root_dir}}"
vim demo6.yaml
- name: sixth play
  hosts: all
  remote_user: root
  vars:
  - pkg: httpd

  tasks:
  - name: install apache
    yum: name=httpd state=latest
  - name: create root dir
    file: state=directory path={{item}}
    with_items:
    - /var/www/html/accp
    - /var/www/html/benet
  - name: create index.html in www.accp.com
    copy: content="<h1>this is accp web</h1>" dest=/var/www/html/accp/index.html
    when: ansible_default_ipv4.address == "192.168.242.67"
  - name: create index.html in www.benet.com
    copy: content="<h1>this is benet web</h1>" dest=/var/www/html/benet/index.html
    when: inventory_hostname == "192.168.242.68"
  - name: prepare configuration file
    template: src=/etc/ansible/playbook/httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf
    notify: "reload apache"
  - name: start apache
    service: name={{pkg}} state=started enabled=yes

  handlers:
  - name: reload apache
    service: name={{pkg}} state=reloaded  

在这里插入图片描述
在这里插入图片描述

tags模块

  • 可以在一个playbook中为某个或某些任务定义“标签”,在执行此playbook时通过ansible-playbook命令使用–tags选项能实现仅运行指定的tasks。
  • playbook还提供了一个特殊的tags为always。作用就是当使用always作为tags的task时,无论执行哪一个tags时,定义有always的tags都会执行。
- name: seventh play
  hosts: dbservers
  remote_user: root
  tasks:
  - name: create abc.txt
    file: path=/opt/abc.txt  state=touch
    tags:
    - zhangsan
    - lisi

  - name: create 123.txt
    file: path=/opt/123.txt  state=touch
    tags:
    - always

  - name: create 456.txt
    copy: content="123456789"  dest=/opt/456.txt
    tags:
    - zhangsan

在这里插入图片描述

##运行

ansible-playbook demo7.yaml --tags="zhangsan"

在这里插入图片描述

ansible-playbook demo7.yaml --tags="lisi"

在这里插入图片描述
在这里插入图片描述

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

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

操作系统启动相关概念(BIOS、MBR、GPT、BRUB)

不管是 Windows 还是 Linux 操作系统&#xff0c;底层设备一般均为物理硬件&#xff0c;操作系统启动之前会对硬件进行检测&#xff0c;然后硬盘引导启动操作系统&#xff0c;如下为操作系统启动相关的各个概念。 一、BIOS 基本输入输出系统&#xff08;Basic Input Output Sy…

【Java】Spring——创建Spring + 对Spring的存储 /读取对象操作

文章目录 前言一、创建Spring项目二、向Spring容器中存储 Bean 对象三、从Spring容器中读取 Bean 对象得到Spring上下文对象得到 Bean 对象 总结 前言 本人是一个普通程序猿!分享一点自己的见解,如果有错误的地方欢迎各位大佬莅临指导,如果你也对编程感兴趣的话&#xff0c;互…

决策树学习

决策树学习 决策树决策树基础适用决策树学习的经典目标问题样本的表示训练样本决策树的概念发展历史 经典决策树算法ID3算法属性选择和节点混杂度&#xff08;Impurity&#xff09;ID3 Q1: 哪个属性是最佳属性&#xff1f;当前最佳属性节点选择熵&#xff08;Entropy&#xff0…

JSP 中的隐式对象预定义变量详解

JSP隐式对象是JSP容器为每个页面提供的Java对象&#xff0c;开发者可以直接使用它们而不用显式声明。JSP隐式对象也被称为预定义变量。 JSP所支持的九大隐式对象&#xff1a; request对象 request对象是javax.servlet.http.HttpServletRequest 类的示例。每当客户端请求一个J…

揭秘UniApp跨平台魔力:6道面试题带你探索!

文章目录 1. UniApp是什么&#xff1f;它有什么特点和优势&#xff1f;2. 介绍一下UniApp的跨平台原理。1. 基于WebView的渲染2. 统一封装API3. 平台特性适配4. 虚拟DOM Diff算法 3. 如何在UniApp中实现页面路由跳转&#xff1f;4. 如何在UniApp中发送网络请求&#xff1f;5. 详…

ELK 使用kibana查询和分析nginx日志

背景&#xff1a;使用kibana查询和分析nginx请求日志&#xff0c;方便开发人员查询系统日志和分析系统问题。 setp 1、定义Index patterns 2、定义Discover(Search 查询数据) 3、定义Visualizations 3.1 定义Vertical Bar 3.2 、Choose a source 3.3、定义图表 4、定义…

WPS本地镜像化在线文档操作以及样例

一个客户项目有引进在线文档操作需求&#xff0c;让我这边做一个demo调研下&#xff0c;给我的对接文档里有相关方法的说明&#xff0c;照着对接即可。但在真正对接过程中还是踩过不少坑&#xff0c;这儿对之前的对接工作做个记录。 按照习惯先来一个效果&#xff1a; Demo下载…

K8S系统监控:使用Metrics Server和Prometheus

Kubernetes 也提供了类似的linux top的命令&#xff0c;就是 kubectl top&#xff0c;不过默认情况下这个命令不会生效&#xff0c;必须要安装一个插件 Metrics Server 才可以。 Metrics Server 是一个专门用来收集 Kubernetes 核心资源指标&#xff08;metrics&#xff09;的…

qt服务器 网络聊天室

widget.cpp #include "widget.h" #include "ui_widget.h"Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget) {ui->setupUi(this);//给服务器指针实例化空间server new QTcpServer(this); }Widget::~Widget() {delete ui; }//启动…

使用Gradio构建生成式AI应用程序; Stability AI推出Stable Diffusion XL 1.0

&#x1f989; AI新闻 &#x1f680; Stability AI推出最先进的AI工具Stable Diffusion XL 1.0 摘要&#xff1a;Stability AI宣布推出Stable Diffusion XL 1.0&#xff0c;该版本是其迄今为止最先进的AI工具。Stable Diffusion XL 1.0提供更鲜艳、更准确的图片生成&#xff…

【AI之路】使用huggingface_hub优雅解决huggingface大模型下载问题

文章目录 前言一、Hugging face是什么&#xff1f;二、准备工作三、下载整个仓库或单个大模型文件1. 下载整个仓库2. 下载单个大模型文件 总结附录 前言 Hugging face 资源很不错&#xff0c;可是国内下载速度很慢&#xff0c;动则GB的大模型&#xff0c;下载很容易超时&#…

【QT 网络云盘客户端】——主窗口界面的设计

目录 1.设计主窗口界面 2.设置窗口的背景图片 3. 自定义标题栏 3.1 设置toolbutton按钮的图片 3.2 设置按钮的大小 3.3 将自定义标题栏添加设置到主页面中 3.4 去除窗口的原标题栏 3.5 设置按钮颜色 3.6 切换页面功能实现 4.我的文件页面的设计 4.1 菜单栏的设计 4…

分冶算法 剑指 07 重建二叉树 排序算法:剑指45 把数组排成最小的数 10-I 斐波那契数列

来记录几个注意事项 1.vector容器里利用find&#xff08;&#xff09;函数 不同于map&#xff08;map有find方法&#xff09;&#xff0c;vector本身是没有find这一方法&#xff0c;其find是依靠algorithm来实现的。 所以要包含头文件 #include <iostream> #include <…

物联网阀控水表计量准确度如何?

物联网阀控水表是一种新型的智能水表&#xff0c;它采用了先进的物联网技术&#xff0c;可以通过远程控制和监测水表的运行情况&#xff0c;实现更加精准的水量计量和费用结算。那么&#xff0c;物联网阀控水表的计量准确度如何呢&#xff1f;下面我们将从以下几个方面进行详细…

sql中with as用法/with-as 性能调优/with用法

文章目录 一、概述二、基本语法三、使用场景3.1、定义CTE,并为每列重命名3.2、多次引用/多次定义3.3、with与union all联合使用3.4、with返回多种结果的值3.5、with与insert使用 四、递归查询4.1、语法4.2、使用场景4.2.1、用with递归构造1-10的数据4.2.2、with与insert递归造数…

flink to starrocks 问题集锦....

[问题排查]导入失败相关 - 问题排查 - StarRocks中文社区论坛 starrocks官网如下&#xff1a; Search StarRocks Docs starrocks内存配置项&#xff1a; 管理内存 Memory_management StarRocks Docs 问题1&#xff1a;实时写入starrocks &#xff0c;配置参数设置如下&a…

微信小程序,商城底部工具栏的实现

效果演示&#xff1a; 前提条件&#xff1a; 去阿里云矢量图标&#xff0c;下载8个图标&#xff0c;四个黑&#xff0c;四个红&#xff0c;如图&#xff1a; 新建文件夹icons&#xff0c;把图标放到该文件夹&#xff0c;然后把该文件夹移动到该项目的文件夹里面。如图所示 app…

第一次后端复习整理(JVM、Redis、反射)

1. JVM 文章仅为自身笔记 详情查看一篇文章掌握整个JVM&#xff0c;JVM超详细解析&#xff01;&#xff01;&#xff01; 1.1 什么是JVM jvm是Java虚拟机 1.2 Java文件的编译过程 程序员编写代码形成.java文件经过javac编译成.class文件再通过JVM的类加载器进入运行时数据…

论文分享:PowerTCP: Pushing the Performance Limits of Datacenter Networks

1 原论文的题目&#xff08;中英文&#xff09;、题目中包含了哪些关键词&#xff1f;这些关键词的相关知识分别是什么&#xff1f; 题目&#xff1a;PowerTCP: Pushing the Performance Limits of Datacenter Networks PowerTCP&#xff1a;逼近数据中心的网络性能极限 2 论…

app稳定性测试-iOS篇

稳定性测试&#xff1a;测试应用程序在长时间运行过程中是否存在内存泄漏、崩溃等问题&#xff0c;以确保应用程序具有较高的稳定性和可靠性。 对于安卓端&#xff0c;官方提供了很好的稳定性测试工具&#xff1a;monkey。 相比较而言&#xff0c;iOS则没有&#xff0c;而且当前…