Accessing inventory host variable in Ansible playbook

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 … Read more

Ansible – Save registered variable to file

Thanks to tmoschou for adding this comment to an outdated accepted answer: As of Ansible 2.10, The documentation for ansible.builtin.copy says: If you need variable interpolation in copied files, use the ansible.builtin.template module. Using a variable in the content field will result in unpredictable output. For more details see this and an explanation Original answer: … Read more

Ansible remote_user vs ansible_user

They both seem to be the same. Take a look here: https://github.com/ansible/ansible/blob/c600ab81ee/lib/ansible/playbook/play_context.py#L46-L55 # the magic variable mapping dictionary below is used to translate # host/inventory variables to fields in the PlayContext # object. The dictionary values are tuples, to account for aliases # in variable names. MAGIC_VARIABLE_MAPPING = dict( connection = (‘ansible_connection’,), remote_addr = (‘ansible_ssh_host’, … Read more

Ansible with_subelements

This is really bad example of how subelements lookup works. (And has old, unsupported, syntax as well). Look at this one: — – hosts: localhost gather_facts: no vars: families: – surname: Smith children: – name: Mike age: 4 – name: Kate age: 7 – surname: Sanders children: – name: Pete age: 12 – name: Sara … Read more

How to set host_key_checking=false in ansible inventory file?

Due to the fact that I answered this in 2014, I have updated my answer to account for more recent versions of ansible. Yes, you can do it at the host/inventory level (Which became possible on newer ansible versions) or global level: inventory: Add the following. ansible_ssh_common_args=”-o StrictHostKeyChecking=no” host: Add the following. ansible_ssh_extra_args=”-o StrictHostKeyChecking=no” hosts/inventory … Read more