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 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 stdout Formatting

Try this option. You’ll love it. There’s a new YAML callback plugin introduced with Ansible 2.5 — meaning any machine running Ansible 2.5.0 or later can automatically start using this format without installing custom plugins. To use it, edit your ansible.cfg file (either globally, in /etc/ansible/ansible.cfg, or locally in your playbook/project), and add the following … Read more

How to loop over this dictionary in Ansible?

Hows this – hosts: localhost vars: war_files: server1: – file1.war – file2.war server2: – file1.war – file2.war – file3.war tasks: – name: Loop over subelements of the dictionary debug: msg: “Key={{ item.0.key }} value={{ item.1 }}” loop: “{{ war_files | dict2items | subelements(‘value’) }}” dict2items, subelements filters are coming in Ansible 2.6. FYI, if a … Read more