当前位置: 首页 > news >正文

做网站公司/谷歌官网入口

做网站公司,谷歌官网入口,四川住房与城乡建设部网站,百度云虚拟主机playbook的介绍、应用与实施 文章目录 playbook的介绍、应用与实施1. 实施playbook1.1 Ansible Playbook与临时命令1.2 格式化Ansible Playbook1.3 运行playbook1.4 提高输出的详细程度1.5 语法验证1.6 执行空运行 2. 实施多个play2.1 缩写多个play2.2 play中的远程用户和特权升…

playbook的介绍、应用与实施

文章目录

  • playbook的介绍、应用与实施
    • 1. 实施playbook
      • 1.1 Ansible Playbook与临时命令
      • 1.2 格式化Ansible Playbook
      • 1.3 运行playbook
      • 1.4 提高输出的详细程度
      • 1.5 语法验证
      • 1.6 执行空运行
    • 2. 实施多个play
      • 2.1 缩写多个play
      • 2.2 play中的远程用户和特权升级
        • 2.2.1 用户属性
        • 2.2.2 特权升级属性
      • 2.3 查找用于任务的模块
        • 2.3.1 模块文档
        • 2.3.2 Playbook语法变化
        • 2.3.3 过时的“键=值” playbook简写

1. 实施playbook

1.1 Ansible Playbook与临时命令

临时命令可以作为一次性命令对一组目标主机运行一项简单的任务。不过,若要真正发挥Ansible的力量,需要了解如何使用playbook以便轻松重复的方式对一组目标主机执行多项复杂的任务。

play是针对清单中选定的主机运行的一组有序任务。playbook是一个文本文件,其中包含由一个或多个按特定顺序运行的play组成的列表。

Play可以将一系列冗长而复杂的手动管理任务转变为可轻松重复的例程,并且具有可预测的成功成果。在playbook中,可以将play内的任务序列保存为人类可读并可立即运行的形式。根据任务的编写方式,任务本身记录了部署应用或基础架构所需的步骤。

1.2 格式化Ansible Playbook

前面我们学习了临时命令模块,下面以一条命令做为案例来讲解下其在playbook中是如何编写的。

ansible 192.168.116.140 -m user -a 'name=runtime uid=4000 state=present'

这个任务可以将其编写为一个单任务的play并保存在playbook中。生成的playbook如下方所示:

[root@node1 ~]# id tom
id: ‘tom’: no such user[root@controller ~]# vim test.yaml
[root@controller ~]# cat test.yaml
---
- name: create a userhosts: 192.168.116.140tasks:- name: create user user: name: tomuid: 2000state: present
[root@controller ~]# ansible-playbook test.yamlPLAY [create a user] ************************************************************************************************************TASK [Gathering Facts] **********************************************************************************************************
[WARNING]: Platform linux on host 192.168.116.140 is using the discovered Python interpreter at /usr/bin/python, but future
installation of another Python interpreter could change this. See
https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information.
ok: [192.168.116.140]TASK [create user] **************************************************************************************************************
changed: [192.168.116.140]PLAY RECAP **********************************************************************************************************************
192.168.116.140            : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   [root@node1 ~]# id tom
uid=2000(tom) gid=2000(tom) groups=2000(tom)

Playbook是以YAML格式编写的文本文件,通常使用扩展名yml保存。Playbook使用空格字符缩进来表示其数据结构。YAML对用于缩进的空格数量没有严格的要求,但有两个基本的规则:

  • 处于层次结构中同一级别的数据元素(例如同一列表中的项目)必须具有相同的缩进量。
  • 如果项目属于其他项目的子项,其缩进量必须大于父项

只有空格字符可用于缩进,不允许使用tab键。约定俗成的缩进量一般是一级2个空格。

Playbook开头的一行由三个破折号(—)组成,这是文档开始标记。其末尾可能使用三个圆点(…)作为文档结束标记,尽管在实践中这通常会省略。

在这两个标记之间,会以一个play列表的形式来定义playbook。YAML列表中的项目以一个破折号加空格开头。例如,YAML列表可能显示如下:

- apple
- orange
- grape

Play本身是一个键值对集合。同一play中的键应当使用相同的缩进量。以下示例显示了具有三个键的YAML代码片段。前两个键具有简单的值。第三个将含有三个项目的列表作为值。

- name: just an examplehosts: webserverstasks:- first- second- third

作为play中的一部分,tasks属性按顺序实际列出要在受管主机上运行的任务。列表中各项任务本身是一个键值对集合。

还以上面创建用户的play为例,play中唯一任务有两个键:

  • name是记录任务用途的可选标签。最好命名所有的任务,从而帮助记录自动流程中的每一步用途。
  • user是要为这个任务运行的模块。其参数作为一组键值对传递,它们是模块的子项(name、uid和state)。

下面再来看一个含有多项任务的tasks属性案例:

tasks:- name: web server is enabledservice:name: httpdenabled: true- name: NTP server is enabledservice:name: chronydenabled: true- name: Postfix is enabledservice:name: postfixenabled: true

playbook中play和任务列出的顺序很重要,因为Ansible会按照相同的顺序运行它们。

