Loop through the contents of include_vars file via a shell module

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

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".

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:

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.

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

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.

Sorry that's a typo .

The location in playbook and the actual file is same.

I got this working

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

With_items: "{{ dits | list }}"

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.