Loop through the contents of include_vars file via a shell module

1,246 views
Skip to first unread message

Saranya N

unread,
May 10, 2018, 10:42:54 AM5/10/18
to Ansible Project
I want to replicate a shell script that loops through the contents of a text.file
For i in `cat text.file`

I tried below and got an error.
I'm not getting which logic to use. Kindly advise.

- name: Load the variables

include_vars:

file: /home/devuser/internproj/inventories/NP/voblist.yml

register: dits_list

- debug:

msg: "{{ dits_list.ansible_facts }}"

- name: run the tool commands

shell: /usr/atria/bin/sertool -load -host aaa {{ items }} {{ items }}

args:

chdir: "/home/vobadmin"

with_items: "{{ vob_list.dits }}" #How to achieve this part?

I want to pass the contents of the voblist to the shell module to excute the above shell command.

Basically I want to replicate the below shell script

For i in `cat /home/devuser/internproj/inventories/NP/voblist.yml`

Do

/usr/atria/bin/sertool -load -host aaa $i $i

Done

if

===========================================================================================================

cat /home/devuser/internproj/inventories/NP/voblist.yml

---

dits:

- /project/vob_solaris_test.vbs

- /project/vob_solaris_test2.vbs

Kai Stian Olstad

unread,
May 12, 2018, 3:32:12 PM5/12/18
to ansible...@googlegroups.com
On 10.05.2018 16:42, Saranya N wrote:
> I want to replicate a shell script that loops through the contents of
> a text.file
> For i in `cat text.file`
>
> I tried below and got an error.
> I'm not getting which logic to use. Kindly advise.
>
> - name: Load the variables
>
> include_vars:
>
> file: /home/devuser/internproj/inventories/NP/voblist.yml
>
> register: dits_list

include_vars read the variable inside the file and make them available
for Ansible so no need for the register.


> - debug:
>
> msg: "{{ dits_list.ansible_facts }}"
>
>
>
> - name: run the tool commands
>
> shell: /usr/atria/bin/sertool -load -host aaa {{ items }} {{ items
> }}
>
> args:
>
> chdir: "/home/vobadmin"
>
> with_items: "{{ vob_list.dits }}" #How to achieve this part?

- name: run the tool commands
shell: /usr/atria/bin/sertool -load -host aaa {{ item }} {{ item }}
args:
chdir: "/home/vobadmin"
with_items: "{{ dits }}"



> I want to pass the contents of the voblist to the shell module to
> excute the above shell command.
>
> Basically I want to replicate the below shell script
>
> For i in `cat /home/devuser/internproj/inventories/NP/voblist.yml`
>
> Do
>
> /usr/atria/bin/sertool -load -host aaa $i $i
>
> Done
>
> if
>
>
>
> ===========================================================================================================
>
> cat /home/devuser/internproj/inventories/NP/voblist.yml
>
> ---
>
> dits:
>
>
>
> - /project/vob_solaris_test.vbs
>
> - /project/vob_solaris_test2.vbs

That for loop doesn't make sense with that file.
On fist loop it would contain "---", and the second it would contain
"dist:" and third "- /project/vob_solaris_test.vbs".

--
Kai Stian Olstad

Saranya N

unread,
May 13, 2018, 5:39:50 AM5/13/18
to Ansible Project
Hi Kai ,

Thankyou for your response but unfortunately it still doesn't work for me.

It still doesn’t work. Sorry, I’m very new to ansible and just started working on it.



I get below error:



task path: /home/dev_user/ansible_test/ansible_dev/clearcase/playbooks/testdump.yml:20

ok: [aaa.com] => {

"ansible_facts": {

"localvar": {

"vobs": [

"/project/vob_solaris_test.vbs",

"/project/vob_solaris_test22.vbs"

]

}

},

"changed": false

}



TASK [lsvob] **************************************************************************************************************************************************************************************************

task path: /home/dev_user/ansible_test/ansible_dev/clearcase/playbooks/testdump.yml:32

fatal: [aaa.com]: FAILED! => {

"failed": true,

"msg": "the field 'args' has an invalid value, which appears to include a variable that is undefined. The error was: 'items' is undefined\n\nThe error appears to have been in '/home/dev_user/ansible_test/ansible_dev/clearcase/playbooks/testdump.yml': line 32, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - name: lsvob\n ^ here\n"

}

to retry, use: --limit @/home/dev_user/ansible_test/ansible_dev/clearcase/playbooks/testdump.retry



PLAY RECAP ***************************************************************************************************************************

aaa.com : ok=3 changed=2 unreachable=0 failed=1









my playbook lokks like below:

---

- hosts: sol

become: yes

become_method: su

become_user: vobadmin

gather_facts: no



tasks:



- name: Load the variables

include_vars:

file: /home/a594588/ansible_test/ansible_dev/clearcase/inventories/NP/voblist.yml

name: localvar



- name: lsvob

shell: /usr/atria/bin/cleartool lsvob | grep -i {{ items }}