1.3 运行playbook

absible-playbook命令可用于运行playbook。该命令在控制节点上执行,要运行的playbook的名称则作为参数传递。

ansible-playbook test.yml

在运行playbook时,将生成输出来显示所执行的play和任务。输出中也会报告执行的每一项任务的结果。

以下示例中显示了一个简单的playbook的内容,后面是运行它的结果。

[root@node1 ~]# rpm -qa|grep httpd //没运行之前啥也没有[root@controller ~]# mkdir httpd
[root@controller ~]# cd httpd
[root@controller httpd]# vim apache.yaml
[root@controller httpd]# cat apache.yaml
---
- name: bushu httpd fuwuhosts: 192.168.116.140tasks:- name: anzhaung httpd fuwu yum:name: httpdstate: present- name: kaiqi httpd fuwuservice:name: httpdstate: started- name: set httpd enableservice:name: httpdenabled: yes
[root@controller httpd]# ansible-playbook apache.yamlPLAY [bushu httpd fuwu] *********************************************************************************************************TASK [Gathering Facts] **********************************************************************************************************
[WARNING]: Platform linux on host 192.168.116.140 is using the discovered Python interpreter at /usr/bin/python, but future
installation of another Python interpreter could change this. See
https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information.
ok: [192.168.116.140]TASK [anzhaung httpd fuwu] ******************************************************************************************************
changed: [192.168.116.140]TASK [kaiqi httpd fuwu] *********************************************************************************************************
changed: [192.168.116.140]TASK [set httpd enable] *********************************************************************************************************
changed: [192.168.116.140]PLAY RECAP **********************************************************************************************************************
192.168.116.140            : ok=4    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   [root@node1 ~]# rpm -qa|grep httpd       //运行之后
httpd-tools-2.4.57-5.el9.x86_64
httpd-filesystem-2.4.57-5.el9.noarch
rocky-logos-httpd-90.14-2.el9.noarch
httpd-core-2.4.57-5.el9.x86_64
httpd-2.4.57-5.el9.x86_64
[root@node1 ~]# systemctl status httpd
● httpd.service - The Apache HTTP ServerLoaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; preset: disabled)Active: active (running) since Sat 2023-12-23 16:40:10 CST; 1min 28s agoDocs: man:httpd.service(8)Main PID: 713843 (httpd)Status: "Total requests: 0; Idle/Busy workers 100/0;Requests/sec: 0; Bytes served/sec:   0 B/sec"Tasks: 213 (limit: 10820)Memory: 41.6MCPU: 171msCGroup: /system.slice/httpd.service├─713843 /usr/sbin/httpd -DFOREGROUND├─714844 /usr/sbin/httpd -DFOREGROUND├─714845 /usr/sbin/httpd -DFOREGROUND├─714846 /usr/sbin/httpd -DFOREGROUND└─714847 /usr/sbin/httpd -DFOREGROUND

请注意,在playbook运行时,屏幕中会显示每个play和任务的name键的值。(Gathering Facts任务是一项特别的任务,setup模块通常在play启动时自动运行这项任务。)对于含有多个play和任务的playbook,设置name属性后可以更加轻松地监控playbook执行的进展。

通常而言,Ansible Playbook中的任务是幂等的,而且能够安全地多次运行playbook。如果目标受管主机已处于正确的状态,则不应进行任何更改。如果再次运行这个playbook,所有任务都会以状态OK传递,且不报告任何更改。

1.4 提高输出的详细程度

ansible-playbook命令提供的默认输出不提供详细的任务执行信息。ansible-playbook -v命令提供了额外的信息,总共有四个级别。

配置Playbook执行的输出详细程序

选项描述
-v显示任务结果
-vv任务结果和任务配置都会显示
-vvv包含关于与受管主机连接的信息
-vvvv增加了连接插件相关的额外详细程序选项,包括受管主机上用于执行脚本的用户以及所执行的脚本

下面来演示一下效果

