【发布时间】: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