... 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
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.