简体   繁体   English

将嵌套变量作为参数传递给ansible shell模块的问题

[英]Issue passing nested variable as parameter to ansible shell module

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. 我无法弄清楚如何将嵌套变量作为参数传递给Ansible的shell模块并调用脚本“ check.sh”,但是,下面是我尝试的方法。

---
- 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. 上面的剧本可以运行,但不会调用shell模块,并且无需执行任何操作即可完成。 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模块

- 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. 我正在使用ansible的最新版本,而python版本是2.7.5。

You want to use the vars lookup plugin: https://docs.ansible.com/ansible/latest/plugins/lookup/vars.html 您想使用vars查找插件: 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"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM