Concatenate variable and string in looping with_items

Is it possible to concatenate like below?

  • name: Retaining latest backup directory
    shell: find $(pwd)/backup -type d -prune -exec ls -d {} ; |tail -1
    register: import_back_up_dir

  • name: Import dashboard
    grafana_dashboard:
    state: present

message: Updated by ansible
overwrite: no
path: “{{ item }}”
with_items:

  • “{{ import_back_up_dir }} + SonarQube_Dashboard.json” >>>>>> I am trying to concatenate like this however this is not working - could some one please suggest how can we achieve this concatenation.

*_Is it possible to concatenate like below?_*

\- name: Retaining latest backup directory 
  shell:  find $\(pwd\)/\*backup\* \-type d \-prune \-exec ls \-d \{\} \\; |tail \-1
  register: import\_back\_up\_dir

\- name: Import dashboard
  grafana\_dashboard:
    state: present
    message: Updated by ansible
    overwrite: no
    path: "\{\{ item \}\}"
  with\_items:
     \- "\{\{ import\_back\_up\_dir \}\} \+ SonarQube\_Dashboard\.json"    \_>>>>>> I am trying to concatenate like this however

this is not working - could some one please suggest how can we achieve this concatenation.

Maybe you give us the objective of your task first ...

Regards
         Racke

Thanks Stefan for looking into this. actually I want to import some grafana dashboards and I need to map this import_back_up_dir (which I cannot hardcode) to appropriate json files. so I want to find a way to concatenate these.

Replied

Thanks Stefan for looking into this. actually I want to import some grafana dashboards and I need to map this import_back_up_dir (which I cannot hardcode) to appropriate json files. so I want to find a way to concatenate these.

Concatenate need to be done in inside of {{ }} and use tilde

"{{ import_back_up_dir ~ 'SonarQube_Dashboard.json' }}"

HI kai,

thanks for the response. however I am trying to use inside looping with_items it isn’t working.

“msg”: "error : Can’t load json file [Errno 2] No such file or directory: '{\‘stderr_lines\’:

trying like this

with_items:

  • “{{ dashboards_imported_from_latest_backup ~ ‘/Artifactory_Monitoring.json’ }}”

This will only be one item so you don't need to use with_items.
Anyway, since you only need the text it should be like this

  with_items:
    - "{{ dashboards_imported_from_latest_backup}}/Artifactory_Monitoring.json"

Thanks Kai. No actually this is not going to be one item only as it should be done for almost 7-8 json files. so i think the below trick you shown I am going to try that right now and see if that works forme. Thanks again , cheers!

I tried the above what Kai suggested however that is not working and unable to get the particular json for me from the specific backup dir. However I could find a way to get this done as shown below - this way we can get the output from the variable line by line.

with_items:

  • “{{ dashboards_imported_from_latest_backup.stdout_lines[2] }}”
  • “{{ dashboards_imported_from_latest_backup.stdout_lines[3] }}”
  • “{{ dashboards_imported_from_latest_backup.stdout_lines[4] }}”