# Dynamic File/ Template populate.

**URL:** <https://forum.ansible.com/t/dynamic-file-template-populate/34437>\
**Category:** Ansible Project\
**Created:** [January 14, 2021, 2:39am UTC](https://forum.ansible.com/t/dynamic-file-template-populate/34437 "2021-01-14T02:39:22Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![ursa\_Brown](https://avatars.discourse-cdn.com/v4/letter/u/eb9ed0/32.png) [@ursa\_Brown](https://forum.ansible.com/u/ursa_Brown)\
**Post date:** [January 14, 2021, 2:39am UTC](https://forum.ansible.com/t/dynamic-file-template-populate/34437/1 "2021-01-14T02:39:22Z")

</div>



---

<div class="post-metadata">

**Author:** ![ursa\_Brown](https://avatars.discourse-cdn.com/v4/letter/u/eb9ed0/32.png) [@ursa\_Brown](https://forum.ansible.com/u/ursa_Brown)\
**Post date:** [January 14, 2021, 12:34pm UTC](https://forum.ansible.com/t/dynamic-file-template-populate/34437/2 "2021-01-14T12:34:05Z")

</div>

How can it continue the playbook and if disk2 is not there it will just put not define. and get all the values.

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [January 14, 2021, 4:54pm UTC](https://forum.ansible.com/t/dynamic-file-template-populate/34437/3 "2021-01-14T16:54:13Z")

</div>

> How can it continue the playbook and if disk2 is not there it will just put not define. and get all the values.
> 
> &nbsp;&nbsp;&nbsp;&nbsp;\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
> &nbsp;&nbsp;&nbsp;&nbsp;---  
> &nbsp;&nbsp;&nbsp;&nbsp;- hosts: all  
> &nbsp;&nbsp;&nbsp;&nbsp; name:  
> &nbsp;&nbsp;&nbsp;&nbsp; become: yes  
> &nbsp;&nbsp;&nbsp;&nbsp; tasks:
> 
> &nbsp;&nbsp;&nbsp;&nbsp; - name: Template  
> &nbsp;&nbsp;&nbsp;&nbsp; template:  
> &nbsp;&nbsp;&nbsp;&nbsp; src: disk.j2  
> &nbsp;&nbsp;&nbsp;&nbsp; dest: /tmp/mydisk  
> &nbsp;&nbsp;&nbsp;&nbsp; ignore\_error: True  
> &nbsp;&nbsp;&nbsp;&nbsp;\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
> &nbsp;&nbsp;&nbsp;&nbsp;Jinja Template  
> &nbsp;&nbsp;&nbsp;&nbsp;ARCH={{ ansible\_architecture }}  
> &nbsp;&nbsp;&nbsp;&nbsp;alias={{ ansible\_default\_ipv4.alias }}  
> &nbsp;&nbsp;&nbsp;&nbsp;DISK1={{ ansible\_devices.sda.partitions.sda1.size }}  
> &nbsp;&nbsp;&nbsp;&nbsp;DISK2={{ ansible\_devices.sdb.partitions.sdb1.size }}

Please try the following:

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ARCH={{ ansible\_architecture }}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;alias={{ ansible\_default\_ipv4.alias }}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DISK1={{ ansible\_devices.sda.partitions.sda1.size | default('Not Defined')}}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DISK2={{ ansible\_devices.sdb.partitions.sdb1.size | default('Not Defined')}}

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![ursa\_Brown](https://avatars.discourse-cdn.com/v4/letter/u/eb9ed0/32.png) [@ursa\_Brown](https://forum.ansible.com/u/ursa_Brown)\
**Post date:** [January 14, 2021, 5:20pm UTC](https://forum.ansible.com/t/dynamic-file-template-populate/34437/4 "2021-01-14T17:20:22Z")

</div>

Same error, it will still look for the sdb…

fatal: [server1]: FAILED! =\> {“changed”: false, “msg”: “AnsibleError: template error while templating string: unexpected ‘}’, expected ‘)’. String: ARCH={{ ansible\_architecture }}\nalias={{ ansible\_default\_ipv4.alias }}\nDISK1={{ ansible\_devices.sda.partitions.sda1.size }}\nDISK2={{ ansible\_devices.sdb.partitions.sdb1.size | default(‘Not Defined’ }}\n\n”}

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [January 14, 2021, 5:27pm UTC](https://forum.ansible.com/t/dynamic-file-template-populate/34437/5 "2021-01-14T17:27:32Z")

</div>

> Same error, it will still look for the sdb..

No, different error, because you didn't copy/paste it correctly.  
Please read the error first, and try to understand what it means.

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [January 14, 2021, 5:27pm UTC](https://forum.ansible.com/t/dynamic-file-template-populate/34437/6 "2021-01-14T17:27:34Z")

</div>

> Same error, it will still look for the sdb..
> 
> fatal: [server1]: FAILED! =\> {"changed": false, "msg": "AnsibleError: template error while templating string: unexpected  
> '}', expected ')'. String: ARCH={{ ansible\_architecture }}\nalias={{ ansible\_default\_ipv4.alias }}\nDISK1={{  
> ansible\_devices.sda.partitions.sda1.size }}\nDISK2={{ ansible\_devices.sdb.partitions.sdb1.size | default('Not Defined'  
> }}\n\n"}

No, you did not copy my example correctly.

DISK2={{ ansible\_devices.sdb.partitions.sdb1.size | default('Not Defined' }}

The closing ) is missing in your template.

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![ursa\_Brown](https://avatars.discourse-cdn.com/v4/letter/u/eb9ed0/32.png) [@ursa\_Brown](https://forum.ansible.com/u/ursa_Brown)\
**Post date:** [January 14, 2021, 8:56pm UTC](https://forum.ansible.com/t/dynamic-file-template-populate/34437/7 "2021-01-14T20:56:23Z")

</div>

Please remember, some of the server has sdb define, some just sda. I still want my playbook to run, The playbook works well if there is sdb.

TASK [Template] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
fatal: [server1]: FAILED! =\> {“changed”: false, “msg”: “AnsibleUndefinedVariable: ‘dict object’ has no attribute ‘sdb’”}

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [January 14, 2021, 8:59pm UTC](https://forum.ansible.com/t/dynamic-file-template-populate/34437/8 "2021-01-14T20:59:08Z")

</div>

> Please remember, some of the server has sdb define, some just sda. I still want my playbook to run, The playbook works  
> well if there is sdb.
> 
> TASK [Template] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
> fatal: [server1]: FAILED! =\> {"changed": false, "msg": "AnsibleUndefinedVariable: 'dict object' has no attribute 'sdb'"}

I already gave you an example which covered both cases.

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![ursa\_Brown](https://avatars.discourse-cdn.com/v4/letter/u/eb9ed0/32.png) [@ursa\_Brown](https://forum.ansible.com/u/ursa_Brown)\
**Post date:** [January 14, 2021, 9:10pm UTC](https://forum.ansible.com/t/dynamic-file-template-populate/34437/9 "2021-01-14T21:10:26Z")

</div>

this is now my jinja, what example did you gave?? I might miss it…

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [January 15, 2021, 7:35am UTC](https://forum.ansible.com/t/dynamic-file-template-populate/34437/10 "2021-01-15T07:35:06Z")

</div>

> this is now my jinja, what example did you gave?? I might miss it..
> 
> ############################################################
> 
> DISK1={{ ansible\_devices.sda.partitions.sda1.size | default('Not Defined') }}  
> DISK2={{ ansible\_devices.sdb.partitions.sdb1.size | default('Not Defined') }}  
> ############################################################
> 
> And is still throwing error...  
> TASK [Template] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
> fatal: [server1]: FAILED! =\> {"changed": false, "msg": "AnsibleUndefinedVariable: 'dict object' has no attribute 'sdb'"}

It works for me with Ansible 2.9.16 / Jinja 2.9.16 / Python 3.9.1.

Alternatively you can use:

DISK2={% if 'sdb' in ansible\_devices %}{{ ansible\_devices.sdb.partitions.sdb1.size }}{% else %}Not Defined{% endif %}

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![ursa\_Brown](https://avatars.discourse-cdn.com/v4/letter/u/eb9ed0/32.png) [@ursa\_Brown](https://forum.ansible.com/u/ursa_Brown)\
**Post date:** [January 15, 2021, 12:13pm UTC](https://forum.ansible.com/t/dynamic-file-template-populate/34437/11 "2021-01-15T12:13:49Z")

</div>

THe if else statement work, another arsenal for me. Appreciated it… But

Below is the output:

---

<div class="post-metadata">

**Author:** ![ursa\_Brown](https://avatars.discourse-cdn.com/v4/letter/u/eb9ed0/32.png) [@ursa\_Brown](https://forum.ansible.com/u/ursa_Brown)\
**Post date:** [January 15, 2021, 12:23pm UTC](https://forum.ansible.com/t/dynamic-file-template-populate/34437/12 "2021-01-15T12:23:56Z")

</div>

YEs, the first solution worked when I upgrade my ansible to 2.9, will this work in 2.7? But the new peoblem still persist. i tried it in 2 disks…its not going to new line.

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [January 15, 2021, 1:05pm UTC](https://forum.ansible.com/t/dynamic-file-template-populate/34437/13 "2021-01-15T13:05:21Z")

</div>

> THe if else statement work, another arsenal for me. Appreciated it.. But
> 
> Below is the output:  
> ###########  
> ARCH=x86\_64  
> alias=ens192  
> DISK1=1.00 GBDISK2=Not Def  
> #####################
> 
> Was hoping  
> Disk1  
> Disk2

Adding an empty line between DISK1 and DISK2 in the template should fix that.

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke
