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”,我想要的是循環遍歷每個項目並在條件為真時執行某些操作

我怎樣才能實現這個目標?

答案1

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

您已經建立了一個包含單一項目的陣列。相反,您需要傳遞變數的結果。試試這個:

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

相關內容