Yaml 如何在同一任务中展开多个列表
我有两个不同的变量,我想在一个命令中引用它们: 例如,我期望以下内容作为输出:Yaml 如何在同一任务中展开多个列表,yaml,ansible,ansible-playbook,Yaml,Ansible,Ansible Playbook,我有两个不同的变量,我想在一个命令中引用它们: 例如,我期望以下内容作为输出: list 1 item a item b list 2 another item from different var 10 -name : Run a module which executes a command on a host eg. via ssh command: host: {{ device_ip }} cmd_str: - 'list 1 ' - ' {{
list 1
item a
item b
list 2
another item from different var 10
-name : Run a module which executes a command on a host eg. via ssh
command:
host: {{ device_ip }}
cmd_str:
- 'list 1 '
- ' {{ item item[0].name }}'
- 'list 2 '
- ' {{ another item from different var item[1].id }}'
with_items:
- {{ var1 }}
- {{ var2 }}
var1:
- { name:a, address:test }
- { name:b, address:test2 }
var2:
- { name:x, id:10 }
要完成这项工作,我应该写些什么而不是用_items“
问题是我如何在同一位置展开两个不同的变量,而不必迭代整个命令(如果我将_项移动到与模块调用相同的缩进级别,这是可以做到的)我无法理解您实际想要做什么,但下面的剧本演示了:
- 使用dict在单个项中传递多个变量
- 使用Jinja2模板迭代每个变量
---
- hosts: all
gather_facts: no
vars:
var1:
- { name: a, address: test }
- { name: b, address: test2 }
var2:
- { name: x, id: 10 }
tasks:
- debug:
msg: |
list 1
{% for x in item.1 %}
item {{x.name}}
{% endfor %}
list 2
{% for x in item.2 %}
another item from different var {{x.id}}
{% endfor %}
with_items:
- { 1: "{{var1}}", 2: "{{var2}}" }
- shell: |
>/tmp/output.txt # truncate file
{% for x in item.1 %}
echo item {{x.name}} >>/tmp/output.txt
{% endfor %}
{% for x in item.2 %}
echo another item from different var {{x.id}} >>/tmp/output.txt
{% endfor %}
with_items:
- { 1: "{{var1}}", 2: "{{var2}}" }
示例会话:
$ ansible-playbook -i localhost, playbook.yml
PLAY [all] ********************************************************************
TASK: [debug ] ****************************************************************
ok: [localhost] => (item={1: [{'name': 'a', 'address': 'test'}, {'name': 'b', 'address': 'test2'}], 2: [{'name': 'x', 'id': 10}]}) => {
"item": {
"1": [
{
"address": "test",
"name": "a"
},
{
"address": "test2",
"name": "b"
}
],
"2": [
{
"id": 10,
"name": "x"
}
]
},
"msg": "list 1\n item a\n item b\nlist 2\n another item from different var 10\n"
}
TASK: [shell >/tmp/output.txt # truncate file
{% for x in item.1 %}
echo item {{x.name}} >>/tmp/output.txt
{% endfor %}
{% for x in item.2 %}
echo another item from different var {{x.id}} >>/tmp/output.txt
{% endfor %}
] ***
changed: [localhost] => (item={1: [{'name': 'a', 'address': 'test'}, {'name': 'b', 'address': 'test2'}], 2: [{'name': 'x', 'id': 10}]})
PLAY RECAP ********************************************************************
localhost : ok=2 changed=1 unreachable=0 failed=0
来自debug
模块的msg
中显示的输出:
list 1
item a
item b
list 2
another item from different var 10
item a
item b
another item from different var 10
从shell
模块在/tmp/Output.txt
中输出:
list 1
item a
item b
list 2
another item from different var 10
item a
item b
another item from different var 10
以下是有关循环的Ansible docs页面:
我认为您正在寻找嵌套循环、子元素或并行集合上的循环。单命令是什么意思?一项任务?在
命令中,似乎有两个不同的任务。如果您有两个任务,那么列表之间的关系是什么?他们有相同数量的物品吗?或者你只想一个接一个地遍历两个列表。一个更具体的任务示例更容易理解。这是一个单独的任务,通过组合下面的项目来构建命令。例如,要发送到设备的一组指令。嵌套循环和并行集在此不适用。嵌套循环提供m*n值,并行集合要求两个列表的长度相同。不确定子元素,无法使其工作。谢谢,这与我期望的非常接近。两个问题:1。如何使列表1、项目a、项目b等都成为列表的一个元素(即,我需要将它们分别传递给一个命令来代替msg.2.“|”是什么msg:do?有相关文档吗?更新了我的答案。我建议用jinja2模板填充的命令编写一个脚本,并通过shell
模块执行它。|
是常见的YAML语法:它对于在Ansible Playbook中嵌入多行文本或脚本非常有用。而且我总是重新推荐给pass模块参数以dict的形式出现,如本剧本中的|
可用,即使用debug:{msg:text}
而不是debug:msg=text
。