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...}}”

相关内容