Ansible tip - finding the server size from Dynamic Inventory variables


The tip in this post assumes you are working with Ansible AWS auto-discovery See the documentation here


In the case you would like to “discover” what is the size of the server that ansible is working on in the playbook, and accordingly set a variable you should do the following:

Add to your tasks in the role a set_fact

- name: set_fact ec2 server type
    ec2_server_type: "{{ vars.hostvars[ansible_host].ec2_instance_type | replace('.','_') }}"

The above finds the current ansible_host’s ec2_instance_type

Note the replace at the end

This comes to replace the dot with an underscore for better searching of dictionary variables - see next post