0

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?

U880D
  • 8,601
  • 6
  • 24
  • 40
Tony
  • 35
  • 4
  • Does [Variable is undefined when running Ansible `debug` ad-hoc](https://stackoverflow.com/a/74597905/6771046) answer your question? – U880D May 17 '23 at 04:27
  • Thank you for your answer, I read the link you sent, it seems to say that adhoc will not automatically collect facts, playbook can achieve what I want, but sometimes 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, so I am thinking that since discovered_interpreter_python can be used as a common return value, then fqdn should also be possible – Tony May 17 '23 at 06:01
  • "_adhoc will not automatically collect facts_", correct, that is the case. "_I am thinking that since discovered_interpreter_python can be used as a common return value, then fqdn should also be possible_", unfortunately not in that way. – U880D May 17 '23 at 06:03
  • Yeah, not everything goes my way – Tony May 17 '23 at 07:25

1 Answers1

0

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

U880D
  • 8,601
  • 6
  • 24
  • 40