news 2026/4/18 8:52:14

03-编写和运行Playbook

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
03-编写和运行Playbook

文章目录

  • 03-编写和运行Playbook
    • 实验环境
    • Playbook 介绍
    • Vim 编辑器设置
    • Playbook 编写
      • Playbook 示例
      • YAML 注释
      • YAML 单行字符串
      • YAML 多行字符串
      • YAML 字典
      • YAML 列表
    • Playbook 运行
      • 运行
      • 语法检查
      • 空运行
    • Playbook 提权

03-编写和运行Playbook

实验环境

[dyx@controller ~]$mkdirweb[dyx@controller ~]$cdweb/[dyx@controller web]$vimansible.cfg[dyx@controller web]$catansible.cfg[defaults]remote_user=dyxinventory=./inventory[privilege_escalation]become=truebecome_user=rootbecome_method=sudobecome_ask_pass=false[dyx@controller web]$viminventory[dyx@controller web]$catinventory node[1:4]

Playbook 介绍

ad hoc 命令可以作为一次性命令对一组主机运行一项简单的任务。不过,若要真正发挥Ansible的能力,需要使用功能 playbook。

playbook 是一个文本文件,其中包含由一个或多个按特定顺序运行的play组成的列表。play是针对清单中选定的主机运行的一组有序任务。play可以让您将一系列冗长而复杂的手动管理任务转变为可轻松重复的例程,并且具有可预测性。

在 playbook 中,您可以将play内的任务序列保存为人类可读并可立即运行的形式。根据任务的编写方式,任务本身记录了部署应用或基础架构所需的步骤。

ad hoc 命令示例:

[dyx@controller web]$ ansible -m user -a"name=newbie uid=4000 state=present"node1 node1|CHANGED=>{"ansible_facts":{"discovered_interpreter_python":"/usr/bin/python"},"changed":true,"comment":"","create_home":true,"group":4000,"home":"/home/newbie","name":"newbie","shell":"/bin/bash","state":"present","system":false,"uid":4000}

Playbooks以yaml格式编写,通常以 yaml 和 yml 扩展名保存。

改写成playbook:

[dyx@controller web]$vimnewuser.yaml
[dyx@controller web]$ cat newuser.yaml----name:configure important user consistentlyhosts:node1tasks:-name:newbie exists with uid 4000user:name:newbieuid:4000state:present...

测试:

[dyx@controller web]$ ansible-playbook newuser.yaml PLAY[configure important user consistently]******************************************** TASK[Gathering Facts]****************************************************************** ok:[node1]TASK[newbie exists with uid4000]****************************************************** ok:[node1]PLAY RECAP ****************************************************************************** node1:ok=2changed=0unreachable=0failed=0skipped=0rescued=0ignored=0

yaml格式只使用空格缩进,对于空格的数量没有强制要求。

基本规则:

  • 同一级别的元素,使用相同的缩进。

  • 对于子项目,使用比父项目更多的缩进。

  • 增加空白行,提高可读性。

  • 键值之间用:隔开,:后面加个空格

  • 第一行必须---

  • 最后一行...可以省略

Vim 编辑器设置

如果使用vim编辑器,设置vim环境便于编辑Playbooks。

在$HOME/.vimrc文件中添加以下内容:

[dyx@controller web]$vim~/.vimrc[dyx@controller web]$cat~/.vimrcsetaits=2

效果:

  • “ai”,即 “autoindex”,表示自动缩进。

  • “ts”,即 “tabstop”,表示tab键使用2个空格代替。

  • autocmd FileType yam,代表文件类型是yaml时,自动执行“set ai ts=2”。

Playbook 编写

Playbook 示例

playbook.yaml 内容如下:

