Ansible with_items, 각 항목을 평가하지 않는 경우

Ansible with_items, 각 항목을 평가하지 않는 경우

배열의 키 설정 여부에 따라 배열의 다른 키에서 파일을 만들려고 합니다.

##################################################
#main site, same ftp user
- name: copy deploy keys private
  template: src=deploy_key dest=/root/.ssh/id_rsa-{{item.user}} owner=root group=root mode=0600
  with_items:
   - "{{joomla_websites + joomla_tls_websites + wordpress_websites + wordpress_h2_websites + phpbb_sites + symfony_websites + phpbb_sites}}"
  ignore_errors: true
  when: item.ftp_user is not defined
  tags:
    - sshd
    - newsite
#staging site, different ftp user, use same key
- name: copy deploy keys private
  template: src=deploy_key dest=/root/.ssh/id_rsa-{{item.ftp_user}} owner=root group=root mode=0600
  with_items:
   - "{{joomla_websites + joomla_tls_websites + wordpress_websites + wordpress_h2_websites + phpbb_sites + symfony_websites + phpbb_sites}}"
  ignore_errors: true
  when: item.ftp_user is defined
  tags:
    - sshd
    - newsite
##################################################

그러나 오류가 발생합니다.

fatal: [1.2.3.4]: FAILED! => {"failed": true, "msg": "The conditional check 'item.ftp_user is not defined' failed. The error was: error while evaluating conditional (item.ftp_user is not defined): 'item' is undefined\n\nThe error appears to have been in '/home/jochen/projects/automatem/ansible/roles/accounts/tasks/main.yml': line 14, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- name: copy deploy keys private, main site\n  ^ here\n"}

플레이북의 로그 메시지를 기반으로 Ansible은 전체 항목 배열을 "when"으로 전달합니다. 내가 원하는 것은 각 항목을 반복하고 조건이 true일 때 작업을 수행하는 것입니다.

어떻게 이를 달성할 수 있나요?

답변1

with_items:
   - "{{joomla_web...}}”

단일 항목으로 배열을 만들었습니다. 대신 변수의 결과를 전달해야 합니다. 대신 이것을 시도해 보세요:

with_items: |
   "{{joomla_web...}}”

관련 정보