Concatenate variable and string in looping with_items

491 views
Skip to first unread message

Vishal Bobade

unread,
Jan 31, 2020, 12:17:01 PM1/31/20
to Ansible Project
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.

Stefan Hornburg (Racke)

unread,
Jan 31, 2020, 12:37:36 PM1/31/20
to ansible...@googlegroups.com
On 1/31/20 6:17 PM, Vishal Bobade wrote:
> *_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
> _
>
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to
> ansible-proje...@googlegroups.com <mailto:ansible-proje...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/98f66710-6fc0-4b1f-afdf-6190afab69c2%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/98f66710-6fc0-4b1f-afdf-6190afab69c2%40googlegroups.com?utm_medium=email&utm_source=footer>.


--
Ecommerce and Linux consulting + Perl and web application programming.
Debian and Sympa administration. Provisioning with Ansible.

signature.asc

Vishal Bobade

unread,
Jan 31, 2020, 1:04:45 PM1/31/20
to Ansible Project
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.

Vishal Bobade

unread,
Jan 31, 2020, 1:26:45 PM1/31/20
to Ansible Project
Replied


On Friday, January 31, 2020 at 11:07:36 PM UTC+5:30, Stefan Hornburg (Racke) wrote:
On 1/31/20 6:17 PM, Vishal Bobade wrote:
> *_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
> _
>
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to

Vishal Bobade

unread,
Jan 31, 2020, 1:30:23 PM1/31/20
to Ansible Project
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.

Kai Stian Olstad

unread,
Jan 31, 2020, 2:27:57 PM1/31/20
to ansible...@googlegroups.com
On 31.01.2020 18:17, Vishal Bobade wrote:
> *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.*

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

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

--
Kai Stian Olstad

Vishal Bobade

unread,
Feb 1, 2020, 12:54:05 AM2/1/20
to Ansible Project
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\\':

Vishal Bobade

unread,
Feb 1, 2020, 12:55:44 AM2/1/20
to Ansible Project

trying like this 

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

Kai Stian Olstad

unread,
Feb 1, 2020, 3:12:41 AM2/1/20
to ansible...@googlegroups.com
On 01.02.2020 06:55, Vishal Bobade wrote:
> trying like this
>
> with_items:
> - "{{ dashboards_imported_from_latest_backup ~
> '/Artifactory_Monitoring.json' }}"
>
>
>
> On Saturday, February 1, 2020 at 11:24:05 AM UTC+5:30, Vishal Bobade
> wrote:
>>
>> 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\\':

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"


--
Kai Stian Olstad

Vishal Bobade

unread,
Feb 2, 2020, 11:52:42 PM2/2/20
to Ansible Project
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!

Vishal Bobade

unread,
Feb 3, 2020, 12:24:52 AM2/3/20
to Ansible Project
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] }}"
Reply all
Reply to author
Forward
0 new messages