args:

chdir: "/home/vobadmin"

with_items: "{{ dits }}"





I changed the contents of the include_vars file input file:



cat /home/devuser/internproj/inventories/NP/voblist.yml



dits:



- /project/vob_solaris_test.vbs

- /project/vob_solaris_test22.vbs

Karl Auer

unread,
May 13, 2018, 7:06:21 AM5/13/18
to ansible...@googlegroups.com
I'm new to Ansible too, so take this explanation with a grain of salt!

My understanding of "with_items" is that it takes a list. The loop iterates over the list, referring to the current element as "item".

So inside your loop, you should refer to "item" (not "items"!)) which on each iteration will be replaced by the next element from the list called "dits".

Something like this:

- name: lsvob
    shell: /usr/atria/bin/cleartool lsvob | grep -i {{ item }}
    args:
      chdir: "/home/vobadmin"
    with_items: "{{ dits }}"

I'm not sure what that shell command is doing, so can't comment on whether it is correct for your purpose.

Regards, K.



--
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-project+unsubscribe@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/82f6779b-c698-44f0-81b5-37de10960a4c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Karl Auer

Email  : ka...@2pisoftware.com
Website: 
http://2pisoftware.com

GPG/PGP : 958A 2647 6C44 D376 3D63 86A5 FFB2 20BC 0257 5816
Previous: F0AB 6C70 A49D 1927 6E05 81E7 AD95 268F 2AB6 40EA

Saranya N

unread,
May 13, 2018, 7:55:20 AM5/13/18
to Ansible Project
Hi Karl,

The shell command is for a tool.
But the logic I'm trying to execute is very simple.
List all the files such that the files' names contains the words present in dits list which I'm sending via the include_vars file.

I'm seriously not understanding whats going wrong here.

I get dits is not defined error like below.

task path: /home/devuser/ansible_test/ansible_dev/clearcase/playbooks/simple.yml:15

fatal: [aaa.com]: FAILED! => {

"failed": true,

"msg": "'dits' is undefined"

}

to retry, use: --limit @/home/devuser/ansible_test/ansible_dev/clearcase/playbooks/simple.retry

PLAY RECAP ****************************************************************************************************************************************************************************************************

aaa.com : ok=1 changed=0 unreachable=0 failed=1

---

- hosts: sol

become: yes

become_method: su

become_user: vobadmin

gather_facts: no

tasks:

- name: Load the variables

include_vars:

file: /home/devuser/ansible_test/ansible_dev/clearcase/inventories/NP/voblist.yml

name: localvar

- name: List id

shell: ls -ltr | grep -i {{ item }}

register: shell_op

with_items: "{{ dits }}"

- debug:

msg: "{{ localvar.dits }}"

cat /home/devuser/internproj/inventories/NP/voblist.yml

dits:

- aaa

- bbb


Karl Auer

unread,
May 13, 2018, 8:49:00 AM5/13/18
to ansible...@googlegroups.com
The file you refer to in your playbook is:

/home/devuser/ansible_test/ansible_dev/clearcase/inventories/NP/voblist.yml

The file containing the dits variable is:

/home/devuser/internproj/inventories/NP/voblist.yml

Could that be the problem?

Regards, K.


--
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-project+unsubscribe@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Saranya N

unread,
May 13, 2018, 8:52:26 AM5/13/18
to Ansible Project
Sorry that's a typo .

The location in playbook and the actual file is same.

Message has been deleted

Saranya N

unread,
May 13, 2018, 10:33:04 AM5/13/18
to Ansible Project
I got this working

Since dits is a list I had to use like below.

With_items: "{{ dits | list }}"

Kai Stian Olstad

unread,
May 13, 2018, 11:26:01 AM5/13/18
to ansible...@googlegroups.com
On 13.05.2018 13:55, Saranya N wrote:
> I'm seriously not understanding whats going wrong here.

It's impotent to pay attention to the details.


> I get dits is not defined error like below.
>
> task path:
> /home/devuser/ansible_test/ansible_dev/clearcase/playbooks/simple.yml:15
>
> fatal: [aaa.com]: FAILED! => {
>
> "failed": true,
>
> "msg": "'dits' is undefined"
>
> }

That's because it doesn't exist, I explain bellow.


> - name: Load the variables
>
> include_vars:
>
> file:
> /home/devuser/ansible_test/ansible_dev/clearcase/inventories/NP/voblist.yml
>
> name: localvar

Here you say, take the variables in voblist.yml and put them is the
variable localvar.
This you did not have in you first post.


> - name: List id
>
> shell: ls -ltr | grep -i {{ item }}
>
> register: shell_op
>
> with_items: "{{ dits }}"

So you then need to use localvar.dits


> - debug:
>
> msg: "{{ localvar.dits }}"

You did it here so it's important to go over the entire code when you
changes things.

To me it looks like you are changing too many things at the same time,
and because of that makes bugs in your code.

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