1

我正在尝试根据条件在一组主机上安装软件。执行播放后,我想计算安装发生了多少主机以及跳过了多少主机。

在调用下面的任务之前,我尝试在 play 中声明一个变量,但是每个主机都会覆盖该变量,所以我无法获得实际计数:它始终为 1 或 0

玩:

- name: Install Junos OS
  hosts: reachable_a_hosts
  connection: local
  gather_facts: no
  roles:
    - Juniper.junos
  vars:
    credentials:
      host: "{{ loopback_v4 }}"
      username: "username"
    
      port: "{{ port }}"
      timeout: 60
  tasks:
  - name: Extra variable test
      debug:
        msg: "upgrade count {{ upgrade_count }}"
  - name: OS Install
    import_role:
      name: os_install
      tasks_from: change__os
  - name: debug
    debug:
      msg: "upgrade count {{ upgrade_count }}"

任务:

---
- name: Increment installation count
  set_fact:
    upgrade_count : ( {{ upgrade_count|int + 1|int }} )
  when: switch_os_version != OS_version

输出:

TASK [debug] *************************************
ok: [site1] => {
    "msg": "upgrade count (1)"
}
ok: [site2] => {
    "msg": "upgrade count (1)"
}

所以我将变量作为命令行使用-e. 现在无论条件是否满足,输出始终为0

更新的输出

TASK [os_install : Increment installation count] *********************
ok: [site1]
ok: [site2]


TASK [debug] *************************************
ok: [site1] => {
    "msg": "upgrade count 0"
}
ok: [site2] => {
    "msg": "upgrade count 0"
}

有人可以帮助我使变量表现得像全局变量,以便我可以获得实际计数吗?

4

2 回答 2

0

要解决此问题,您可以使用 ansible 寄存器,当您想将任务的输出捕获到变量时使用 Ansible 寄存器。然后,您可以将这些寄存器的值用于不同的场景,例如条件语句、日志记录等。示例:

---
- name: Increment installation count
  set_fact:
    upgrade_count : ( {{ upgrade_count|int + 1|int }} )
  register: upgrade_count # add this line
  when: switch_os_version != OS_version

和剧本:

- name: Install Junos OS
  hosts: reachable_a_hosts
  connection: local
  gather_facts: no
  roles:
    - Juniper.junos
  vars:
    credentials:
      host: "{{ loopback_v4 }}"
      username: "username"
    
      port: "{{ port }}"
      timeout: 60
  tasks:
  - name: Extra variable test
      debug:
        msg: "upgrade count {{ upgrade_count }}"
  - name: OS Install
    import_role:
      name: os_install
      tasks_from: change__os
  - name: debug
    debug:
      msg: "upgrade count {{ upgrade_count }}"

有关更多信息,请访问此示例

于 2021-06-25T13:21:10.563 回答
0

正如您所发现的那样,set_fact是.... 设置事实,即设置特定于播放循环中当前目标主机的变量的值。此外,正如我在评论中所报告的那样,额外的变量在播放过程中无法更改,因为它们具有最高级别的优先级。这些都不适合您尝试做的柜台。

同时,有几种方法可以实现您的要求。一个简单的国际海事组织。

  1. 选择一个您知道主机上不存在的 var。对于我将选择的示例_OS_upgraded
  2. 将此 var 设置为true在每台正在安装/升级的机器上,false过度。您可以删除when子句并直接设置测试结果。
    - name: Set info about upgrade.
      set_fact:
        _OS_upgraded: "{{ switch_os_version != OS_version }}"
    
  3. 完成后,您可以使用魔法变量 hostvars和一些过滤器 轻松计算主机数
    - name: Give some info about the tasks
      vars:
        processed_hosts: "{{ hostvars | dict2items | selectattr('value._OS_upgraded', 'defined') | list }}"
        num_host: "{{ processed_hosts | count }}"
        upgraded_hosts: "{{ processed_hosts | selectattr('value._OS_upgraded') | list | count }}"
        untouched_hosts: "{{ processed_hosts | rejectattr('value._OS_upgraded') | list | count }}"
        message: |-
          We processed {{ num_host }} hosts
          {{ upgraded_hosts }} were upgraded
          {{ untouched_hosts }} were left alone
      debug:
        msg: "{{ message.split('\n') }}"
    
于 2021-06-25T16:15:54.890 回答