I'm trying to create files from different keys in an array, based on a key in the array being set or not:
##################################################
#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
##################################################
However I'm getting an error
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"}
Based on the log messages of the playbook, Ansible is passing the whole items array to "when", what I want is to loop through each item and do something when a condition is true
How can I achieve this?
Best Answer
You have created an array with a single item. Instead, you need to pass the results of your variables. Try this instead: