如何使用 ad-hoc 命令过滤 nocache
block 或 free
block ?我尝试了 ansible centos1 -m setup -a 'filter=ansible_memory_mb.nocache'
但没有将其过滤掉。
ansible centos1 -m setup -a 'filter=ansible_memory_mb'
centos1 | SUCCESS => {
"ansible_facts": {
"ansible_memory_mb": {
"nocache": {
"free": 11808,
"used": 926
},
"real": {
"free": 10686,
"total": 12734,
"used": 2048
},
"swap": {
"cached": 0,
"free": 4096,
"total": 4096,
"used": 0
}
},
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false
}
最佳答案
如果你想尝试使用ansible命令,你必须与grep和head混合使用:
ansible centos -m setup -a 'filter=ansible_memory_mb' | grep -Eo [0-9]+ | head -1
但您应该使用 playbook:var 结果将包含所需的值。
- name: test
hosts: centos1
tasks:
- name: set vars
set_fact: result="{{ ansible_memory_mb.nocache.free}}"
- name: show
debug:
var: result
结果:
TASK [show] ***********************************************************************************************************************************************************************
ok: [localhost] => {
"result": "712"
}
关于使用 ad-hoc 命令的 Ansible 过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69594223/