2

I am collecting some output from ansible_facts and writing them to a file on remote server with the help of copy module (content and dest) I need to write as NONE if some item is not available on server (say disk sdb is not present on server).

How can I write NONE if disk sdb is not found?

My playbook is as below

- copy:
    content: |
      Memory = {{ ansible_memtotal_mb }}
      Size of disk sda =  {{ ansible_devices.sda.size  }}
    dest: /tmp/test

but if disk sdb is not available on the server how to check and write a custom message like below

Size of disk sdb =  NONE
Vladimir Botka
  • 58,131
  • 4
  • 32
  • 63
pratkul007
  • 89
  • 1
  • 6

1 Answers1

2

Use default filter. For example

- copy:
    content: |
      Memory = {{ ansible_memtotal_mb }}
      Size of disk sda =  {{ ansible_devices.sda.size  }}
      Size of disk sdb =  {{ ansible_devices.sdb.size|default('NONE')  }}
    dest: /tmp/test

Generally, Jinja2 condition if-else-endif should also do the job

- copy:
    content: |
      Memory = {{ ansible_memtotal_mb }}
      Size of disk sda =  {{ ansible_devices.sda.size }}
      {% if ansible_devices.sdb.size is defined %}
      Size of disk sdb =  {{ ansible_devices.sdb.size }}
      {% else %}
      Size of disk sdb =  NONE
      {% endif %}
    dest: /tmp/test

See 'if' statement in jinja2 template.

Vladimir Botka
  • 58,131
  • 4
  • 32
  • 63