Bagaimana cara menetapkan nilai kosong ke variabel di Ansible?


12

Jika firewall_allowed_ports dalam:

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

tidak ditentukan, maka kesalahan ini terjadi:

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

Mencoba memecahkan masalah tersebut

"{{ firewall_allowed_ports | }}"

menghasilkan:

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

Jawaban:


14

Gunakan default([])untuk membuat daftar kosong jika firewall_allowed_portstidak ditentukan. The with_itemsakan melewatkan itu ketika kosong.

- name: port {{ item }} allowed in firewall
  ufw:
    rule: allow
    port: "{{ item }}"
    proto: tcp
  with_items: 
    - 22
    - "{{ firewall_allowed_ports | default([]) }}"
Dengan menggunakan situs kami, Anda mengakui telah membaca dan memahami Kebijakan Cookie dan Kebijakan Privasi kami.
Licensed under cc by-sa 3.0 with attribution required.