2016-06-21 76 views
0

我们试图在shell命令中使用嵌套循环。计数器部分(从0到5的循环)在线上没有被替换,并且由此导致投掷错误。嵌套循环中的Ansible投掷错误 - 计数器问题

任务是如下

- name: debug 
    debug: var=output.results.{{item}}.stdout.split('|').1 
    with_items: 
    - 0 
    - 1 
    - 2 
    - 3 
    - 4 
    - 5 

- name: Remove Groups 
    shell: echo neutron port-update {{ output.results.{{item}}.stdout.split("|").1 }} --no-security-groups > /tmp/test.txt 
    with_items: 
    - 0 
    - 1 
    - 2 
    - 3 
    - 4 
    - 5 

只是为了显示调试输出

changed: [1.1.1.1] => (item=2.2.2.2) 
changed: [1.1.1.1] => (item=3.3.3.3) 
changed: [1.1.1.1] => (item=4.4.4.4) 
changed: [1.1.1.1] => (item=5.5.5.5) 
changed: [1.1.1.1] => (item=6.6.6.6) 
changed: [1.1.1.1] => (item=7.7.7.7) 

当我们给的任务,因为

output.results.0.stdout.split('|').1 
or 
output.results.1.stdout.split('|').1 

,我们所得到的corrspondin IP。

但是,当我们循环它为5次,项都没有得到substtituted导致以下错误

fatal: [1.1.1.1]: FAILED! => {"failed": true, "msg": "template error while templating string: expected name or number. String: echo neutron port-update {{ output.results.[item].stdout.split(\"|\").1 }} --no-security-groups > /tmp/test.txt"} 

回答

1

var部分已经被解释为神社变量/表达。因此你不能在里面放置一个带花括号的变量。你也不能在彼此中嵌套表达式。 {{ foo {{ bar }} }}是无效的语法。

这应该工作:

debug: msg="{{ output.results[item].stdout.split('|').1 }}" 
... 
shell: echo neutron port-update {{ output.results[item].stdout.split("|").1 }} --no-security-groups > /tmp/test.txt