我正在尝试根据条件在一组主机上安装软件。执行播放后,我想计算安装发生了多少主机以及跳过了多少主机。
在调用下面的任务之前,我尝试在 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"
}
有人可以帮助我使变量表现得像全局变量,以便我可以获得实际计数吗?