将嵌套变量作为参数传递给ansible shell模块时发出
我不知道如何将嵌套变量作为参数传递给Ansible的shell模块&调用脚本“check.sh”。不过,下面是我尝试的将嵌套变量作为参数传递给ansible shell模块时发出,shell,syntax,ansible,yaml,runtime-error,Shell,Syntax,Ansible,Yaml,Runtime Error,我不知道如何将嵌套变量作为参数传递给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 ] }}"
---
- 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"
给出语法错误
- shell: "./check.sh {{ Number }} /"'{{ vars['fpath_' + Layer] }}'/" > hello.txt"
我使用的是ansible的最新版本,python版本是2.7.5。您想使用
vars
查找插件:
在上面的示例中,您希望执行以下操作:
- shell: "./check.sh {{ Number }} {{ lookup('vars', 'fpath_' + Layer) }}" > hello.txt"
[警告]:从fpath.yml第2行第3列构造映射时,发现**重复的dict键(任务)**。仅使用上次定义的值。
这清楚地表明粘贴的剧本不是正在运行的剧本。您的任务包含一个重复的任务
条目,该条目为空,这就是为什么不播放任务的原因(或者您将发布的剧本包含在另一个带有包含任务
的剧本中,而不是包含剧本
)。您的任务中还存在其他潜在问题,但请先解决这些问题,并尝试进一步解决。感谢您指出。我现在已经添加了完整的剧本,但仍然失败。任何解决方案都会很好!!感谢您的输入,但是这有语法错误。另外,如果您查看我的剧本,则要检查的第二个参数.sh必须在单引号“”中。我试过:-shell:“./check.sh{{{Number}}{{{lookup('vars','fpath{Layer}}}'>hello.txt”,但它给出了错误:error!在任务中未检测到任何操作。这通常表示模块名称拼写错误或模块路径不正确。-名称:Invoke shell script^在这里,我删除了重复的任务条目,这适用于`-shell:“./check.sh{{{Number}}{{lookup('vars','fpath{Layer)}}}>hello.txt”。谢谢@Mark Loeser的指点!!