Score:0

Ansible playbook - defining var based on condition

cn flag
Sam

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 ?

br flag
curls "{{ }}" can't be nested.
cn flag
Sam
So you mean I need to remove {{ }} from the vars inside the primary/parent {{ }} ? Like this var: "{{ folder/target }}"
U880D avatar
ca flag
Right, you need to remove them, but like `"{{ folder }}/{{ target }}/test_...`
cn flag
Sam
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
Score:0
in flag

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 }}"
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.