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

Only check whether a line present in a file (ansible)

Use check_mode, register and failed_when in concert. This fails the task if the lineinfile module would make any changes to the file being checked. Check_mode ensures nothing will change even if it otherwise would. – name: “Ensure /tmp/my.conf contains ‘127.0.0.1’” lineinfile: name: /tmp/my.conf line: “127.0.0.1” state: present check_mode: yes register: conf failed_when: (conf is changed) … Read more

How to set Linux environment variables with Ansible

There are multiple ways to do this and from your question it’s nor clear what you need. 1. If you need environment variable to be defined PER TASK ONLY, you do this: – hosts: dev tasks: – name: Echo my_env_var shell: “echo $MY_ENV_VARIABLE” environment: MY_ENV_VARIABLE: whatever_value – name: Echo my_env_var again shell: “echo $MY_ENV_VARIABLE” Note … Read more

Using set_facts and with_items together in Ansible

There is a workaround which may help. You may “register” results for each set_fact iteration and then map that results to list: — – hosts: localhost tasks: – name: set fact set_fact: foo_item=”{{ item }}” with_items: – four – five – six register: foo_result – name: make a list set_fact: foo=”{{ foo_result.results | map(attribute=”ansible_facts.foo_item”) | … Read more

How to copy files between two nodes using ansible

As ant31 already pointed out you can use the synchronize module to this. By default, the module transfers files between the control machine and the current remote host (inventory_host), however that can be changed using the task’s delegate_to parameter (it’s important to note that this is a parameter of the task, not of the module). … Read more

How can I use Ansible nested variable?

Per Ansible FAQ: Another rule is ‘moustaches don’t stack’. We often see this: {{ somevar_{{other_var}} }} The above DOES NOT WORK, if you need to use a dynamic variable use the hostvars or vars dictionary as appropriate: {{ hostvars[inventory_hostname][‘somevar_’ + other_var] }} So in your case: – debug: msg={{hostvars[inventory_hostname][Component].community_release_num}} Or: – debug: msg={{vars[Component].community_release_num}} Or (since … Read more