1

I'm not able to figure out how I can pass a nested variable as parameter to Ansible's shell module & invoke the script "check.sh" however, below is what I tried.

---
- name: "Find the details here "

  hosts: localhost
  tasks:
    - name: set_fact
      set_fact:
        fpath_APP: "{{ fpath_APP + [ item.split('.')[0] ~ '/' ~ item | basename ] }}"
      with_items:
        - "{{ Source_Files.split(',') }}"
      vars:
        fpath_APP: []

    - name: Invoke shell script

    - shell: "./check.sh {{ Number }} '{{ vars['fpath_' + Layer] }}' > hello.txt"

  tasks:
    - name: Ansible find files multiple patterns examples
      find:
        paths: /home/examples
        patterns: "*.db"
        recurse: yes
      register: files_matched

    - name: Search for Number in the matched files
      command: grep -i {{ Number }} {{ item.path }}
      with_items:
        - "{{ files_matched.files }}"

The above playbook runs but does not invoke the shell module and completes without doing anything. See Output below:

$ ansible-playbook fpath.yml  -e " Source_Filenames=/tmp/logs/filename1.src,/tmp/logs/33211.sql,/app/axmw/Jenkins/file1.mrt
Layer=APP Number=57550"  [WARNING]: provided hosts list is empty, only
localhost is available. Note that the implicit localhost does not
match 'all'

 [WARNING]: While constructing a mapping from fpath.yml, line 2,
column 3, found a duplicate dict key (tasks). Using last defined value
only.


PLAY [Find the details here]
**************************************************************************************************************************************************

TASK [Gathering Facts]
******************************************************************************************************************************************************** ok: [localhost]

PLAY RECAP
******************************************************************************************************************************************************************** localhost                  : ok=1    changed=0    unreachable=0   
failed=0    skipped=0    rescued=0    ignored=0

Below are my failed attempts at changing the shell module syntax:

- shell: "./check.sh {{ Number }} '{{ 'fpath_' + vars[Layer] }}' > hello.txt"  

Does not invoke Shell module

- shell: "./check.sh {{ Number }} '/"{{ vars['fpath_' + Layer] }}/"' > hello.txt"

Gives Syntax Error.

- shell: "./check.sh {{ Number }} /"'{{ vars['fpath_' + Layer] }}'/" > hello.txt"

Gives Syntax Error.

I'm on the latest version of ansible and python version is 2.7.5.

2
  • [WARNING]: While constructing a mapping from fpath.yml, line 2, column 3, found a **duplicate dict key (tasks)**. Using last defined value only. This clearly indicates that the playbook you pasted is not the one you are running. Yours contains a duplicate tasks entry which is empty and this is why no tasks are played (or you are including the posted playbook in an other one with include_tasks rather than include_playbook). There are other potential problems in your tasks but fix that first and try to go further. Commented Aug 20, 2019 at 19:16
  • Thank you for pointing out. I have now added the complete playbook which still fails. Any solution would be great!! Thanks Commented Aug 20, 2019 at 20:03

1 Answer 1

1

You want to use the vars lookup plugin: https://docs.ansible.com/ansible/latest/plugins/lookup/vars.html

For your example, above you would want to do:

- shell: "./check.sh {{ Number }} {{ lookup('vars', 'fpath_' + Layer) }}" > hello.txt"
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for the input, however this has syntax error. Also, if you look at my playbook the second parameter to check.sh has to be in single quotes ''. I tried : - shell: "./check.sh {{ Number }} '{{ lookup('vars', 'fpath_' + Layer) }}' > hello.txt" but it gives error: ERROR! no action detected in task. This often indicates a misspelled module name, or incorrect module path. - name: Invoke shell script ^ here
I removed the duplicate task entry and this works with ``` - shell: "./check.sh {{ Number }} '{{ lookup('vars', 'fpath_' + Layer) }}' > hello.txt". Thank you @Mark Loeser for the pointer!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.