Ansible 2.8:如何仅在内核更新后重新启动机器?计算数组变量

Ansible 2.8:如何仅在内核更新后重新启动机器?计算数组变量

我正在尝试使用 Ansible Playbook 更新我们的 RHEL7 机器,如果内核已更新,则会自动重新启动机器。

这就是我想出的:

---
- hosts:  server1
  become: yes
  tasks:
    - name: YUM Update
      yum:
        name:  "*"
        state: latest
      register: yum_updates

    - debug:
        var: yum_updates.changes.updated

    - name: Reboot server1
      reboot:
      when: '"kernel" in yum_updates.changes.updated'
...

当然,到目前为止更新有效,但我找不到一种按照我需要的方式评估输出变量的方法。这就是输出:

PLAY [server1] *********************************************************************************************************************************************************************************

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

TASK [YUM Update] **************************************************************************************************************************************************************************************
changed: [server1]

TASK [debug] *******************************************************************************************************************************************************************************************
ok: [de-agb1-smba01t] => {
    "yum_updates.changes.updated": [
        [
            "kernel-headers", 
            "3.10.0-1062.4.3.el7.x86_64 from rhel-7-server-rpms"
        ], 
        [
            "kernel", 
            "3.10.0-1062.4.3.el7.x86_64 from rhel-7-server-rpms"
        ], 
        [
            "sudo", 
            "1.8.23-4.el7_7.1.x86_64 from rhel-7-server-rpms"
        ], 
        [
            "bpftool", 
            "3.10.0-1062.4.3.el7.x86_64 from rhel-7-server-rpms"
        ], 
        [
            "python-perf", 
            "3.10.0-1062.4.3.el7.x86_64 from rhel-7-server-rpms"
        ], 
        [
            "kernel-tools-libs", 
            "3.10.0-1062.4.3.el7.x86_64 from rhel-7-server-rpms"
        ], 
        [
            "kernel-devel", 
            "3.10.0-1062.4.3.el7.x86_64 from rhel-7-server-rpms"
        ], 
        [
            "microcode_ctl", 
            "2:2.1-53.3.el7_7.x86_64 from rhel-7-server-rpms"
        ], 
        [
            "SDL", 
            "1.2.15-14.el7_7.x86_64 from rhel-7-server-rpms"
        ], 
        [
            "kernel-tools", 
            "3.10.0-1062.4.3.el7.x86_64 from rhel-7-server-rpms"
        ], 
        [
            "perf", 
            "3.10.0-1062.4.3.el7.x86_64 from rhel-7-server-rpms"
        ]
    ]
}

TASK [Reboot server1] **************************************************************************************************************************************************************************************
skipping: [server1]

PLAY RECAP *********************************************************************************************************************************************************************************************
server1            : ok=3    changed=1    unreachable=0    failed=0    skipped=1    rescued=0    ignored=0   

当条件看起来如何时,它会找到单词“kernel”?

任何想法?

答案1

使用展平过滤器,如接受的答案所示https://unix.stackexchange.com/a/554727/307127将包名称和版本混合到一个列表中。尽管这不太可能引起任何问题,但它并没有达到应有的干净程度。

相反,您可以将重新启动的测试编写为
when: dict((yum_updates.changes|default(dict())).updated|default([])).kernel is defined

这会将已更改的包和版本的列表转换为字典(以包名称作为键,版本作为值),然后测试字典是否包含键“kernel”。请注意,在两个地方使用“默认”过滤器来覆盖边缘情况,例如没有更新包时。这是必要的,因为当没有更新任何包时,注册变量中不存在“changes”键(即“yum_updates.changes”不存在)。

答案2

一个简单的解决方案是压扁列表中,选择项目匹配图案和测试如果有任何项目满足条件。

when: yum_updates.changes.updated|flatten|select('regex', '^kernel$') is any

答案3

您有一个列表 ( yum.updates.changes.updated),其中每一项都是两个项目的列表(软件包名称和版本)。仅匹配软件包的简单 when 条件kernel如下所示:

---
- debug:
    msg: "{{ item.0 }} was updated to version {{ item.1 }}"
  when: item.0 == "kernel"
  loop: "{{ yum_updates.changes.updated }}"

根据您的示例数据,此任务将输出:

TASK [debug] *****************************************************************************************************************************************************************
skipping: [localhost] => (item=['kernel-headers', '3.10.0-1062.4.3.el7.x86_64 from rhel-7-server-rpms'])
ok: [localhost] => (item=['kernel', '3.10.0-1062.4.3.el7.x86_64 from rhel-7-server-rpms']) => {
    "msg": "kernel was updated to version 3.10.0-1062.4.3.el7.x86_64 from rhel-7-server-rpms"
}
skipping: [localhost] => (item=['sudo', '1.8.23-4.el7_7.1.x86_64 from rhel-7-server-rpms'])
skipping: [localhost] => (item=['bpftool', '3.10.0-1062.4.3.el7.x86_64 from rhel-7-server-rpms'])
skipping: [localhost] => (item=['python-perf', '3.10.0-1062.4.3.el7.x86_64 from rhel-7-server-rpms'])
skipping: [localhost] => (item=['kernel-tools-libs', '3.10.0-1062.4.3.el7.x86_64 from rhel-7-server-rpms'])
skipping: [localhost] => (item=['kernel-devel', '3.10.0-1062.4.3.el7.x86_64 from rhel-7-server-rpms'])
skipping: [localhost] => (item=['microcode_ctl', '2:2.1-53.3.el7_7.x86_64 from rhel-7-server-rpms'])
skipping: [localhost] => (item=['SDL', '1.2.15-14.el7_7.x86_64 from rhel-7-server-rpms'])
skipping: [localhost] => (item=['kernel-tools', '3.10.0-1062.4.3.el7.x86_64 from rhel-7-server-rpms'])
skipping: [localhost] => (item=['perf', '3.10.0-1062.4.3.el7.x86_64 from rhel-7-server-rpms'])

相关内容