Dynamic File/ Template populate.

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

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

    *******************************
    ---
    - 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

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”}

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.

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

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’”}

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
        Racke

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

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

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

Below is the output:

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.

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