[dyx@controller web]$ vim playbook.yaml[dyx@controller web]$ cat playbook.yaml# yaml格式起始行,一般不省略---# Playbook中第一个play# play具有属性:name,hosts,become,tasks,缩进一致# name属性,用于简要描述play-name:Enable intranet services# hosts属性,用于定义要在哪个受管理节点执行hosts:node2# tasks属性,用于描述play中任务,属性是列表格式tasks:# 第一个任务# 任务具有属性:涵name和模块名等。# name属性,用于简要描述任务-name:latest version of httpd and firewalld isntalledyum:name:-httpd-firewalldstate:latest-name:test html page is installedcopy:content:"welcome dyx website!\n"dest:/var/www/html/index.html-name:firewalld enabled and runningservice:name:firewalldenabled:yesstate:started-name:firewalld permits access to httpd servicefirewalld:service:httppermanent:yesstate:enabledimmediate:yes-name:httpd enabled and runningservice:name:httpdenabled:truestate:started
[dyx@controller web]$ ansible-playbook playbook.yaml PLAY[Enable intranet services]********************************************************* TASK[Gathering Facts]****************************************************************** ok:[node2]TASK[latest version of httpd and firewalld isntalled]********************************** changed:[node2]TASK[test html page is installed]****************************************************** changed:[node2]TASK[firewalld enabled and running]**************************************************** ok:[node2]TASK[firewalld permits access to httpd service]**************************************** changed:[node2]TASK[httpd enabled and running]******************************************************** changed:[node2]PLAY RECAP ****************************************************************************** node2:ok=6changed=2unreachable=0failed=0skipped=0rescued=0ignored=0

[dyx@controller web]$vimplaybook.yaml[dyx@controller web]$catplaybook.yaml --- - name: Enable intranet services hosts: node2 tasks: - name: latest version of httpd and firewalld isntalled yum: name: - httpd - firewalld state: latest - name:testhtml page is installed copy: content:"welcome dyx website!\n"dest: /var/www/html/index.html - name: firewalld enabled and running service: name: firewalld enabled:yesstate: started - name: firewalld permits access to httpdservicefirewalld: service: http permanent:yesstate: enabled immediate:yes- name: httpd enabled and running service: name: httpd enabled:truestate: started - name:testintranet web server hosts: localhost become: no tasks: - name: connect to inteanet web server uri: url: http://node2 return_content:yesstatus_code:200...

验证:

[dyx@controller web]$ ansible-playbook playbook.yaml PLAY[Enable intranet services]********************************************************* TASK[Gathering Facts]****************************************************************** ok:[node2]TASK[latest version of httpd and firewalld isntalled]********************************** ok:[node2]TASK[test html page is installed]****************************************************** ok:[node2]TASK[firewalld enabled and running]**************************************************** ok:[node2]TASK[firewalld permits access to httpd service]**************************************** ok:[node2]TASK[httpd enabled and running]******************************************************** ok:[node2]PLAY[test intranet web server]********************************************************* TASK[Gathering Facts]****************************************************************** ok:[localhost]TASK[connect to inteanet web server]*************************************************** ok:[localhost]PLAY RECAP ****************************************************************************** localhost:ok=2changed=0unreachable=0failed=0skipped=0rescued=0ignored=0node2:ok=6changed=0unreachable=0failed=0skipped=0rescued=0ignored=0

YAML 注释

