Dynamic File/ Template populate.

62 views
Skip to first unread message

ursa Brown

unread,
Jan 13, 2021, 9:39:22 PM1/13/21
to Ansible Project
*******************************
---
- hosts: all
  name:
  become: yes
  tasks:

  - name: Template
    template:
       src: disk.j2
       dest: /tmp/mydisk
    ignore_error: True
*******************************
Jinja Template
ARCH={{ ansible_architecture }}
alias={{ ansible_default_ipv4.alias }}
DISK1={{ ansible_devices.sda.partitions.sda1.size }}
DISK2={{ ansible_devices.sdb.partitions.sdb1.size }}

Expected Output: If Disk1 and Disk 2 is available
############### 
cat /tmp/mydisk
ARCH=x86_64
alias=ens192
DISK1=1.00 GB
DISK2=762.00 MB
###############

Expected Output if Disk2 is not available.
############### 
cat /tmp/mydisk
ARCH=x86_64
alias=ens192
DISK1=1.00 GB
DISK2=Not Defined
###############
Reply all
Reply to author
Forward

ursa Brown

unread,
Jan 14, 2021, 7:34:05 AM1/14/21
to Ansible Project
How can it continue the playbook and if disk2 is not there it will just put not define. and get all the values.

Stefan Hornburg (Racke)

unread,
Jan 14, 2021, 11:54:42 AM1/14/21
to ansible...@googlegroups.com
On 1/14/21 1:34 PM, ursa Brown wrote:
> How can it continue the playbook and if disk2 is not there it will just put not define. and get all the values.
>
> On Wednesday, January 13, 2021 at 8:39:22 PM UTC-6 ursa Brown wrote:
>
> *******************************
> ---
> - hosts: all
>   name:
>   become: yes
>   tasks:
>
>   - name: Template
>     template:
>        src: disk.j2
>        dest: /tmp/mydisk
>     ignore_error: True
> *******************************
> Jinja Template
> ARCH={{ ansible_architecture }}
> alias={{ ansible_default_ipv4.alias }}
> DISK1={{ ansible_devices.sda.partitions.sda1.size }}
> DISK2={{ ansible_devices.sdb.partitions.sdb1.size }}

Please try the following:

ARCH={{ ansible_architecture }}
alias={{ ansible_default_ipv4.alias }}
DISK1={{ ansible_devices.sda.partitions.sda1.size | default('Not Defined')}}
DISK2={{ ansible_devices.sdb.partitions.sdb1.size | default('Not Defined')}}

Regards
Racke

>
> Expected Output: If Disk1 and Disk 2 is available
> ############### 
> cat /tmp/mydisk
> ARCH=x86_64
> alias=ens192
> DISK1=1.00 GB
> DISK2=762.00 MB
> ###############
>
> Expected Output if Disk2 is not available.
> ############### 
> cat /tmp/mydisk
> ARCH=x86_64
> alias=ens192
> DISK1=1.00 GB
> DISK2=Not Defined
> ###############
> Reply all
> Reply to author
> Forward
>
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to
> ansible-proje...@googlegroups.com <mailto:ansible-proje...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/57531664-ba31-49fc-b573-c20082fcabc6n%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/57531664-ba31-49fc-b573-c20082fcabc6n%40googlegroups.com?utm_medium=email&utm_source=footer>.


--
Ecommerce and Linux consulting + Perl and web application programming.
Debian and Sympa administration. Provisioning with Ansible.

OpenPGP_signature

ursa Brown

unread,
Jan 14, 2021, 12:20:23 PM1/14/21
to Ansible Project
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"}


Dick Visser

unread,
Jan 14, 2021, 12:27:59 PM1/14/21
to ansible...@googlegroups.com
On Thu, 14 Jan 2021 at 18:20, ursa Brown <ursab...@gmail.com> wrote:
>
> 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.
> To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/222abdaa-cffb-4fb6-b51e-5457e54c28a5n%40googlegroups.com.



--
Dick Visser
Trust & Identity Service Operations Manager
GÉANT

Stefan Hornburg (Racke)

unread,
Jan 14, 2021, 12:27:59 PM1/14/21
to ansible...@googlegroups.com
On 1/14/21 6:20 PM, ursa Brown wrote:
> 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
Racke
> <https://groups.google.com/d/msgid/ansible-project/57531664-ba31-49fc-b573-c20082fcabc6n%40googlegroups.com?utm_medium=email&utm_source=footer
> <https://groups.google.com/d/msgid/ansible-project/57531664-ba31-49fc-b573-c20082fcabc6n%40googlegroups.com?utm_medium=email&utm_source=footer>>.
>
>
>
> --
> Ecommerce and Linux consulting + Perl and web application programming.
> Debian and Sympa administration. Provisioning with Ansible.
>
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to
> ansible-proje...@googlegroups.com <mailto:ansible-proje...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/222abdaa-cffb-4fb6-b51e-5457e54c28a5n%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/222abdaa-cffb-4fb6-b51e-5457e54c28a5n%40googlegroups.com?utm_medium=email&utm_source=footer>.
OpenPGP_signature

