Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Register variables in with_items loop in Ansible playbook

I've got a dictionary with different names like

vars:     images:       - foo       - bar 

Now, I want to checkout repositories and afterwards build docker images only when the source has changed. Since getting the source and building the image is the same for all items except the name I created the tasks with with_items: images and try to register the result with:

register: "{{ item }}" 

and also tried

register: "src_{{ item }}" 

Then I tried the following condition

when: "{{ item }}|changed" 

and

when: "{{ src_item }}|changed" 

This always results in fatal: [piggy] => |changed expects a dictionary

So how can I properly save the results of the operations in variable names based on the list I iterate over?

Update: I would like to have something like that:

- hosts: all   vars:     images:       - foo       - bar   tasks:     - name: get src       git:         repo: [email protected]/repo.git         dest: /tmp/repo       register: "{{ item }}_src"       with_items: images      - name: build image       shell: "docker build -t repo ."       args:         chdir: /tmp/repo       when: "{{ item }}_src"|changed       register: "{{ item }}_image"       with_items: images      - name: push image       shell: "docker push repo"       when: "{{ item }}_image"|changed       with_items: images 
like image 407
soupdiver Avatar asked Apr 08 '15 10:04

soupdiver


People also ask

How do I register variables in Ansible?

Ansible registers are used when you want to capture the output of a task to a variable. You can then use the value of these registers for different scenarios like a conditional statement, logging etc. The variables will contain the value returned by the task. The common return values are documented in Ansible docs.

How do I register multiple variables in Ansible?

No, this is not possible. Ansible (as of current version 2.4) does not allow to register partial output of the module (or several different parts of it). You can register only full result and extract parts of it in the tasks to follow.

How do you pass variable value while running Ansible playbook?

To pass a value to nodes, use the --extra-vars or -e option while running the Ansible playbook, as seen below. This ensures you avoid accidental running of the playbook against hardcoded hosts.

What is the difference between With_items and loop in Ansible?

The with_ keywords rely on Lookup Plugins - even items is a lookup. The loop keyword is equivalent to with_list, and is the best choice for simple loops. The loop keyword will not accept a string as input, see Ensuring list input for loop: query vs. lookup.


1 Answers

So how can I properly save the results of the operations in variable names based on the list I iterate over?

You don't need to. Variables registered for a task that has with_items have different format, they contain results for all items.

- hosts: localhost   gather_facts: no   vars:     images:       - foo       - bar   tasks:     - shell: "echo result-{{item}}"       register: "r"       with_items: "{{ images }}"      - debug: var=r      - debug: msg="item.item={{item.item}}, item.stdout={{item.stdout}}, item.changed={{item.changed}}"       with_items: "{{r.results}}"      - debug: msg="Gets printed only if this item changed - {{item}}"       when: item.changed == true       with_items: "{{r.results}}"  
like image 160
Kashyap Avatar answered Sep 21 '22 17:09

Kashyap