【问题标题】:Issue passing nested variable as parameter to ansible shell module问题将嵌套变量作为参数传递给 ansible shell 模块
【发布时间】:2019-12-26 00:55:50
【问题描述】:

我无法弄清楚如何将嵌套变量作为参数传递给 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 }}"

上面的 playbook 运行但不调用 shell 模块并且什么都不做就完成了。请参阅下面的输出:

$ 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

以下是我更改 shell 模块语法的失败尝试:

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

不调用 Shell 模块

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

给出语法错误。

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

给出语法错误。

我用的是最新版本的ansible,python版本是2.7.5。

【问题讨论】:

  • [WARNING]: While constructing a mapping from fpath.yml, line 2, column 3, found a **duplicate dict key (tasks)**. Using last defined value only. 这清楚地表明您粘贴的剧本不是您正在运行的剧本。您的包含重复的 tasks 条目,该条目为空,这就是为什么不播放任务的原因(或者您将发布的剧本包含在另一个带有 include_tasks 而不是 include_playbook 的剧本中)。您的任务中还有其他潜在问题,但请先解决此问题并尝试更进一步。
  • 感谢您的指出。我现在添加了仍然失败的完整剧本。任何解决方案都会很棒!!谢谢

标签: shell syntax ansible yaml runtime-error


【解决方案1】:

您想使用vars 查找插件:https://docs.ansible.com/ansible/latest/plugins/lookup/vars.html

对于你的例子,上面你会想要做:

- shell: "./check.sh {{ Number }} {{ lookup('vars', 'fpath_' + Layer) }}" > hello.txt"

【讨论】:

  • 感谢您的输入,但是这有语法错误。此外,如果您查看我的剧本,check.sh 的第二个参数必须用单引号 '' 括起来。我试过: - shell: "./check.sh {{ Number }} '{{ lookup('vars', 'fpath_' + Layer) }}' > hello.txt" 但它给出了错误:错误!任务中未检测到任何操作。这通常表示模块名称拼写错误或模块路径不正确。 - 名称:在此处调用 shell 脚本 ^
  • 我删除了重复的任务条目,这适用于 ``` - shell: "./check.sh {{ Number }} '{{ lookup('vars', 'fpath_' + Layer) }}' > hello.txt”。谢谢@Mark Loeser 的指点!!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-11
  • 1970-01-01
  • 1970-01-01
  • 2017-12-25
  • 2017-05-15
  • 2022-11-07
相关资源
最近更新 更多