I want to create a var based on condition so I created the below :
hosts: test
vars: vtarget_backup_folder_in_progress: "{{ "{{ folder }}/{{ target }}/test{{ hostvars['localhost']['tstamp'].stdout }}{{ type }}" if node_type == "master" else "{{ folder }}/{{ target }}/prod{{ hostvars['localhost']['tstamp'].stdout }}_{{ type }}" }}"
but it fails with me with below error :
We could be wrong, but this one looks like it might be an issue with missing quotes. Always quote template expression brackets when they start a value. For instance:
with_items:
- {{ foo }}
Should be written as:
with_items:
- "{{ foo }}"
can you please help me with that ?
EDIT
I have tried the below :
tasks:
- name: set IN_PROGRESS backup folder
set_fact:
v_target_backup_folder_in_progress: >-
{% if node_type == "master" %}
"{{ folder }}/{{ target }}/test_{{ hostvars['localhost']['tstamp'].stdout }}_{{ type }}"
{% else %}
"{{ folder }}/{{ target }}/prod_{{ hostvars['localhost']['tstamp'].stdout }}_{{ type }}"
{% endif %}
- name: ensure target in progress folder exists
file:
path: "{{ v_target_backup_folder_in_progress }}"
state: directory
owner: "{{ app_user }}"
group: "{{ app_user_group }}"
mode: u=rwx,g=rwx
it is getting the right values of the variable when I debug the playbook BUT when I check v_target_backup_folder_in_progress on each node of the 3 nodes that the play runs on I find something weird :
on one node the v_target_backup_folder_in_progress doesnt get created although it appears to be created in debug mode but when I go to the same path I cant find the directory !
on the other 2 nodes the v_target_backup_folder_in_progress get created but as a file! not a directory although it appears to be created in debug mode as a directory.
so why is this happening ?