I have a playbook:
- name: Quick Test Script
hosts: servers
vars:
var_list:
- AAA
- BBB
- CCC
- DDD
tasks:
- name: Make options list
set_fact:
my_list: []
- name: Add variables to list
set_fact:
my_list: "{{ my_list + ['--exclude {{ item }}.yaml'] }}"
loop: "{{ var_list }}"
- name: List of rsync_opts
debug:
var: my_list
- name: Deploy files
synchronize:
src: ~/ansible
dest: "{{ ansible_env.HOME }}/test"
archive: false
checksum: true
owner: false
group: false
recursive: true
links: true
times: true
delete: true
rsync_opts: "{{ my_list }}"
I was expecting this to make a list of ['--exclude AAA.yaml', '--exclude BBB.yaml, '--exclude CCC.yaml, '--exclude DDD.yaml and pass that to the rsync_opts. What I get instead:
PLAY [Quick Test Script] ****************************************************************************************************************************************************************************
TASK [Gathering Facts] ******************************************************************************************************************************************************************************
ok: [TESTBOX]
TASK [Make options list] ****************************************************************************************************************************************************************************
ok: [TESTBOX]
TASK [Add variables to list] ************************************************************************************************************************************************************************
ok: [TESTBOX] => (item=AAA)
ok: [TESTBOX] => (item=BBB)
ok: [TESTBOX] => (item=CCC)
ok: [TESTBOX] => (item=DDD)
TASK [List of rsync_opts] ***************************************************************************************************************************************************************************
ok: [TESTBOX] => {
"my_list": "VARIABLE IS NOT DEFINED!"
}
TASK [Deploy files] *********************************************************************************************************************************************************************************
fatal: [TESTBOX]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: ['--exclude BBB.yaml', '--exclude CCC.yaml', '--exclude DDD.yaml', '--exclude {{ item }}.yaml']: 'item' is undefined\n\nThe error appears to be in 'test.yml': line 22, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - name: Deploy files\n ^ here\n"}
From the error message, it looks like the first item in the list isn't getting templated, so the debug and rsync modules have issues with item not being defined. What is going on here, and how do I fix it? Is it the initial definition of var_list or how I'm building my_list?