3.2 实现多个PLAYS

3.2.1 目标

学完这一章节,学生能去:

  • 编写一个剧本使用多个plays并且每个play进行权限提升
  • 高效的使用ansible-doc去学习怎么样去使用新的模块去实现play中的任务

3.2.2 写多个plays

一个playbook是一个YAML文件包含了一个或多个plays的集合,记住,单个play是针对清单文件中选中的主机执行的有序任务列表。因此,如果一个playbook包含了多个plays,每个play应用它的任务针对一个单独的主机集合。

这是非常有用的当策划一个复杂的部署可能涉及不同的任务在不同的主机上,你可以写一个playbook执行一个play针对一部分主机,当执行完成后,另一部分主机执行另外一个play。

写一个playbook包含多个plays是非常简单的,plabook中的每个play都被写为playbook中的顶级列表项,每个play都是包含常用play关键字的列表项。

---
# This is a simple playbook with two plays
- name: first playhosts: web.example.comtasks:- name: first taskyum:name: httpdstatus: present- name: second taskservice:name: httpdenabled: true- name: second playhosts: database.example.comtasks:- name: first taskservice:name: mariadbenabled: true

3.2.3 远程用户和权限提升在PLAYS中

Plays能使用不同的远程用户或权限提升设置,而不是在默认设置中指定。这些在play中设置为与hosts或tasks相同的级别,plays中的设置优先级高于ansible.cfg。如下例:

- name: /etc/hosts is up to datehosts: datacenter-westremote_user: automationbecome: yestasks:- name: server.example.com in /etc/hostslineinfile:path: /etc/hostsline: '192.168.2.42 server.example.com server'state: present

3.2.4 查找模块为TASKS

ansible附带的大量模块为管理员提供了许多执行常见管理任务的工具。作为一个回顾,执行ansible-doc -l命令去查看modules清单。

[sysadmin@ansible security]$ ansible-doc -l
fortios_router_community_list                                 Configure community lists in Fortinet's FortiOS and FortiGate
azure_rm_devtestlab_info                                      Get Azure DevTest Lab facts
ecs_taskdefinition                                            register a task definition in ecs
avi_alertscriptconfig                                         Module for setup of AlertScriptConfig Avi RESTful Object
tower_receive                                                 Receive assets from Ansible Tower
...output omitted...

使用ansible-doc [module name]命令展示详细的模块说明

[sysadmin@ansible security]$ ansible-doc yum
> YUM    (/usr/lib/python2.7/site-packages/ansible/modules/packaging/os/yum.py)Installs, upgrade, downgrades, removes, and lists packages and groups with the `yum' package manager. This module only works on Python 2. If you require Python 3support see the [dnf] module.* This module is maintained by The Ansible Core Team* note: This module has a corresponding action plugin.OPTIONS (= is mandatory):- allow_downgrade
...output omitted...

ansible-doc命令也提供了-s选项,生成的示例输出可以作为如何在play中使用特定模块的模型。这个输出可以作为一个初始模板,它可以包含在play中,实现用于任务执行的模块。注释被包含在输出去,提醒管理员每个选项的用法。如下例

[sysadmin@ansible security]$ ansible-doc -s yum
- name: Manages packages with the `yum' package manageryum:allow_downgrade:       # Specify if the named package and version is allowed to downgrade a maybe already installed higher version of that package. Note that setting allow_downgrade=True can make thismodule behave in a non-idempotent way. The task could end up with a set of packages that does not match the complete list of specified packages toinstall (because dependencies between the downgraded package and others can cause changes to the packages which were in the earlier transaction).autoremove:            # If `yes', removes all "leaf" packages from the system that were originally installed as dependencies of user-installed packages but which are no longer required by any suchpackage. Should be used alone or when state is `absent' NOTE: This feature requires yum >= 3.4.3 (RHEL/CentOS 7+)

3.2.5 Module维护

Ansible发布了大量的模块,能被用于许多的任务。上游的社区是非常的活跃,这些模块处于不同的开发阶段。模块的ansible-doc被希望指定谁负责上游的维护及当前的开发状态是。这被显示在METADATA区在ansible-doc的末尾。
status记录了模块的开发状态:

  • stableinterface: 模块的关键字是稳定的,我们将尽一切努力不删除关键字或改变它们的含义
  • preview:
    此处有很多参数,非重点,不做详细介绍。

尽可能避免使用command,shell,和raw模块在playbooks中。尽管它们看起来非常简单去使用。因为它们可以携带任意的命令,使用这些模块非常容易写出非幂等性的playbook。如下例中的任务,shell模块是没有幂等性的。play每执行一次,它重新写/etc/resolv.conf,即使它已经包含了nameserver 192.0.2.1这行。

- name: Non-idempotent approach with shell moduleshell: echo "nameserver 192.0.2.1" > /etc/resolv.conf

以幂等方式使用shell模块编写任务有几种方法,有时执行这些更改并使用shell是最好的方法。一个快速的解决方案是去用copy模块去替代。

- name: idempotent approach with copy modulecopy: dest: /etc/resolv.confcontent: "nameserver  192.0.2.1\n"

copy模块测试如果那状态已经满足要求,它不做改变。shell模块提供了更多的灵活性,但也需要更多的关注使得它执行在一个幂等的模式下。

3.2.6 PLAYBOOK语法的变化

本章的最后一部分研究一些变化你可能遭遇的关于YAML或playbooks。
YAML 注释
注释能增加可读性。在YAML中,#号右边的所有内容都是注释。

# This is a YAML comment

YAML 字符串
YAML中正常不需要放置引号,即使包含空格也不需要。如果希望使用引号,你可以使用单引号或双引号都可以。

this is a string
'this is anoter string'
"this is yet another a string"
  1. 有两种方法去写多个字符串,你能使用竖线(|)字符串去表示新行的字符,字符串中的换行符将被保留。
include_newlines: |Example Company123 Main StreetAtlanta,GA 30303
  1. 使用>字符
fold_newlines: >Example Company123 Main StreetAtlanta,GA 30303
  1. YMAL字典
    您已经看到键-值对的集合被写为缩进块 ,如下例:
  name: svcrolesvcservice: httpdsvcport: 80

字典也可以用内嵌块格式写在花括号中

  {name: svrole, svcservice: httpd, svcport: 80}

在大多数情况下,尽量避免写内嵌块格式,因为它不易读。然而,至少在一种厂家下,它是比较通用的。roles的用法在稍后被讨论。当一个playbook中包含了多个roles,它是更常用使用这种语法,使得它更容易区分包含在playbook中的roles和传递给roles的变量。

  1. YMAL列表
  hosts: - servera- serverb- serverc

列表也可以写成内嵌格式包括在方括号中

  hosts: [servera, serverb, serverc]

应该避免使用这种写法,因为它不易读。

5.老式的键值写法
应该避免使用这种写法,因为它不易读。

  tasks:- name: shorthand formservice: name=httpd enabled=true state=started

推荐写法

  tasks:- name: normal formservice: name: httpd enabled: true state: started

Chapter3.2 实现多个PLAYS相关推荐

  1. ###《Effective STL》--Chapter3

    点击查看Evernote原文. #@author: gr #@date: 2014-09-13 #@email: forgerui@gmail.com Chapter3 关联容器 Topic 22: ...

  2. CF1516E. Baby Ehab Plays with Permutations(组合数学)

    CF1516E. Baby Ehab Plays with Permutations Solution 因为组合水平不行所以只弄出来一个O(k4)O(k^4)O(k4)的做法(虽然随便改改可能就O(k ...

  3. 【Java笔记】【Java核心技术卷1】chapter3 D4变量

    package chapter3;public class D4变量 {public static final int BBB=100; //类常量public static void main(St ...

  4. C++:深入理解C++11新特性:Chapter3:左值和右值

    Chapter3:左值和右值 1. 将右值绑定到 左值 2. 将右值绑定到 常量左值引用 3. 将右值绑定到右值引用 总结: 5. 左值,右值和右值引用 6. 引用类型可以引用的的值类型 7. 全能类 ...

  5. Boboniu Plays Chess (模拟构造)

    Boboniu Plays Chess 题目链接 题意 : 一个n*m大的棋盘,在某个位置有一个棋子,棋子的走法和象棋中的"车"一样,要求输出棋子遍历完整个棋盘所有位置经过的位置. ...

  6. 论文笔记 AAAI 2021|what the role is vs. What plays the role: Semi-supervised Event Argument Extraction v

    文章目录 1 简介 1.1 动机 1.2 创新 2 方法 3 半监督双重训练策略 4 实验 1 简介 论文题目:What the role is vs. What plays the role: Se ...

  7. MaratonIME plays Cîrokime

    MaratonIME plays Cîrokime Have you ever seen flavored vodka? Everaldo, Glauber The MaratonIME member ...

  8. Codeforces 897D. Ithea Plays With Chtholly (交互)

    题目链接:D. Ithea Plays With Chtholly 题意: 给你n张纸,在纸上写字(在 1 - c之间)可以写m次数 (,).(主要是交互,让你判断) 题解: 首先,看到m>=n ...

  9. Codeforces D - Ithea Plays With Chtholly

    D - Ithea Plays With Chtholly 思路:考虑每个位置最多被替换c/2次 那么折半考虑,如果小于c/2,从左往右替换,大于c/2总右往左替换,只有小于这个数(从左往右)或者大于 ...

最新文章

  1. equals()与hashCode()
  2. ACL 2018最佳论文公布!计算语言学最前沿研究都在这里了
  3. 比较不错的一个ios找茬游戏源码
  4. 深层神经网络中的前向传播
  5. 洛谷 P1208混合牛奶【贪心】
  6. 「转型新范式」第四范式2021发布会全程直播倒计时
  7. 案例:演示out对象的使用及原理分析
  8. win7 IE9 internet explorer[IE] [IE 9]已停止工作
  9. python离散积分_科学网—python数据处理笔记(三)通道积分图 - 钱磊的博文
  10. 基于神念TGAM的脑波小车(4)
  11. Win10PE启动维护工具 | U盘WinPE下载
  12. 暨反欺诈建模场景实操
  13. flac音乐格式怎么转换mp3?
  14. 计算机病毒有熊猫病毒,世界最厉害的电脑病毒排名 熊猫烧香病毒最使人讨厌...
  15. python时间函数纳秒_python – 获取纳秒级精度的文件修改时间
  16. 2509-Druid监控功能的深入使用与配置-基于SpringBoot-完全使用 .properties配置文件
  17. matlab里面nargin,Matlab中的nargin命令
  18. 小规模 DDoS 黑客攻击摧毁了维基解密网站
  19. vscode配置C++环境(图文详解)
  20. 文件夹加密超级大师会把文件上传到服务器吗,共享文件夹加密超级大师怎么加密文件夹?...

热门文章

  1. 微信域名防封、域名检测接口api、域名跳转技术、360防拦截揭秘(三)------2020新域名防封技术解析
  2. 保时捷狂推NFT,高调喊出打造Web3社区,Web2品牌“天生缺陷”终将折戟沉沙?...
  3. 苏宁易购登录参数password2的生成过程
  4. react-native调起第三方高德地图导航URL解释
  5. 大型计算机网络主机通常采用什么型,全国计算机一级选择题真题集(1)
  6. 喵星史话(一)——猫的起源
  7. Sapphire应用场景剖析 | 基于行业首个隐私EVM构建DApp
  8. 网页设计颜色及平面设计的常用16进制+RGB色值表参考大全
  9. 张大哥笔记-如何利用网络赚钱(20种网络赚钱方法推荐)
  10. 增量爬取电影网站2级详情页面电影名称和下载链接