Accessing inventory host variable in Ansible playbook

前端 未结 6 1857
夕颜
夕颜 2020-12-03 00:52

I am using Ansible 2.1. I have the following inventory host file and a role being called by a play that needs access to the host file variable. Any thoughts on how to acce

6条回答
  •  难免孤独
    2020-12-03 01:16

    You are on the right track about hostvars.
    This magic variable is used to access information about other hosts.

    hostvars is a hash with inventory hostnames as keys.
    To access fields of each host, use hostvars['test-1'], hostvars['test2-1'], etc.

    ansible_ssh_host is deprecated in favor of ansible_host since 2.0.
    So you should first remove "_ssh" from inventory hosts arguments (i.e. to become "ansible_user", "ansible_host", and "ansible_port"), then in your role call it with:

    {{ hostvars['your_host_group'].ansible_host }}
    

提交回复
热议问题