Access ansible facts from command line

Hi all,

I am wondering how I can access ansible_fatcs from the command line as ad-hoc commands.

I tried this command below in vain.

ansible node00 -i inventory -m debug -a 'var=ansible_eth0.ipv4.address'

it always gives me

node00 | SUCCESS => {
    "ansible_eth0.ipv4.address": "VARIABLE IS NOT DEFINED!"
}

I tried this as well

ansible node00 -i inventory -m debug -a 'var=ansible_facts.ansible_eth0.ipv4.address'

But I got the same error

Hello Ahmed,
you can try

- hosts: node00
  tasks:
    - debug: var=ansible_default_ipv4.address

Thanks,
KodeKloud Support

Thank you anyway for the response, but this is not what I wanted to do, it is pretty clear this will work in a playbook whereas gathering_facts is set to true

What I can do instead as a workaround:

ansible all -i inventory -m setup -a 'filter=ansible_eth1'
ansible all -i inventory -m setup -a 'filter=*eth1*'

Good luck

Happy learning, @Ahmed!

1 Like