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

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.

I don’t think you can filter host vars based on where they’re set

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