0

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 ?

Gerald Schneider
  • 19,757
  • 8
  • 52
  • 79
Sam
  • 1
  • 1
  • 2
    curls "{{ }}" can't be nested. – Vladimir Botka Nov 17 '21 at 23:26
  • So you mean I need to remove {{ }} from the vars inside the primary/parent {{ }} ? Like this var: "{{ folder/target }}" – Sam Nov 18 '21 at 08:35
  • Right, you need to remove them, but like `"{{ folder }}/{{ target }}/test_...` – U880D Nov 18 '21 at 09:48
  • If you mean like this : v_target_backup_folder_in_progress: "{{ folder }}/{{ target }}/test_{{ hostvars['localhost']['tstamp'].stdout }}_{{ type }}" if node_type == "master" else "{{ folder }}/{{ target }}" it didn't work – Sam Nov 18 '21 at 16:13

1 Answers1

0

This should do it:

vtarget_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 %}"

All in one string, no nested braces, if/else/endif with {% %}

You could even reduce it a little by moving the common start and end out of the conditional:

vtarget_backup_folder_in_progress: "{{ folder }}/{{ target }}/{% if node_type == 'master' %}test{{ hostvars['localhost']['tstamp'].stdout }}{% else %}prod{{ hostvars['localhost']['tstamp'].stdout }}_{{ type }}{% endif %}{{ type }}"
Gerald Schneider
  • 19,757
  • 8
  • 52
  • 79