Ansible에서 변수에 빈 값을 할당하는 방법은 무엇입니까? – “{{ firewall_allowed_ports

있는 경우 firewall_allowed_ports:

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

정의되지 않은 경우이 오류가 발생합니다.

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

문제 해결을 시도

"{{ firewall_allowed_ports | }}"

결과 :

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


답변

정의되지 않은 default([])경우 빈 목록을 작성하는 데 사용하십시오 firewall_allowed_ports. 는 with_items비어있는 경우를 건너 뜁니다.

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