RHCE Ansible 系列 #4:Ansible 变量、事实和寄存器

您的托管系统中总会有很多差异。 因此,您需要学习如何使用 Ansible 变量。

在本教程中,您将学习如何定义和引用变量 Ansible。 您还将学习如何使用 Ansible 事实来检索托管节点上的信息。

此外,您还将学习如何使用寄存器来捕获任务输出。

如果您是第一次来这里,请查看本系列的前几章:

  • Ansible RHCE #1:介绍和安装
  • Ansible RHCE #1:运行临时命令
  • Ansible RHCE #1:理解剧本

第 1 部分:在 Ansible 中使用变量

让我们先从变量开始。 请记住,所有这些都写在您的 YML 文件中。

定义和引用变量

您可以使用 变量 关键字直接在剧本中定义变量。

为了 example你可以定义一个 最喜欢的颜色 变量并将其值设置为黄色,如下所示:

---
- name: Working with variables
  hosts: all
  vars:
    fav_color: yellow

现在你如何使用(参考) 最喜欢的颜色 多变的? Ansible 使用 Jinja2 模板系统 使用变量。 在本系列中将有一个专门讨论 Jinja2 的教程,但现在您只需要了解非常基础的知识。

为了获得价值 最喜欢的颜色 多变的; 您需要用一对花括号将其括起来,如下所示:

My favorite color is {{ fav_color }}

请注意,如果您的变量是行中的第一个元素(或唯一元素),则必须使用引号,如下所示:

"{{ fav_color }} is my favorite color."

现在让我们编写一个名为 变量-playbook.yml 这将所有这些放在一起:

[[email protected] plays]$ cat variables-playbook.yml 
---
- name: Working with variables
  hosts: node1
  vars:
    fav_color: yellow
  tasks:
    - name: Show me fav_color value
      debug:
        msg: My favorite color is {{ fav_color }}.

我用过 调试 模块连同 味精 模块选项打印的值 最喜欢的颜色 多变的。

现在运行 playbook,您将看到您喜欢的颜色显示如下:

[[email protected] plays]$ ansible-playbook variables-playbook.yml 

PLAY [Working with variables] **************************************************

TASK [Gathering Facts] *********************************************************
ok: [node1]

TASK [Show me fav_color value] *************************************************
ok: [node1] => {
    "msg": "My favorite color is yellow."
}

PLAY RECAP *********************************************************************
node1                      : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

创建列表和字典

您还可以使用列表和字典来定义多值变量。 为了 example,您可以定义一个名为 端口号 并将其值设置如下:

  vars:
    port_nums: [21,22,23,25,80,443]

您也可以使用以下方式来定义 端口号 这是等效的:

vars:
    port_nums:
      - 21
      - 22
      - 23
      - 25
      - 80
      - 443

您可以打印中的所有值 端口号 如下:

All the ports {{ port_nums }}

您还可以访问特定的列表元素:

First port is {{ port_nums[0] }}

请注意,您使用索引(位置)来访问列表元素。

您还可以定义一个名为 用户 如下:

vars:
    users:
      bob:
        username: bob
        uid: 1122
        shell: /bin/bash
      lisa:
        username: lisa
        uid: 2233
        shell: /bin/sh

您可以使用两种不同的方式来访问字典元素:

  • dict_name[‘key’] -> 用户[‘bob’][‘shell’]
  • dict_name.key -> users.bob.shell

请注意,您使用键来访问字典元素。

现在您可以编辑 变量-playbook.yml 显示列表和字典的剧本:

[[email protected] plays]$ cat variables-playbook.yml 
---
- name: Working with variables
  hosts: node1
  vars:
    port_nums: [21,22,23,25,80,443]
 
    users: 
      bob:
        username: bob
        uid: 1122
        shell: /bin/bash
      lisa:
        username: lisa
        uid: 2233
        shell: /bin/sh
  tasks:
    - name: Show 2nd item in port_nums
      debug:
        msg: SSH port is {{ port_nums[1] }}

    - name: Show the uid of bob
      debug:
        msg: UID of bob is {{ users.bob.uid }}

您现在可以运行 playbook 以显示第二个元素 端口号 并显示鲍勃的 uid:

[[email protected] plays]$ ansible-playbook variables-playbook.yml 

PLAY [Working with variables] **************************************************

TASK [Show 2nd item in port_nums] **********************************************
ok: [node1] => {
    "msg": "SSH port is 22"
}

TASK [Show the uid of bob] *****************************************************
ok: [node1] => {
    "msg": "UID of bob is 1122"
}

包括外部变量

就像您可以在剧本中导入(或包含)任务一样。 你也可以对变量做同样的事情。 也就是说,在剧本中,您可以包含在外部文件中定义的变量。

为了演示,让我们创建一个名为 myvars.yml 包含我们的 端口号 列出和 用户 字典:

[[email protected] plays]$ cat myvars.yml 
---
port_nums: [21,22,23,25,80,443]

users:
  bob:
    username: bob
    uid: 1122
    shell: /bin/bash
  lisa:
    username: lisa
    uid: 2233
    shell: /bin/sh

阅读全文

本文的其余部分仅供 LHB 会员使用。 您现在可以免费注册以阅读本文的其余部分以及访问所有仅限会员的帖子。 您还可以订阅我们的双周 Linux 通讯。

订阅

已经有一个帐户?
登入