¿Cómo asignar un valor vacío a una variable en Ansible?

12

Si firewall_allowed_portsen:

- name: port {{ item }} allowed in firewall
  ufw:
    rule: allow
    port: "{{ item }}"
    proto: tcp
  with_items: 
    - 22
    - "{{ firewall_allowed_ports }}"

no está definido, entonces se produce este error:

fatal: [host.example.com]: FAILED! => {"failed": true, "msg": "the field 'args' 
has an invalid value, which appears to include a variable that is undefined.

Intenta resolver el problema

"{{ firewall_allowed_ports | }}"

resultados en:

fatal: [host.example.com]: FAILED! => {"failed": true, "msg": "template error
while templating string: expected token 'name', got 'end of print statement'. 
String: {{ firewall_allowed_ports | }}"}
030
fuente

Respuestas:

14

Use default([])para crear una lista vacía si firewall_allowed_portsno está definida. La with_itemsomitirá cuando esté vacía.

- name: port {{ item }} allowed in firewall
  ufw:
    rule: allow
    port: "{{ item }}"
    proto: tcp
  with_items: 
    - 22
    - "{{ firewall_allowed_ports | default([]) }}"
jscott
fuente