ursa Brown

unread,
Jan 14, 2021, 3:56:23 PM1/14/21
to Ansible Project
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'"}

Stefan Hornburg (Racke)

unread,
Jan 14, 2021, 3:59:30 PM1/14/21
to ansible...@googlegroups.com
On 1/14/21 9:56 PM, ursa Brown wrote:
> 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.
> <https://groups.google.com/d/msgid/ansible-project/222abdaa-cffb-4fb6-b51e-5457e54c28a5n%40googlegroups.com?utm_medium=email&utm_source=footer
> <https://groups.google.com/d/msgid/ansible-project/222abdaa-cffb-4fb6-b51e-5457e54c28a5n%40googlegroups.com?utm_medium=email&utm_source=footer>>.
>
>
>
> --
> Ecommerce and Linux consulting + Perl and web application programming.
> Debian and Sympa administration. Provisioning with Ansible.
>
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to
> ansible-proje...@googlegroups.com <mailto:ansible-proje...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/e27e9b99-fcbd-44d0-a676-80d39bbb755cn%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/e27e9b99-fcbd-44d0-a676-80d39bbb755cn%40googlegroups.com?utm_medium=email&utm_source=footer>.
OpenPGP_signature

ursa Brown

unread,
Jan 14, 2021, 4:10:26 PM1/14/21
to Ansible Project
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'"}

PLAY RECAP ********************************************************************

Stefan Hornburg (Racke)

unread,
Jan 15, 2021, 2:35:34 AM1/15/21
to ansible...@googlegroups.com
On 1/14/21 10:10 PM, ursa Brown wrote:
> 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
Racke
> <https://groups.google.com/d/msgid/ansible-project/e27e9b99-fcbd-44d0-a676-80d39bbb755cn%40googlegroups.com?utm_medium=email&utm_source=footer
> <https://groups.google.com/d/msgid/ansible-project/e27e9b99-fcbd-44d0-a676-80d39bbb755cn%40googlegroups.com?utm_medium=email&utm_source=footer>>.
>
>
>
> --
> Ecommerce and Linux consulting + Perl and web application programming.
> Debian and Sympa administration. Provisioning with Ansible.
>
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to
> ansible-proje...@googlegroups.com <mailto:ansible-proje...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/2e6db3f1-f84f-48f0-bec1-2091a020ca31n%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/2e6db3f1-f84f-48f0-bec1-2091a020ca31n%40googlegroups.com?utm_medium=email&utm_source=footer>.
OpenPGP_signature

ursa Brown

unread,
Jan 15, 2021, 7:13:49 AM1/15/21
to Ansible Project
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

ursa Brown

unread,
Jan 15, 2021, 7:23:56 AM1/15/21
to Ansible Project
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.

###########
"msg": [
    "ARCH=x86_64",
    "alias=ens192",
    "DISK1=1.00 GBDISK2=Not DefDISK3=Not Define"
######################


DISK1={% if 'sdb' in ansible_devices %}{{ ansible_devices.sdb.partitions.sdb1.size }}{% else%}{{ 'Not Def' }}{% endif %}
DISK2={% if 'sdb' in ansible_devices %}{{ ansible_devices.sdb.partitions.sdbc.size}}{% else%}{{ 'Not Def' }}{% endif %}
DISK3={{ ansible_devices.sdc.partitions.sdc1.size | default('Not Define') }}
~

Was expecting
DISK1=100G
DISK2=Not Def
DISK3=Not Define

Stefan Hornburg (Racke)

unread,
Jan 15, 2021, 8:05:43 AM1/15/21
to ansible...@googlegroups.com
On 1/15/21 1:13 PM, ursa Brown wrote:
> 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
Racke
> <https://groups.google.com/d/msgid/ansible-project/2e6db3f1-f84f-48f0-bec1-2091a020ca31n%40googlegroups.com?utm_medium=email&utm_source=footer
> <https://groups.google.com/d/msgid/ansible-project/2e6db3f1-f84f-48f0-bec1-2091a020ca31n%40googlegroups.com?utm_medium=email&utm_source=footer>>.
>
>
>
> --
> Ecommerce and Linux consulting + Perl and web application programming.
> Debian and Sympa administration. Provisioning with Ansible.
>
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to
> ansible-proje...@googlegroups.com <mailto:ansible-proje...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/32b45b1b-3746-481a-b114-91c56b6fdd6an%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/32b45b1b-3746-481a-b114-91c56b6fdd6an%40googlegroups.com?utm_medium=email&utm_source=footer>.
OpenPGP_signature
Reply all
Reply to author
Forward
0 new messages