news 2026/6/23 9:51:48

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/6/22 2:28:30

如何用Locale Emulator实现完美区域语言模拟:新手终极指南

如何用Locale Emulator实现完美区域语言模拟:新手终极指南 【免费下载链接】Locale-Emulator Yet Another System Region and Language Simulator 项目地址: https://gitcode.com/gh_mirrors/lo/Locale-Emulator Locale Emulator作为一款强大的系统区域和语言…

作者头像 李华
网站建设 2026/6/23 3:00:31

Java与操作系统常用命令交互全解析

Java与操作系统常用命令交互全解析第一章:Java执行操作系统命令的核心API1.1 Runtime.exec() 方法详解Runtime.exec() 是Java执行外部命令最直接的方式,有6个重载版本:java// 最常用的四种形式 public Process exec(String command) public P…

作者头像 李华
网站建设 2026/6/17 18:07:52

Mac微信防撤回插件WeChatIntercept:终极完整使用指南

Mac微信防撤回插件WeChatIntercept:终极完整使用指南 【免费下载链接】WeChatIntercept 微信防撤回插件,一键安装,仅MAC可用,支持v3.7.0微信 项目地址: https://gitcode.com/gh_mirrors/we/WeChatIntercept 在商务沟通和团…

作者头像 李华
网站建设 2026/6/23 16:09:58

LobeChat能否实现AI律师函撰写?法律文书自动化产出

LobeChat能否实现AI律师函撰写?法律文书自动化产出 在律所的某个深夜,一位年轻律师正对着屏幕反复修改第三版催款律师函——当事人信息、合同条款、违约时间线、法律依据……每一处细节都必须精准无误。这样的场景,在法律行业中每天都在上演。…

作者头像 李华
网站建设 2026/6/18 17:27:28

基于Python的在线零食购物商城系统的设计与实现

基于Python的在线零食购物商城系统的设计与实现一.系统概述本项目设计并实现了一个基于Python、Django框架和MySQL数据库的在线零食购物商城系统。系统包括多个模块,以提供完整的电商功能。系统功能覆盖了用户管理、商品管理、购物车管理、订单管理、支付模块、管理…

作者头像 李华
网站建设 2026/6/22 23:51:17

小爱音箱AI升级终极指南:三步打造你的智能语音管家

小爱音箱AI升级终极指南:三步打造你的智能语音管家 【免费下载链接】mi-gpt 🏠 将小爱音箱接入 ChatGPT 和豆包,改造成你的专属语音助手。 项目地址: https://gitcode.com/GitHub_Trending/mi/mi-gpt 还在为小爱音箱千篇一律的回答感到…

作者头像 李华