//一个v
[root@controller httpd]# ansible-playbook -v apache.yaml
Using /etc/ansible/ansible.cfg as config filePLAY [bushu httpd fuwu] *********************************************************************************************************TASK [Gathering Facts] *********************************************************************************************************
ok: [192.168.116.140]TASK [anzhaung httpd fuwu] ******************************************************************************************************
ok: [192.168.116.140] => {"changed": false, "msg": "Nothing to do", "rc": 0, "results": []}TASK [kaiqi httpd fuwu] *********************************************************************************************************
ok: [192.168.116.140] => {"changed": false, "name": "httpd", "state": "started", "status": {"AccessSELinuxContext": "system_u:object_r:httpd_unit_file_t:s0", "ActiveEnterTimestamp": "Sat 2023-12-23 16:40:10 CST", "ActiveEnterTimestampMonotonic": "17391422769
.....................     //内容有点多省略了
PLAY RECAP **********************************************************************************************************************
192.168.116.140            : ok=4    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   //两个vv
[root@controller httpd]# ansible-playbook -vv apache.yaml
ansible-playbook 2.9.27config file = /etc/ansible/ansible.cfgconfigured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']ansible python module location = /usr/lib/python3.6/site-packages/ansibleexecutable location = /usr/bin/ansible-playbookpython version = 3.6.8 (default, Dec  5 2019, 15:45:45) [GCC 8.3.1 20191121 (Red Hat 8.3.1-5)]
Using /etc/ansible/ansible.cfg as config file
Skipping callback 'actionable', as we already have a stdout callback.
Skipping callback 'counter_enabled', as we already have a stdout callback.
Skipping callback 'debug', as we already have a stdout callback.
Skipping callback 'dense', as we already have a stdout callback.
Skipping callback 'dense', as we already have a stdout callback.
Skipping callback 'full_skip', as we already have a stdout callback.
Skipping callback 'json', as we already have a stdout callback.
Skipping callback 'minimal', as we already have a stdout callback.
Skipping callback 'null', as we already have a stdout callback.
Skipping callback 'oneline', as we already have a stdout callback.
Skipping callback 'selective', as we already have a stdout callback.
Skipping callback 'skippy', as we already have a stdout callback.
Skipping callback 'stderr', as we already have a stdout callback.
Skipping callback 'unixy', as we already have a stdout callback.
Skipping callback 'yaml', as we already have a stdout callback.PLAYBOOK: apache.yaml ***********************************************************************************************************
1 plays in apache.yamlPLAY [bushu httpd fuwu] *********************************************************************************************************TASK [Gathering Facts] **********************************************************************************************************
task path: /root/httpd/apache.yaml:2
....................         //省略
PLAY RECAP **********************************************************************************************************************
192.168.116.140            : ok=4    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   //三个vvv
[root@controller httpd]# ansible-playbook -vvv apache.yaml//四个vvvv
[root@controller httpd]# ansible-playbook -vvvv apache.yaml

1.5 语法验证

在执行playbook之前,最好要进行验证,确保其内容的语法正确无误。ansible-playbook命令提供了一个–syntax-check选项,可用于验证playbook的语法。

下例演示了一个playbook成功通过语法验证:

[root@controller httpd]# ansible-playbook --syntax-check apache.yamlplaybook: apache.yaml

语法验证失败时,将报告语法错误。输出中包含语法问题在playbook中的大致位置。

下例演示了一个playbook语法验证失败的情况:

[root@controller httpd]# vim apache.yaml
[root@controller httpd]# cat apache.yaml
---
- name: bushu httpd fuwuhosts: 192.168.116.140tasks:                //这里多了一个空格测试语法- name: anzhaung httpd fuwu yum:name: httpdstate: present- name: kaiqi httpd fuwuservice:name: httpdstate: started- name: set httpd enableservice:name: httpdenabled: yes
[root@controller httpd]# ansible-playbook --syntax-check apache.yaml
ERROR! We were unable to read either as JSON nor YAML, these are the errors we got from each:
JSON: Expecting value: line 1 column 1 (char 0)Syntax Error while loading YAML.mapping values are not allowed in this contextThe error appears to be in '/root/httpd/apache.yaml': line 4, column 9, but may
be elsewhere in the file depending on the exact syntax problem.The offending line appears to be:hosts: 192.168.116.140tasks:^ here

1.6 执行空运行

可以使用-C选项对playbook执行空运行。这会使Ansible报告在执行该playbook时将会发生什么更改,但不会对受管主机进行任何实际的更改。

下例演示了一个playbook的空运行,它包含单项任务,可确保在受管主机上安装了最新版本的httpd软件包。注意该空运行报告此任务会对受管主机产生的更改。

[root@node1 ~]# rpm -qa | grep vsftpd  //没安装之前啥也没有[root@controller ~]# vim vsftpd.yaml
[root@controller ~]# cat vsftpd.yaml
---
- name: bushu vsftpd fuwuhosts: 192.168.116.140tasks: - name: anzhuang vsftpd fuwuyum: name: vsftpdstate: present
[root@controller ~]# ansible-playbook -C vsftpd.yamlPLAY [bushu vsftpd fuwu] ********************************************************************************************************TASK [Gathering Facts] **********************************************************************************************************
[WARNING]: Platform linux on host 192.168.116.140 is using the discovered Python interpreter at /usr/bin/python, but future
installation of another Python interpreter could change this. See
https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information.
ok: [192.168.116.140]TASK [anzhuang vsftpd fuwu] *****************************************************************************************************
changed: [192.168.116.140]PLAY RECAP **********************************************************************************************************************
192.168.116.140            : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   [root@node1 ~]# rpm -qa | grep vsftpd  //还是啥也没有[root@controller ~]# ansible-playbook vsftpd.yaml       //没加-CPLAY [bushu vsftpd fuwu] ********************************************************************************************************TASK [Gathering Facts] **********************************************************************************************************
[WARNING]: Platform linux on host 192.168.116.140 is using the discovered Python interpreter at /usr/bin/python, but future
installation of another Python interpreter could change this. See
https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information.
ok: [192.168.116.140]TASK [anzhuang vsftpd fuwu] *****************************************************************************************************
changed: [192.168.116.140]PLAY RECAP **********************************************************************************************************************
192.168.116.140            : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   [root@node1 ~]# rpm -qa | grep vsftpd
vsftpd-3.0.5-5.el9.x86_64

2. 实施多个play

2.1 缩写多个play

Playbook是一个YAML文件,含有由一个或多个play组成的列表。记住一个play按顺序列出了要对清单中的选定主机执行的任务。因此,如果一个playbook中有多个play,每个play可以将其任务应用到单独的一组主机。

在编排可能涉及对不同主机执行不同任务的复杂部署时,这会大有帮助。我们可以这样进行编写:对一组主机运行一个play,完成后再对另一组主机运行另一个play。

缩写包含多个play的playbook非常简单。Playbook中的各个play编写为playbook中的顶级列表项。各个play是含有常用play关键字的列表项。

以下示例显示了含有两个play的简单playbook。第一个play针对192.168.116.140运行,第二个play则针对192.168.116.143运行。

[root@controller ~]# vim httpd.yaml
[root@controller ~]# cat httpd.yaml
---
- name: bushu httpd fuwuhosts: 192.168.116.140tasks:- name: create a httpd fuwuyum:name: httpdstate: present- name: bushu httpdhosts: 192.168.116.143tasks: - name: create a httpd fuwuyum: name: httpdstate: present
[root@controller ~]# ansible-playbook httpd.yamlPLAY [bushu httpd fuwu] *********************************************************************************************************TASK [Gathering Facts] ********************************************************************************************************
ok: [192.168.116.140]TASK [create a httpd fuwu] ******************************************************************************************************
changed: [192.168.116.140]PLAY [bushu httpd] **************************************************************************************************************TASK [Gathering Facts] ********************************************************************************************************
ok: [192.168.116.143]TASK [create a httpd fuwu] ******************************************************************************************************
changed: [192.168.116.143]PLAY RECAP **********************************************************************************************************************
192.168.116.140            : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
192.168.116.143            : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   [root@node1 ~]# rpm -qa | grep httpd
httpd-tools-2.4.57-5.el9.x86_64
httpd-filesystem-2.4.57-5.el9.noarch
rocky-logos-httpd-90.14-2.el9.noarch
httpd-core-2.4.57-5.el9.x86_64
httpd-2.4.57-5.el9.x86_64[root@node2 ~]# rpm -qa | grep httpd
httpd-tools-2.4.57-5.el9.x86_64
httpd-filesystem-2.4.57-5.el9.noarch
rocky-logos-httpd-90.14-2.el9.noarch
httpd-core-2.4.57-5.el9.x86_64
httpd-2.4.57-5.el9.x86_64

2.2 play中的远程用户和特权升级

Play可以将不同的远程用户或特权升级设置用于play,取代配置文件中指定的默认设置。这些在play本身中与hosts或tasks关键字相同的级别上设置。

2.2.1 用户属性

playbook中的任务通常通过与受管主机的网络连接来执行。与临时命令相同,用于任务执行的用户帐户取决于Ansible配置文件/etc/ansible/ansible.cfg中的不同关键字。运行任务的用户可以通过remote_user关键字来定义。不过,如果启用了特权升级,become_user等其他关键字也会发生作用。

如果用于任务执行的Ansible配置中定义的远程用户不合适,可以通过在play中使用remote_user关键字覆盖。

[root@controller ~]# vim test.yaml
[root@controller ~]# cat test.yaml
---
- name: create a userremote_user: root                 //指定用户hosts: 192.168.116.140tasks:- name: create user user: name: tomuid: 2000state: present
[root@controller ~]# ansible-playbook test.yaml
PLAY [create a user] ************************************************************************************************************TASK [Gathering Facts] ********************************************************************************************************
ok: [192.168.116.140]TASK [create user] **************************************************************************************************************
ok: [192.168.116.140]PLAY RECAP **********************************************************************************************************************
192.168.116.140            : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
2.2.2 特权升级属性

Ansible也提供额外的关键字,从而在playbook内定义特权升级参数。become布尔值关键字可用于启用或禁用特权升级,无论它在Ansible配置文件中的定义为何。它可取yes或true值来启用特权升级,或者取no或false值来禁用它。、

[root@controller ~]# vim test.yaml
[root@controller ~]# cat test.yaml
---
- name: create a userremote_user: rootbecome: no                  //指定是否启用特权hosts: 192.168.116.140tasks:- name: create user user: name: tomuid: 2000state: present
[root@controller ~]# ansible-playbook test.yamlPLAY [create a user] ************************************************************************************************************TASK [Gathering Facts] ********************************************************************************************************
ok: [192.168.116.140]TASK [create user] **************************************************************************************************************
ok: [192.168.116.140]PLAY RECAP **********************************************************************************************************************
192.168.116.140            : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

如果启用了特权升级,则可以使用become_method关键字来定义特定play期间要使用的特权升级方法。

以下示例中指定sudo用于特权升级:

become_method: sudo

此外,启用了特权升级时,become_user关键字可定义特定play上下文内要用于特权升级的用户帐户。

become_user: privileged_user

2.3 查找用于任务的模块

2.3.1 模块文档

Ansible随附打包的大量模块为管理员提供了许多用于常见管理任务的工具。前面我们介绍了Ansible官方网站的帮助文档链接https://docs.ansible.com/。通过模块索引,可以很轻松的找到对应的模块。例如,适用于用户和服务管理的模块可以在Systems Modules下找到,而适合数据库管理的模块则可在Database Modules下找到。

对于每一个模块,Ansible官网提供了其功能摘要,以及关于如何通过模块的选项来调用各项具体功能的说明。文档还提供了实用的示例,演示各个模块的用法,以及任务中关键字的设置方法。

前面我们用到过ansible-doc -l命令。这将显示模块名称列表以及其功能的概要。

ansible-doc -l

使用ansible-doc [module name]命令来显示模块的详细文档。与Ansible官网一样,该命令提供模块功能的概要、其不同选项的详细信息,以及示例。

ansible-doc yum     # 显示yum模块的帮助文档

ansible-doc命令还提供-s选项,它会生成示例输出,可以充当如何在playbook在使用特定模块的示范。此输出可以作为起步模板,包含在实施该模块以执行任务的playbook中。输出中包含的注释,提醒管理员各个选项的用法。下例演示了yum模块的这种输出:

ansible-doc -s yum

使用ansible-doc命令可以查找和了解如何使用模块。尽管command、shell和raw模块的用法可能看似简单,但在可能时,应尽量避免在playbook中使用它们因为它们可以取胜任意命令,因此使用这些模块时很容易写出非幂等的playbook。

例如,以下使用shell模块的任务为非幂等。每次运行play时,它都会重写/etc/resolv.conf,即使它已经包含了行nameserver 192.168.116.140。

[root@controller ~]# vim test.yaml
[root@controller ~]# cat test.yaml
---
- name: xiang wenjian xieru shujuhosts: 192.168.116.140tasks:- name: xie shujushell: echo 'hello world' > /tmp/abc.txt 
[root@controller ~]# ansible-playbook test.yamlPLAY [xiang wenjian xieru shuju] ************************************************************************************************TASK [Gathering Facts] **********************************************************************************************************
ok: [192.168.116.140]TASK [xie shuju] ****************************************************************************************************************
changed: [192.168.116.140]PLAY RECAP **********************************************************************************************************************
192.168.116.140            : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
[root@controller ~]# ansible 192.168.116.140 -a 'cat /tmp/abc.txt'
192.168.116.140 | CHANGED | rc=0 >>
hello world

可以通过多种方式编写以幂等方式使用shell模块的任务,而且有时候进行这些更改并使用shell是最佳的做法。但更快的方案或许是使用ansible-doc发现copy模块,再使用它获得所需的效果。

在以下示例中,如果/etc/resolv.conf文件已包含正确的内容,则不会重写该文件:

[root@controller ~]# ansible 192.168.116.140 -m shell -a 'cat /etc/hosts'
[WARNING]: Platform linux on host 192.168.116.140 is using the discovered Python interpreter at /usr/bin/python, but future
installation of another Python interpreter could change this. See
https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information.
192.168.116.140 | CHANGED | rc=0 >>
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
[root@controller ~]# vim test.yaml
[root@controller ~]# cat test.yaml
---
- name: fuzhi yige wenjianhosts: 192.168.116.140tasks:- name: xiang mubiao wenjian fuzhi neironcopy:dest: /etc/hostscontent: '192.168.116.140 node1\n'
[root@controller ~]# ansible-playbook test.yamlPLAY [fuzhi yige wenjian] *******************************************************************************************************TASK [Gathering Facts] **********************************************************************************************************
[WARNING]: Platform linux on host 192.168.116.140 is using the discovered Python interpreter at /usr/bin/python, but future
installation of another Python interpreter could change this. See
https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information.
ok: [192.168.116.140]TASK [xiang mubiao wenjian fuzhi neiron] ****************************************************************************************
changed: [192.168.116.140]PLAY RECAP **********************************************************************************************************************
192.168.116.140            : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   [root@controller ~]# ansible 192.168.116.140 -m shell -a 'cat /etc/hosts'
[WARNING]: Platform linux on host 192.168.116.140 is using the discovered Python interpreter at /usr/bin/python, but future
installation of another Python interpreter could change this. See
https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information.
192.168.116.140 | CHANGED | rc=0 >>
192.168.116.140 node1

copy模块可以测试来了解是否达到了需要的状态,如果已达到,则不进行任何更改。shell模块容许非常大的灵活性,但需要格外小心,从而确保它以幂等方式运行。

幂等的playbook可以重复运行,确保系统处于特定的状态,而不会破坏状态已经正确的系统。

2.3.2 Playbook语法变化

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

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

YAML字符串
YAML中的字符串通常不需要放在引号里,即使字符串中包含空格。字符串可以用双引号或单引号括起。

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

编写多行字符串有两种方式。可以使用管道符表示要保留字符串中的换行字符。

[root@controller ~]# vim test.yaml
[root@controller ~]# cat test.yaml
---
- name: xiang wenjian xieru neironhosts: 192.168.116.140tasks:- name: xiang wenjian xieru neironcopy: dest: /tmp/123content: |hello worldhello chinahello wuhan
[root@controller ~]# ansible-playbook test.yamlPLAY [xiang wenjian xieru neiron] ***********************************************************************************************TASK [Gathering Facts] **********************************************************************************************************
[WARNING]: Platform linux on host 192.168.116.140 is using the discovered Python interpreter at /usr/bin/python, but future
installation of another Python interpreter could change this. See
https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information.
ok: [192.168.116.140]TASK [xiang wenjian xieru neiron] ***********************************************************************************************
changed: [192.168.116.140]PLAY RECAP **********************************************************************************************************************
192.168.116.140            : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   [root@controller ~]# ansible 192.168.116.140 -a 'cat /tmp/123'
[WARNING]: Platform linux on host 192.168.116.140 is using the discovered Python interpreter at /usr/bin/python, but future
installation of another Python interpreter could change this. See
https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information.
192.168.116.140 | CHANGED | rc=0 >>
hello world
hello china
hello wuhan

要编写多行字符串,还可以使用大于号字符来表示换行字符转换成空格并且行内的引导空白将被删除。这种方法通常用于将很长的字符串在空格字符处断行,使它们跨占多行来提高可读性。

[root@controller ~]# vim test.yaml
[root@controller ~]# cat test.yaml
---
- name: xiang wenjian xieru neironhosts: 192.168.116.140tasks:- name: xiang wenjian xieru neironcopy: dest: /tmp/123content: >hello worldhello chinahello wuhan
[root@controller ~]# ansible-playbook test.yamlPLAY [xiang wenjian xieru neiron] ***********************************************************************************************TASK [Gathering Facts] **********************************************************************************************************
[WARNING]: Platform linux on host 192.168.116.140 is using the discovered Python interpreter at /usr/bin/python, but future
installation of another Python interpreter could change this. See
https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information.
ok: [192.168.116.140]TASK [xiang wenjian xieru neiron] ***********************************************************************************************
changed: [192.168.116.140]PLAY RECAP **********************************************************************************************************************
192.168.116.140            : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   [root@controller ~]# ansible 192.168.116.140 -a 'cat /tmp/123'
[WARNING]: Platform linux on host 192.168.116.140 is using the discovered Python interpreter at /usr/bin/python, but future
installation of another Python interpreter could change this. See
https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information.
192.168.116.140 | CHANGED | rc=0 >>
hello world hello china hello wuhan

YAML字典
下面是一个简单的字典形式:

name: svcrole
svcservice: httpd
svcport: 80

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

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

大多数情况下应避免内联块格式,因为其可读性较低。不过,至少有一种情形中会较常使用它。当playbook中包含角色列表时,较常使用这种语法,从而更加容易区分play中包含的角色和传递给角色的变量。

YAML列表
最简单的列表如下:

hosts:- servera- serverb- serverc

列表也有以中括号括起的内联格式,如下所示:

hosts: [servera, serverb, serverc]

我们应该避免使用此语法,因为它通常更难阅读。

2.3.3 过时的“键=值” playbook简写

某些playbook可能使用较旧的简写方法,通过将模块的键值对放在与模块名称相同的行上来定义任务。例如,你可能会看到这种语法:

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

通常我们应该将这样的语法编写为如下所示:

tasks:- name: normal formservice:name: httpdenabled: truestate: started

通常我们应避免简写形式,而使用普通形式。

普通形式的行数较多,但更容易操作。任务的关键字垂直堆叠,更容易区分。阅读play时,眼睛直接向一扫视,左右运动较少。而且,普通语法是原生的YAML。

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

相关文章:

playbook的介绍、应用与实施

playbook的介绍、应用与实施 文章目录 playbook的介绍、应用与实施1. 实施playbook1.1 Ansible Playbook与临时命令1.2 格式化Ansible Playbook1.3 运行playbook1.4 提高输出的详细程度1.5 语法验证1.6 执行空运行 2. 实施多个play2.1 缩写多个play2.2 play中的远程用户和特权升…...

uniApp使用XR-Frame创建3D场景(5)材质贴图的运用

上一篇讲解了如何在uniApp中创建xr-frame子组件并创建简单的3D场景。 这篇我们讲解在xr-frame中如何给几何体赋予贴图材质。 先看源码 <xr-scene render-system"alpha:true" bind:ready"handleReady"><xr-node><xr-assets><xr-asse…...

阿里云CentOS7安装Hadoop3伪分布式

ECS准备 开通阿里云ECS 略 控制台设置密码 连接ECS 远程连接工具连接阿里云ECS实例&#xff0c;这里远程连接工具使用xshell 根据提示接受密钥 根据提示写用户名和密码 用户名&#xff1a;root 密码&#xff1a;在控制台设置的密码 修改主机名 将主机名从localhost改为需要…...

78.子集90.子集2

78.子集 思路 又回到了组合的模板中来&#xff0c;这道题相比于前面的题省去了递归终止条件。大差不差。 代码 class Solution {List<List<Integer>> result new ArrayList<>();LinkedList<Integer> listnew LinkedList<>();public List<…...

基于Ubuntu的Linux系统安装jsoncpp开发包过程

执行以下命令&#xff1a; sudo apt update sudo apt install libjsoncpp-dev有可能出现的问题&#xff1a; 1.如果在执行sudo apt update时出现以下信息 Hit:1 http://mirrors.aliyun.com/ubuntu bionic InRelease Hit:2 http://mirrors.aliyun.com/ubuntu bionic-security…...

葵花卫星影像应用场景及数据获取

一、卫星参数 葵花卫星是由中国航天科技集团公司研制的一颗光学遥感卫星&#xff0c;代号CAS-03。该卫星于2016年11月9日成功发射&#xff0c;位于地球同步轨道&#xff0c;轨道高度约为35786公里&#xff0c;倾角为0。卫星设计寿命为5年&#xff0c;搭载了高分辨率光学相机和多…...

Jenkins升级中的小问题

文章目录 使用固定版本安装根据jenkins页面下载war包升级jenkins重启jenkins报错问题解决 K8s部署过程中的一些小问题 ##### Jenkins版本小插曲 ​ 在Jenkins环境进行插件安装时全部清一色飘红&#xff0c;发现是因为Jenkins版本过低导致&#xff0c;报错的位置可以找到更新je…...

Apache Hive的基本使用语法(二)

Hive SQL操作 7、修改表 表重命名 alter table score4 rename to score5;修改表属性值 # 修改内外表属性 ALTER TABLE table_name SET TBLPROPERTIES("EXTERNAL""TRUE"); # 修改表注释 ALTER TABLE table_name SET TBLPROPERTIES (comment new_commen…...

基于单片机16位智能抢答器设计

**单片机设计介绍&#xff0c;基于单片机16位智能抢答器设计 文章目录 一 概要二、功能设计设计思路 三、 软件设计原理图 五、 程序六、 文章目录 一 概要 基于单片机16位智能抢答器设计是一个结合了单片机技术、显示技术、按键输入技术以及声音提示技术的综合性项目。其设计…...

idea默认代码生成脚本修改

修改了下idea自带的代码生成脚本&#xff0c;增加了脚本代码的注释&#xff0c;生成了controller&#xff0c;service&#xff0c;impl&#xff0c;mapper&#xff0c;里面都是空的&#xff0c;具体可以根据自己的代码习惯增加 代码生成脚本的使用可以看下使用 idea 生成实体类…...

StarRocks实战——多点大数据数仓构建

目录 前言 一、背景介绍 二、原有架构的痛点 2.1 技术成本 2.2 开发成本 2.2.1 离线 T1 更新的分析场景 2.2.2 实时更新分析场景 2.2.3 固定维度分析场景 2.2.4 运维成本 三、选择StarRocks的原因 3.1 引擎收敛 3.2 “大宽表”模型替换 3.3 简化Lambda架构 3.4 模…...

jmeter总结之:Regular Expression Extractor元件

Regular Expression Extractor是一个后处理器元件&#xff0c;使用正则从服务器的响应中提取数据&#xff0c;并将这些数据保存到JMeter变量中&#xff0c;以便在后续的请求或断言中使用。在处理动态数据或验证响应中的特定信息时很有用。 添加Regular Expression Extractor元…...

快速上手Spring Cloud 七:事件驱动架构与Spring Cloud

快速上手Spring Cloud 一&#xff1a;Spring Cloud 简介 快速上手Spring Cloud 二&#xff1a;核心组件解析 快速上手Spring Cloud 三&#xff1a;API网关深入探索与实战应用 快速上手Spring Cloud 四&#xff1a;微服务治理与安全 快速上手Spring Cloud 五&#xff1a;Spring …...

leetcode 1997.访问完所有房间的第一天

思路&#xff1a;动态规划前缀和 这道题还是很难的&#xff0c;因为你如果需要推出状态方程是很难想的。 在题中我们其实可以发现&#xff0c;这里在访问nextVisit数组的过程中&#xff0c;其实就是对于当前访问的房子之前的房子进行了回访。 怎么说呢&#xff1f;比如你现在…...

【InternLM 实战营第二期笔记】书生·浦语大模型全链路开源体系及InternLM2技术报告笔记

大模型 大模型成为发展通用人工智能的重要途径 专用模型&#xff1a;针对特定任务&#xff0c;一个模型解决一个问题 通用大模型&#xff1a;一个模型应对多种任务、多种模态 书生浦语大模型开源历程 2023.6.7&#xff1a;InternLM千亿参数语言大模型发布 2023.7.6&#…...

Netty对Channel事件的处理以及空轮询Bug的解决

继续上一篇Netty文章&#xff0c;这篇文章主要分析Netty对Channel事件的处理以及空轮询Bug的解决 当Netty中采用循环处理事件和提交的任务时 由于此时我在客户端建立连接&#xff0c;此时服务端没有提交任何任务 此时select方法让Selector进入无休止的阻塞等待 此时selectCnt进…...

【PostgreSQL】- 1.1 在 Debian 12 上安装 PostgreSQL 15

官方说明参考 &#xff08;原文 PostgreSQL&#xff1a;Linux 下载 &#xff08;Debian&#xff09;&#xff09; 默认情况下&#xff0c;PostgreSQL 在所有 Debian 版本中都可用。但是&#xff0c; Debians 的稳定版本“快照”了特定版本的 PostgreSQL 然后在该 Debian 版本的…...

第4章.精通标准提示,引领ChatGPT精准输出

标准提示 标准提示&#xff0c;是引导ChatGPT输出的一个简单方法&#xff0c;它提供了一个具体的任务让模型完成。 如果你要生成一篇新闻摘要。你只要发送指示词&#xff1a;汇总这篇新闻 : …… 提示公式&#xff1a;生成[任务] 生成新闻文章的摘要&#xff1a; 任务&#x…...

HTTP状态 405 - 方法不允许

方法有问题。 用Post发的请求&#xff0c;然后用Put接收的。 大家也可以看看是不是有这种问题 <body><h1>HTTP状态 405 - 方法不允许</h1><hr class"line" /><p><b>类型</b> 状态报告</p><p><b>消息…...

题目 2898: 二维数组回形遍历

题目描述: 给定一个row行col列的整数数组array&#xff0c;要求从array[0][0]元素开始&#xff0c;按回形从外向内顺时针顺序遍历整个数组。如图所示&#xff1a; 代码: package lanqiao;import java.math.BigInteger; import java.util.*;public class Main {public static …...

Git命令上传本地项目至github

记录如何创建个人仓库并上传已有代码至github in MacOS环境 0. 首先下载git 方法很多 这里就不介绍了 1. Github Create a new repository 先在github上创建一个空仓库&#xff0c;用于一会儿链接项目文件&#xff0c;按照自己的需求设置name和是否private 2.push an exis…...

机器学习之决策树现成的模型使用

目录 须知 DecisionTreeClassifier sklearn.tree.plot_tree cost_complexity_pruning_path(X_train, y_train) CART分类树算法 基尼指数 分类树的构建思想 对于离散的数据 对于连续值 剪枝策略 剪枝是什么 剪枝的分类 预剪枝 后剪枝 后剪枝策略体现之威斯康辛州乳…...

【python分析实战】成本:揭示电商平台月度开支与成本结构占比 - 过于详细 【收藏】

重点关注本文思路&#xff0c;用python分析&#xff0c;方便大家实验复现&#xff0c;代码每次都用全量的&#xff0c;其他工具自行选择。 全文3000字&#xff0c;阅读10min&#xff0c;操作1小时 企业案例实战欢迎关注专栏 每日更新&#xff1a;https://blog.csdn.net/cciehl/…...

新网站收录时间是多久,新建网站多久被百度收录

对于新建的网站而言&#xff0c;被搜索引擎收录是非常重要的一步&#xff0c;它标志着网站的正式上线和对外开放。然而&#xff0c;新网站被搜索引擎收录需要一定的时间&#xff0c;而且时间长短受多种因素影响。本文将探讨新网站收录需要多长时间&#xff0c;以及新建网站多久…...

通过Caliper进行压力测试程序,且汇总压力测试问题解决

环境要求 第一步. 配置基本环境 部署Caliper的计算机需要有外网权限;操作系统版本需要满足以下要求:Ubuntu >= 16.04、CentOS >= 7或MacOS >= 10.14;部署Caliper的计算机需要安装有以下软件:python 2.7、make、g++(gcc-c++)、gcc及git。第二步. 安装NodeJS # …...

LabVIEW比例流量阀自动测试系统

LabVIEW比例流量阀自动测试系统 开发了一套基于LabVIEW编程和PLC控制的比例流量阀自动测试系统。通过引入改进的FCMAC算法至测试回路的压力控制系统&#xff0c;有效提升了压力控制效果&#xff0c;展现了系统的设计理念和实现方法。 项目背景&#xff1a; 比例流量阀在液压…...

安卓U3D逆向从Assembly-CSharp到il2cpp

随着unity技术的发展及厂商对于脚本源码的保护&#xff0c;很大一部分U3D应用的scripting backend已经由mono转为了il2cpp&#xff0c;本文从unity简单应用的制作讲起&#xff0c;介绍U3D应用脚本的Assembly-CSharp.dll的逆向及il2cpp.so的逆向分析。 目录如下&#xff1a; 0…...

计算机网络——30SDN控制平面

SDN控制平面 SDN架构 数据平面交换机 快速、简单&#xff0c;商业化交换设备采用硬件实现通用转发功能流表被控制器计算和安装基于南向API&#xff0c;SDN控制器访问基于流的交换机 定义了哪些可以被控制哪些不能 也定义了和控制器的协议 SDN控制器&#xff08;网络OS&#…...

Obsidian插件-高亮块(Admonition)

在插件市场里面搜索Admonition并安装插件&#xff0c;就可以使用高亮块了。 添加高亮块 用法稍微有一些不同。按照下面的格式&#xff0c;输入Markdown就可以创建一个高亮块。 内容内容内容输入*ad-*会出现相应的类型可以选择...

jHipster 之 webflux-前端用EventSource处理sse变成了批量处理而非实时处理

现象&#xff1a; const eventSource new EventSource(API_URL5);eventSource.onmessage streamEvent > {console.log(a message is come in--------->);const content streamEvent.data;console.log(Received content: content);};前端用EventSource 处理webflux的…...