在 YAML中, 编号或井号符号(#)右侧的所有内容都是注释。如果注释的左侧有内容, 请在该编号符号的前面加一个空格。注释可用于提高可读性。

示例:

# This is YAML commentSome data# This is also a YAML comment

YAML 单行字符串

YAML中的字符串通常不需要放在引号里,即使字符串中包含空格。

字符串也可以用双引号或单引号括起。

this is a string'this is another string'"this is yet another a string"

YAML 多行字符串

  • 可以使用竖线(I)字符表示,保留字符串中的换行字符。

    示例:

    [dyx@controller web]$vimplaybook.yaml
    [dyx@controller web]$ cat playbook.yaml----name:test stringhosts:node1tasks:-name:test stringdebug:msg:|example compang 123 main stress aldivh dsi 8
    [dyx@controller web]$ ansible-playbook playbook.yaml PLAY[test string]********************************************************************** TASK[Gathering Facts]****************************************************************** ok:[node1]TASK[test string]********************************************************************** ok:[node1]=>{"msg":"example compang\n123 main stress\naldivh dsi 8\n"}PLAY RECAP ****************************************************************************** node1:ok=2changed=0unreachable=0failed=0skipped=0rescued=0ignored=0
  • 也可以使用大于号(>)字符表示换行字符。执行时换行符使用空格代替,并且行内的引导空白将被删除。

    示例:

    [dyx@controller web]$ vim playbook.yaml[dyx@controller web]$ cat playbook.yaml----name:test stringhosts:node1tasks:-name:test stringdebug:msg:>example compang 123 main stress aldivh dsi 8
    [dyx@controller web]$ ansible-playbook playbook.yaml PLAY[test string]********************************************************************** TASK[Gathering Facts]****************************************************************** ok:[node1]TASK[test string]********************************************************************** ok:[node1]=>{"msg":"example compang 123 main stress aldivh dsi 8\n"}PLAY RECAP ****************************************************************************** node1:ok=2changed=0unreachable=0failed=0skipped=0rescued=0ignored=0

    这种方法通常用于将很长的字符串在空格字符处断行,使它们跨占多行来提高可读性。

  • 不加

    [dyx@controller web]$ vim playbook.yaml[dyx@controller web]$ cat playbook.yaml----name:test stringhosts:node1tasks:-name:test stringdebug:msg:example compang 123 main stress aldivh dsi 8
    [dyx@controller web]$ ansible-playbook playbook.yaml PLAY[test string]********************************************************************** TASK[Gathering Facts]****************************************************************** ok:[node1]TASK[test string]********************************************************************** ok:[node1]=>{"msg":"example compang 123 main stress aldivh dsi 8"}PLAY RECAP ****************************************************************************** node1:ok=2changed=0unreachable=0failed=0skipped=0rescued=0ignored=0

YAML 字典

一组键值对的集合,又称为映射(mapping)和哈希(hashes)。

以缩进块的形式编写键值对集合,如下方所示:user属性是字典格式,是多个键值对集合。

user:name:laogaouid:1088state:absent

字典也可以使用以花括号括起的内联块格式编写,如下方所示:

user:{name:laogao,uid:1088,state:absent}

大多数情形中应避免内联块格式,其可读性较差。不过,当playbook中包含角色列表时,使用这种语法,更加容易区分play中包含的角色和传递给角色的变量。

某些 playbook 可能使用较旧的简写(shorthand)格式,通过将模块的键值对放在与模块名称相同的行上来定义任务。

示例:

-name:shorhand formuser:name=laogao uid=1088 state=absent

普通格式:

-name:shorhand formuser:name:laogaouid:1088state:absent

两者格式总结:

  • 通常您应避免简写格式,而使用普通格式。

  • 普通格式的行数较多,更容易操作。任务的关键字垂直堆叠,更容易区分。 阅读play时,您的眼睛直接向下扫视,左右运动较少。

  • 普通格式是原生的YAML,现代文本编辑器中的语法突出显示工具可以识别,简写形式则不支持。

  • 可能会在文档和他人提供的旧playbook中看到这种语法,而且这种语法仍然可以发挥作用。

YAML 列表

一组按次序排列的值,又称为序列(sequence)和数组(array)。

以缩进块的形式编写的键值对集合,如下方所示:

-name:latest version of httpd and firewalld installedyum:name:-httpd-firewalldstate:latest-name:test html page is installedcopy:content:"Welcome to the example.com intranet!\n"dest:/var/www/html/index.html

以上有两个任务,每个任务都是多个键值对描述。其中yum模块操作的软件包是一个简单的名称列表。内联格式:

name:[httpd, firewalld]

尽量避免内联格式。

Playbook 运行

运行

[dyx@controller web]$vimplaybook.yaml
---# Playbook中第一个play# play具有属性:name,hosts,become,tasks,缩进一致# name属性,用于简要描述play-name:Enable intranet services# hosts属性,用于定义要在哪个受管理节点执行hosts:node2# tasks属性,用于描述play中任务,属性是列表格式tasks:# 第一个任务# 任务具有属性:涵name和模块名等。# name属性,用于简要描述任务-name:latest version of httpd and firewalld isntalledyum:name:-httpd-firewalldstate:latest-name:test html page is installedcopy:content:"welcome dyx website!\n"dest:/var/www/html/index.html-name:firewalld enabled and runningservice:name:firewalldenabled:yesstate:started-name:firewalld permits access to httpd servicefirewalld:service:httppermanent:yesstate:enabledimmediate:yes-name:httpd enabled and runningservice:name:httpdenabled:truestate:started

第一次执行

[dyx@controller web]$ ansible-playbook playbook.yaml PLAY[Enable intranet services]********************************************************* TASK[Gathering Facts]****************************************************************** ok:[node1]TASK[latest version of httpd and firewalld isntalled]********************************** changed:[node1]TASK[test html page is installed]****************************************************** changed:[node1]TASK[firewalld enabled and running]**************************************************** ok:[node1]TASK[firewalld permits access to httpd service]**************************************** changed:[node1]TASK[httpd enabled and running]******************************************************** changed:[node1]PLAY[test intranet web server]********************************************************* TASK[Gathering Facts]****************************************************************** ok:[localhost]TASK[connect to inteanet web server]*************************************************** ok:[localhost]PLAY RECAP ****************************************************************************** localhost:ok=2changed=0unreachable=0failed=0skipped=0rescued=0ignored=0node1:ok=6changed=4unreachable=0failed=0skipped=0rescued=0ignored=0

第二次执行剧本,任务状态全是绿色。

[dyx@controller web]$ ansible-playbook playbook.yaml PLAY[Enable intranet services]********************************************************* TASK[Gathering Facts]****************************************************************** ok:[node1]TASK[latest version of httpd and firewalld isntalled]********************************** ok:[node1]TASK[test html page is installed]****************************************************** ok:[node1]TASK[firewalld enabled and running]**************************************************** ok:[node1]TASK[firewalld permits access to httpd service]**************************************** ok:[node1]TASK[httpd enabled and running]******************************************************** ok:[node1]PLAY[test intranet web server]********************************************************* TASK[Gathering Facts]****************************************************************** ok:[localhost]TASK[connect to inteanet web server]*************************************************** ok:[localhost]PLAY RECAP ****************************************************************************** localhost:ok=2changed=0unreachable=0failed=0skipped=0node1:ok=6changed=0unreachable=0failed=0skipped=0

语法检查

选项–syntax-check,只检查剧本语法,不执行剧本。

[dyx@controller web]$ ansible-playbook playbook.yaml --syntax-check playbook: playbook.yaml

空运行

空运行,是指模拟运行,并不是真正执行。

[dyx@controller web]$ ansible-playbook playbook.yaml -C PLAY[Enable intranet services]********************************************************* TASK[Gathering Facts]****************************************************************** ok:[node1]TASK[latest version of httpd and firewalld isntalled]********************************** ok:[node1]TASK[test html page is installed]****************************************************** ok:[node1]TASK[firewalld enabled and running]**************************************************** ok:[node1]TASK[firewalld permits access to httpd service]**************************************** ok:[node1]TASK[httpd enabled and running]******************************************************** ok:[node1]PLAY[test intranet web server]********************************************************* TASK[Gathering Facts]****************************************************************** ok:[localhost]TASK[connect to inteanet web server]*************************************************** skipping:[localhost]PLAY RECAP ****************************************************************************** localhost:ok=1changed=0unreachable=0failed=0skipped=1rescued=0ignored=0node1:ok=6changed=0unreachable=0failed=0skipped=0rescued=0ignored=0

提高输出详细程度

  • -v,显示任务结果。一般情况使用-v即可。

  • -vv,任务结果和任务配置都会显示。

  • -vvv,包含关于与受管主机连接的信息。

  • -vvvv,增加了连接插件相关的额外详细程度选项,包括受管主机上用于执行脚本的用户,以及所执行的脚本。

Playbook 提权

在playbook中指定此关键字将覆盖/etc/ansible/ansible.cfg文件中的设置特权升级属性

  • remote_user,指定ssh用户

  • become,启用或禁用特权升级

  • become_method,启用特权升级的方法

  • become_user,特殊升级的帐户

实验环境:

注释ansbile.cfg的相关配置

[dyx@controller web]$vimansible.cfg[dyx@controller web]$catansible.cfg[defaults]#remote_user=dyxinventory=./inventory[privilege_escalation]#become=true#become_user=root#become_method=sudo#become_ask_pass=false

安装httpd

[dyx@controller web]$vimplaybook.yaml
----name:enable intranet servicehosts:node1tasks:-name:latest version of httpd and firewalld installdyum:name:-httpd-firewalldstate:latest

验证:提示需要root权限

[dyx@controller web]$ ansible-playbook playbook.yaml PLAY[enable intranet service]********************************************************** TASK[Gathering Facts]****************************************************************** ok:[node1]TASK[latest version of httpd and firewalld installd]*********************************** fatal:[node1]: FAILED!=>{"changed":true,"changes":{"installed":["httpd"],"updated":[]},"msg":"You need to be root to perform this command.\n","rc":1,"results":["All packages providing firewalld are up to date","Loaded plugins: fastestmirror, langpacks\n"]}PLAY RECAP ****************************************************************************** node1:ok=1changed=0unreachable=0failed=1skipped=0rescued=0ignored=0[dyx@controller web]$

[dyx@controller web]$vimplaybook.yaml
----name:enable intranet servicehosts:node1remote_user:dyxbecome:truebecome_method:sudobecome_user:roottasks:-name:latest version of httpd and firewalld installdyum:name:-httpd-firewalldstate:latest

验证:

[dyx@controller web]$ ansible-playbook playbook.yaml PLAY[enable intranet service]********************************************************** TASK[Gathering Facts]****************************************************************** ok:[node1]TASK[latest version of httpd and firewalld installd]*********************************** changed:[node1]PLAY RECAP ****************************************************************************** node1:ok=2changed=1unreachable=0failed=0skipped=0rescued=0ignored=0

思考:

编辑playbook.yaml

node1,node2创建用户xiyangyang(uid2001),meiyangyang(uid2002)

node3,node4创建用户tangsan(uid2003),xiaowu(uid2004)

----name:create userhosts:node1,node2tasks:-name:create xiyangyanguser:name:xiyangyanguid:2001-name:create meiyangyanguser:name:meiyangyanguid:2002-name:create userhosts:node3,node4tasks:-name:create tangsanuser:name:tangsanuid:2003-name:create xiaowuuser:name:xiaowuuid:2004
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/18 3:30:13

【开题答辩全过程】以 基于Java的医院信息管理系统的设计与实现为例,包含答辩的问题和答案

个人简介 一名14年经验的资深毕设内行人,语言擅长Java、php、微信小程序、Python、Golang、安卓Android等 开发项目包括大数据、深度学习、网站、小程序、安卓、算法。平常会做一些项目定制化开发、代码讲解、答辩教学、文档编写、也懂一些降重方面的技巧。 感谢大家…

作者头像 李华
网站建设 2026/4/18 5:23:23

【机器学习】3.GBDT(梯度提升决策树)

GBDT(梯度提升决策树)系统梳理 一、GBDT核心概述 1. 定义 GBDT(Gradient Boosting Decision Tree)即梯度提升决策树,属于Boosting集成学习框架,由Friedman在2001年提出。核心逻辑是串行训练多棵决策树&…

作者头像 李华
网站建设 2026/4/18 5:24:02

社群运营手册:建立活跃的LobeChat用户群

社群运营手册:建立活跃的LobeChat用户群 在AI助手逐渐成为数字生活标配的今天,一个功能强大又易于使用的交互界面,往往决定了用户是否愿意留下来。对于技术社群而言,面对成员频繁提问、知识分散、运维人力有限等现实挑战&#xff…

作者头像 李华
网站建设 2026/4/17 22:35:55

NVIDIA隐藏设置实战:性能调优的终极指南

NVIDIA隐藏设置实战:性能调优的终极指南 【免费下载链接】nvidiaProfileInspector 项目地址: https://gitcode.com/gh_mirrors/nv/nvidiaProfileInspector NVIDIA Profile Inspector作为一款深度挖掘显卡潜力的工具,能够解锁官方控制面板中无法访…

作者头像 李华
网站建设 2026/4/18 5:25:47

终极Mac软件管理神器:Applite让Homebrew图形化操作变得如此简单

还在为复杂的终端命令而头疼吗?每次安装软件都要打开终端,输入一长串记不住的命令,安装完成还要手动清理,这样的体验实在让人烦恼。Applite的出现彻底改变了这一切,这款基于Homebrew Casks的开源工具通过直观的图形界面…

作者头像 李华
网站建设 2026/4/18 2:44:52

系统编程中线程

一、线程的定义与作用定义:线程是轻量级的进程,线程属于某个进程。作用:并发,和进程相同。相对比较耗时的任务。特征:1.进程是系统中最小的资源分配单位。2.线程是系统最小的执行单位。3.进程中,线程与线程…

作者头像 李华