I have a working task to migrate (copy) a specific part of my homedir (second to last line is the important one):
- name: "Migrate my-pc module home files"
copy:
src: "{{ migration_source_path }}{{ item }}"
dest: "{{ migration_destination_path }}{{ item }}"
loop: "{{ migration_paths_my_pc }}"
when:
- not is_migrating_all
- "'{{ migration_source_path }}{{ item }}' is exists"
- m_my_pc | bool
Both of the ..._path
variables always end with a forward slash (/
).
Explanation: It takes a list of file paths from migration_paths_my_pc
and copies them from migration_source_path
to migration_destination_path
). This will only happen if: the flag for that module has been set (m_my_pc
); and the path exists at source; and I'm not already migrating all files from homedir anyway (is_migrating_all
).
My current working solution (above) gives me the warning:
[WARNING]: conditional statements should not include jinja2 templating delimiters such as {{ }} or {% %}. Found: '{{ migration_source_path }}{{ item }}' is exists
I understand that I am supposed to remove the curly braces {{
from when:
section because conditionals have implied curly braces around them anyway. The problem is I cannot figure out how to get it to work. Some of the many unsuccessful attempts are:
- vars["" + migrate_source_path + item] is exists
- vars[migrate_source_path + item] is exists
- vars[migrate_source_path ~ item] is exists
- vars[migrate_source_path]vars[item] is exists
- '' + migrate_source_path + item is exists
- lookup('/home/k/test/test2/' + item) is exists
- "{{ lookup('vars', 'migrate_source_path' + 'item') is exists }}"
- "{{ lookup('vars', 'migrate_source_path') + lookup('vars', 'item') is exists }}"
- lookup('vars', 'migrate_source_path')lookup('vars', 'item') is exists
Background:
I am automating personal PC setups for practical reasons as well as to learn more about Ansible and Ansible-playbooks.
Any other recommendations or advice welcome (in the comments, I guess) as well.
UPDATE:
My design goals (functional aspirations) for the migration are:
- I want to keep the maintenance cost low, but I would like to have the information which paths were copied and which were skipped if I need it. So I thought the script should copy the paths that exist at source without halting or throwing an error in case of a path missing a source. Warnings or other plain messages would be great. My current script does this acceptably.
- It would be nice to be able to easily toggle which paths get a copy attempt and which don't (e.g. something might change in relevance to a migration). Doing this in
project/vars/
seemed intuitive and therefore maybe a better design than toggling them inside the task. Other variables that are more likely to get commented out or changed are atproject/vars/
as well. Commenting something out or uncommenting something is a single hotkey in most text editors and IDE-s. So quite convenient.