英文:
How to add specific facts to the content returned by Ansible ad-hoc?
问题
在 Ansible ad-hoc 命令返回的 JSON 数据中,我注意到在 {'event_data':'res':'ansible_facts':{'discovered_interpreter_python': '/usr/bin/python'}}
中始终存在 ansible_facts
变量 discovered_interpreter_python
。似乎无论执行哪个模块,其输出都会包含这个 ansible_facts
变量 discovered_interpreter_python
。
我希望能够添加一些其他变量,比如 ansible_fqdn
,而无需进行额外操作。无论我执行 ad-hoc 命令还是 playbook,是否有可能实现?
英文:
In the JSON data returned by Ansible ad-hoc command, I noticed that there is ansible_facts
in {'event_data':'res':'ansible_facts':{'discovered_interpreter_python': '/usr/bin/python'}}
. It seems that no matter what module is executed, its output will be fixed with the ansible_facts
variable discovered_interpreter_python
.
I hope to add some other variables, such as ansible_fqdn
, without requiring me to do extra operations.
Whether I execute ad-hoc or playbook, is it possible?
答案1
得分: 0
... without requiring me to do extra operations. Whether I execute ad-hoc or playbook, is it possible?
不可能,根据要求的写法。
How to add some other variables, such as
ansible_fqdn
?
通过ansible-playbook
,根据配置Ansible事实会自动收集,仅通过ansible
和临时命令不会。
要做到这一点,您需要执行setup
模块,结果如下:
ansible test -m setup -a 'filter=ansible_fqdn'
test.example.com | SUCCESS => {
"ansible_facts": {
"ansible_fqdn": "test.example.com",
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false
}
How to add some other variables, such as
ansible_fqdn
... whether I execute ... playbook
一个最简单的示例剧本
---
- hosts: localhost
become: false
gather_facts: true
gather_subset:
- "!all"
- "!min"
- "network"
tasks:
- name: Show Facts
debug:
msg: "{{ ansible_facts.fqdn }}"
将产生以下输出
任务 [Show Facts] *****
ok: [localhost] =>
msg: test.example.com
关于您的评论
我只需要简单的命令执行,不需要使用playbook,但我希望除了IP之外,还可以获得主机的名称...
通过临时命令可以实现的是链式执行命令并以简单方式格式化输出。
ansible test --module-name shell --args "hostname; date; echo ' ';"
test1.example.com | CHANGED | rc=0 >>
test1.example.com
Wed May 17 08:12:01 CEST 2023
test2.example.com | CHANGED | rc=0 >>
test2.example.com
Wed May 17 08:12:02 CEST 2023
英文:
> ... without requiring me to do extra operations. Whether I execute ad-hoc or playbook, is it possible?
No, not as the requirement is written.
> How to add some other variables, such as ansible_fqdn
?
Whereby via ansible-playbook
and depending on the configuration Ansible facts become gathered automatically, via ansible
only and ad-hoc command not.
To do so you would need to execute the setup
module instead, resulting into an output of
ansible test -m setup -a 'filter=ansible_fqdn'
test.example.com | SUCCESS => {
"ansible_facts": {
"ansible_fqdn": "test.example.com",
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false
}
Similar Q&A
> How to add some other variables, such as ansible_fqdn
... whether I execute ... playbook
A minimal example playbook
---
- hosts: localhost
become: false
gather_facts: true
gather_subset:
- "!all"
- "!min"
- "network"
tasks:
- name: Show Facts
debug:
msg: "{{ ansible_facts.fqdn }}"
will result into an output of
TASK [Show Facts] *****
ok: [localhost] =>
msg: test.example.com
Further Q&A
- What is the exact list of Ansible
setup
min
? - Is it possible to gather only specific facts in Ansible?
Regarding your comment
> I just need a simple command execution, do not need to use playbook, but I It is hoped that the name of the host can be obtained in addition to the IP ...
What is possible with ad-hoc commands is to execute commands in chain and format the output in a simple way.
ansible test --module-name shell --args "hostname; date; echo ' ';"
test1.example.com | CHANGED | rc=0 >>
test1.example.com
Wed May 17 08:12:01 CEST 2023
test2.example.com | CHANGED | rc=0 >>
test2.example.com
Wed May 17 08:12:02 CEST 2023
If it is just the FQDN of the Remonte Node, it could be included within the command.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论