Access specific elements using with_items / with_subelememts

14 views
Skip to first unread message

Saranya N

unread,
Jan 28, 2019, 10:21:18 AM1/28/19
to Ansible Project
I have a variable file with dictionary under a role like below format :


Applformat:
- Appname : app-main
Parameters:
- x : a
Y : b
Z : c

- x : d
Y : e
- z : f
- Appname : app-relay
Parameters:
- x : g
Y : h
Z : I

- x : j
Y : k
Z : l

Etc many apps are there with different names.

I need to run a cli( my app command) that will make few apps as child n parent.

My command will be something like below.

Command : Kif run -parentappp {{ item.Appname }} -childapp {{ item.Appname }}
With_items :
- "{{ Appname }}"

I need a logic in my play to state which app is parent and which is child and need assistance on this.

My first app is always the parent of second app.

So first command should take first app as parent and second app as child.

My second app is parent of the 3rd, 4th .. etc remaining apps.

So these two conditions should be satisfied during the loop.

Kindly assist

Kai Stian Olstad

unread,
Jan 28, 2019, 10:45:51 AM1/28/19
to ansible...@googlegroups.com
Something like this should work

- command: Kif run -parentappp {{ Applformat[item | int].Appname }}
-childapp {{ Applformat[item | int + 1].Appname }}
with_sequence: start=0 end={{ Applformat | length - 1 }} stride=2

--
Kai Stian Olstad
Message has been deleted

Saranya N

unread,
Jan 29, 2019, 9:11:47 AM1/29/19
to Ansible Project
Hi Kai ,

M getting below error and I wonder why :(

Can't parse arg end=u'3\xa0stride=2' as integer .


Also please help me understand this,

My first app is parent of second and
Second app is parent of Remaining apps i.e from 3rd app onwards .

So I think I'll need the above task twice with different sequence number to achieve the two conditions ?


Kai Stian Olstad

unread,
Feb 2, 2019, 7:08:10 AM2/2/19
to ansible...@googlegroups.com
On 29.01.2019 13:52, Saranya N wrote:
> I Hi Kai ,
>
>
> I think this will make my 3rd app parent of 4th n so on..
>
> I don't want that.
>
> First is parent of second.
> Second is parent of remaining all apps.

Just split it on two task with different with_sequences

--
Kai Stian Olstad

Kai Stian Olstad

unread,
Feb 2, 2019, 7:09:47 AM2/2/19
to ansible...@googlegroups.com
On 29.01.2019 15:11, Saranya N wrote:
> Hi Kai ,
>
> M getting below error and I wonder why :(
>
> Can't parse arg end=u'3\xa0stride=2' as integer .

You have gotten some strange character in there, delete it and write it
again.


> Also please help me understand this,
>
> My first app is parent of second and
> Second app is parent of Remaining apps i.e from 3rd app onwards .
>
> So I think I'll need the above task twice with different sequence
> number to achieve the two conditions ?

Yes, that would be the easiest.

--
Kai Stian Olstad
Reply all
Reply to author
Forward
0 new messages