List all variables only defined in hosts file corresponding to a host

18 views
Skip to first unread message

utkarsh srivastava

unread,
Jun 11, 2020, 1:09:40 AM6/11/20
to Ansible Project
Hi 

I want to list all variable and it's value defined in hosts file corresponding to a host
for example:

[hostgroup1]
host1     variable1=value1   variable2=value2  variable3=value3

so i want to list all variables defined 
like
variable1: value1
variable2: value2
variable3: value3

I am using 

- debug:
           msg: "{{ hostvars[inventory_hostname] }}"

but in this case, On console output i am getting predefined variables as well related to that host along with manual defined variables


Any help would be appreciated

Thanks in advance. 

Dick Visser

unread,
Jun 11, 2020, 1:36:22 AM6/11/20
to ansible...@googlegroups.com
I don't think you can filter host vars based on where they're set 

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/3ba35e6c-1477-44e9-b9ba-d7e5a8fc32e1o%40googlegroups.com.
--
Sent from a mobile device - please excuse the brevity, spelling and punctuation.

Vladimir Botka

unread,
Jun 11, 2020, 3:23:43 AM6/11/20
to utkarsh srivastava, ansible...@googlegroups.com
On Wed, 10 Jun 2020 22:09:40 -0700 (PDT)
utkarsh srivastava <utkarsh.sr...@gmail.com> wrote:

> I want to list all variable and it's value defined in hosts file
> corresponding to a host
> [hostgroup1]
> host1 variable1=value1 variable2=value2 variable3=value3

You might improve the situation with name-spacing of the variables, e.g
create unique prefix which can be used to filter variables.

But, this is not solving your problem. IMHO, it's not possible to tell from
which precedence's level variable comes from
https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html#variable-precedence-where-should-i-put-a-variable

Instead, if really needed, as a hint, it might be possible to select variables
defined by user. For example, as a first step, remove ansible_facts_* from
hostvars

- hosts: all
tasks:
- set_fact:
my_facts_keys: "{{ ansible_facts.keys()|
map('regex_replace', my_regex, my_replace)|
list }}"
vars:
my_regex: '^(.*)$'
my_replace: 'ansible_\1'
- set_fact:
my_hostvar_keys: "{{ hostvars[inventory_hostname].keys()|
list }}"
- debug:
msg: "{{ my_hostvar_keys|
difference(my_facts_keys)|
sort }}"

gives

"msg": [
"ansible_check_mode",
"ansible_diff_mode",
"ansible_facts",
"ansible_forks",
"ansible_inventory_sources",
"ansible_local",
"ansible_perl_interpreter",
"ansible_playbook_python",
"ansible_python_interpreter",
"ansible_run_tags",
"ansible_skip_tags",
"ansible_verbosity",
"ansible_version",
"gather_subset",
"group_names",
"groups",
"inventory_dir",
"inventory_file",
"inventory_hostname",
"inventory_hostname_short",
"module_setup",
"my_facts_keys",
"omit",
"playbook_dir",
"var1",
"var2",
"var3"
]

This is a list of user-defined variables plus Ansible "special variables"
https://docs.ansible.com/ansible/latest/reference_appendices/special_variables.html#special-variables
If you want to proceed this way create and subtract the list of special
variables (I don't know how to automate this), and fine-tune the list
"ansible_facts, module_setup, ..." .

--
Vladimir Botka
Reply all
Reply to author
Forward
0 new messages