Ansible 总和寄存器值
Ansible sum register value
如何使用 Jinja2 过滤 ansible 获取两个主机的总和
主机 1 和主机 2
---
- name: Count Check
hosts: MYGROUP
gather_facts: true
user: sv_admin
tasks:
- name: count check
shell: cat /etc/hosts | wc -l
register: command_result
- debug:
var: command_result.stdout
- set_fact:
total_result: "{{ command_result.stdout | map('int') | sum(start=0) }}"
- debug:
msg: "Total count: {{ total_result }}"
剧本输出
TASK [debug] *****************************************************************
ok: [Host-01] => {
"msg": "Total count: 134"
}
ok: [Host-02] => {
"msg": "Total count: 133"
}
您可以使用自定义统计信息来做到这一点:https://docs.ansible.com/ansible/latest/modules/set_stats_module.html
所以对于你的情况,它看起来像
---
- name: Count Check
hosts: MYGROUP
gather_facts: true
user: sv_admin
tasks:
- name: count check
shell: cat /etc/hosts | wc -l
register: command_result
- debug:
var: command_result.stdout
- set_fact:
host_result: "{{ command_result.stdout }}"
- debug:
msg: "Count for this host: {{ host_result }}"
- set_stats:
data: "{{ { 'total_count': host_result | int } }}"
然后如果你 运行 它与 ANSIBLE_SHOW_CUSTOM_STATS=yes
它会在最后显示结果:
$ ANSIBLE_SHOW_CUSTOM_STATS=yes ansible-playbook -i inventory pb.yml
... (usual output)
CUSTOM STATS: *************************************************************
RUN: { "total_count": 267}
set_stats
任务默认将所有主机的结果加在一起,这就是您要查找的内容。你需要确保这些值是整数,因为如果它们是字符串,它只会将它们连接起来,你最终会得到类似 RUN: { "total_count": "134133"}
的结果。这就是为什么我按照我的方式放置 data:
位的原因 - 如果您尝试在常规 yaml 中创建字典,例如
data:
total_count: "{{ host_result | int }}"
你会看到该值仍然是一个字符串(由于 yaml/jinja 的工作方式)并且它不会正常工作。
shell> cat playbook.yml
- hosts: test_01:test_03
gather_facts: false
tasks:
- shell: cat /etc/hosts | wc -l
register: command_result
- debug:
var: command_result.stdout
- set_fact:
total_result: "{{ ansible_play_hosts_all|
map('extract', hostvars, ['command_result', 'stdout'])|
map('int')|
sum }}"
run_once: true
- debug:
var: total_result
给出(删节)
shell> ansible-playbook playbook.yml
PLAY [test_01:test_03] ****
TASK [shell] ****
changed: [test_01]
changed: [test_03]
TASK [debug] ****
ok: [test_01] => {
"command_result.stdout": " 62"
}
ok: [test_03] => {
"command_result.stdout": " 31"
}
TASK [set_fact] ****
ok: [test_01]
TASK [debug] ****
ok: [test_03] => {
"total_result": "93"
}
ok: [test_01] => {
"total_result": "93"
}
如何使用 Jinja2 过滤 ansible 获取两个主机的总和 主机 1 和主机 2
---
- name: Count Check
hosts: MYGROUP
gather_facts: true
user: sv_admin
tasks:
- name: count check
shell: cat /etc/hosts | wc -l
register: command_result
- debug:
var: command_result.stdout
- set_fact:
total_result: "{{ command_result.stdout | map('int') | sum(start=0) }}"
- debug:
msg: "Total count: {{ total_result }}"
剧本输出
TASK [debug] *****************************************************************
ok: [Host-01] => {
"msg": "Total count: 134"
}
ok: [Host-02] => {
"msg": "Total count: 133"
}
您可以使用自定义统计信息来做到这一点:https://docs.ansible.com/ansible/latest/modules/set_stats_module.html
所以对于你的情况,它看起来像
---
- name: Count Check
hosts: MYGROUP
gather_facts: true
user: sv_admin
tasks:
- name: count check
shell: cat /etc/hosts | wc -l
register: command_result
- debug:
var: command_result.stdout
- set_fact:
host_result: "{{ command_result.stdout }}"
- debug:
msg: "Count for this host: {{ host_result }}"
- set_stats:
data: "{{ { 'total_count': host_result | int } }}"
然后如果你 运行 它与 ANSIBLE_SHOW_CUSTOM_STATS=yes
它会在最后显示结果:
$ ANSIBLE_SHOW_CUSTOM_STATS=yes ansible-playbook -i inventory pb.yml
... (usual output)
CUSTOM STATS: *************************************************************
RUN: { "total_count": 267}
set_stats
任务默认将所有主机的结果加在一起,这就是您要查找的内容。你需要确保这些值是整数,因为如果它们是字符串,它只会将它们连接起来,你最终会得到类似 RUN: { "total_count": "134133"}
的结果。这就是为什么我按照我的方式放置 data:
位的原因 - 如果您尝试在常规 yaml 中创建字典,例如
data:
total_count: "{{ host_result | int }}"
你会看到该值仍然是一个字符串(由于 yaml/jinja 的工作方式)并且它不会正常工作。
shell> cat playbook.yml
- hosts: test_01:test_03
gather_facts: false
tasks:
- shell: cat /etc/hosts | wc -l
register: command_result
- debug:
var: command_result.stdout
- set_fact:
total_result: "{{ ansible_play_hosts_all|
map('extract', hostvars, ['command_result', 'stdout'])|
map('int')|
sum }}"
run_once: true
- debug:
var: total_result
给出(删节)
shell> ansible-playbook playbook.yml
PLAY [test_01:test_03] ****
TASK [shell] ****
changed: [test_01]
changed: [test_03]
TASK [debug] ****
ok: [test_01] => {
"command_result.stdout": " 62"
}
ok: [test_03] => {
"command_result.stdout": " 31"
}
TASK [set_fact] ****
ok: [test_01]
TASK [debug] ****
ok: [test_03] => {
"total_result": "93"
}
ok: [test_01] => {
"total_result": "93"
}