# setting variable in a playbook from a jinjia2 snippet

**URL:** <https://forum.ansible.com/t/setting-variable-in-a-playbook-from-a-jinjia2-snippet/29742>\
**Category:** Ansible Project\
**Created:** [February 22, 2019, 11:42am UTC](https://forum.ansible.com/t/setting-variable-in-a-playbook-from-a-jinjia2-snippet/29742 "2019-02-22T11:42:29Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Luca\_Cazzaniga](https://avatars.discourse-cdn.com/v4/letter/l/977dab/32.png) [@Luca\_Cazzaniga](https://forum.ansible.com/u/Luca_Cazzaniga)\
**Post date:** [February 22, 2019, 11:42am UTC](https://forum.ansible.com/t/setting-variable-in-a-playbook-from-a-jinjia2-snippet/29742/1 "2019-02-22T11:42:29Z")

</div>

Hi all is it possible assign variable built from jingia2 fragment to other variable in the playbook:

- name: “list files in an archive”  
unarchive:  
src: /root/mybundle.zip  
dest: /opt  
list\_files: yes  
remote\_src: yes  
register: archive\_content  
loop:

- /root/mybundle.zip

- /root/mybudle2.zip

- name: “debug archive\_content”  
debug:  
var: “{{ path }}”  
vars:  
path: "{% set output = %}  
{% for file in item.files %}  
{% if file | regex\_search(‘.rpm$’) %}  
{{ output.append(item.dest ~ ‘/’ ~ file) }}  
{% endif %}  
{% endfor %}\

{% output %}"

I got the following error

fatal: [server2]: FAILED! =\> {“msg”: “template error while templating string: Encountered unknown tag ‘output’… String: {% set output = %}{% for file in item.files %}{% if file | regex\_search(‘.rpm$’) %} {{ output.append(item.dest ~ ‘/’ ~ file) }}{% endif %} {% endfor %}{% output %}”}

regards

Luca

---

<div class="post-metadata">

**Author:** ![Luca\_Cazzaniga](https://avatars.discourse-cdn.com/v4/letter/l/977dab/32.png) [@Luca\_Cazzaniga](https://forum.ansible.com/u/Luca_Cazzaniga)\
**Post date:** [February 22, 2019, 11:44am UTC](https://forum.ansible.com/t/setting-variable-in-a-playbook-from-a-jinjia2-snippet/29742/2 "2019-02-22T11:44:55Z")

</div>

Sorry for the stupid question

solved with

- name: “debug archive\_content”  
debug:  
var: path  
vars:  
path: “{% set output = %}  
{% for file in item.files %}  
{% if file | regex\_search(‘.rpm$’) %}  
{{ output.append(item.dest ~ ‘/’ ~ file) }}  
{% endif %}  
{% endfor %}  
{{ output }}”  
loop: “{{ archive\_content.results|list }}”

Luca

---

<div class="post-metadata">

**Author:** ![JonTheNiceGuy](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jontheniceguy/32/2267_2.png) [@JonTheNiceGuy](https://forum.ansible.com/u/JonTheNiceGuy)\
**Post date:** [February 25, 2019, 12:01pm UTC](https://forum.ansible.com/t/setting-variable-in-a-playbook-from-a-jinjia2-snippet/29742/3 "2019-02-25T12:01:51Z")

</div>

Also, look at the set\_fact module, I use this extensively!

---

<div class="post-metadata">

**Author:** ![Luca\_Cazzaniga](https://avatars.discourse-cdn.com/v4/letter/l/977dab/32.png) [@Luca\_Cazzaniga](https://forum.ansible.com/u/Luca_Cazzaniga)\
**Post date:** [February 25, 2019, 9:05pm UTC](https://forum.ansible.com/t/setting-variable-in-a-playbook-from-a-jinjia2-snippet/29742/4 "2019-02-25T21:05:40Z")

</div>

Thanks for the tip Jon.

So if the variable was necessary in subsequent tasks I could use set\_fact module rather than defining a variable in the scope of the task or play

do you mean something like…

```auto
 - name: "list files in an archive"
    unarchive:
      src: /root/mybundle.zip
      dest: /opt
      list_files: yes
      remote_src: yes
    register: archive_content
    loop:
      - /root/mybundle.zip
      - /root/mybudle2.zip

```

- name: “adapts structure”  
set\_fact:  
path: "{% if output is undefined %}\

{% set output = %}  
{% endif %}\

{% for file in item.files %}  
{% if file | regex\_search(‘.rpm$’) %}  
{{ output.append(item.dest ~ ‘/’ ~ file) }}  
{% endif %}  
{% endfor %}\

{% output %}"  
loop: “{{ archive\_content.results|list }}”

- debug:

var: path

Unfortunately in this moment I can’t test the playbook…

Regards

Luca

```auto

```

---

<div class="post-metadata">

**Author:** ![JonTheNiceGuy](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jontheniceguy/32/2267_2.png) [@JonTheNiceGuy](https://forum.ansible.com/u/JonTheNiceGuy)\
**Post date:** [February 26, 2019, 3:46pm UTC](https://forum.ansible.com/t/setting-variable-in-a-playbook-from-a-jinjia2-snippet/29742/5 "2019-02-26T15:46:35Z")

</div>

Looks right to me 🙂

Like you, however, I don’t have a way to test it right now!
