有人能推荐一种方法来获取两个寄存器变量的内容并将它们传递给一个命令吗?同时还要以1:1的方式排列每个变量的输出结果。 (即VS1:rule1,VS2:rule2,依此类推,如下所示)
以下是“ Virtual_Check”和“ Rule_Check”中stdout_lines中存储的内容:
"Virtual_Check.stdout_lines": [
[
"ltm virtual VS1 ",
"ltm virtual VS2 ",
"ltm virtual VS3 ",
"ltm virtual VS",
"Rule_Check.stdout_lines": [
[
"myrule1",
" ",
"",
" myrule2",
" ",
"",
" myrule3",
" ",
"",
" myrule4",
" ",
"",
现在,我想将变量的内容传递给一个命令,如下所示。当我运行此剧本时,“ with_nested”部分下的“ Virtual_Check”部分按预期循环,但我遇到的问题是,“ Rule_Check”部分无法正确循环(我已经尝试了两种方法下面)
到目前为止,我已经尝试使用with_nested来完成此操作,而且似乎无法正确循环第二个变量。
- name: Update VS iRule
bigip_command:
commands:
- "modify ltm virtual {{ item.0 }} rules { {{ item.1 }} myrule10 }"
provider:
server: "{{ inventory_hostname }}"
password: "{{ remote_passwd }}"
user: "{{ remote_username }}"
validate_certs: no
delegate_to: localhost
with_nested:
- [ "{{ Virtual_Check['stdout'][0] | replace('ltm virtual', '') | replace('\n', '') }}"]
- [ "{{ Rule_Check['stdout'][0] | replace('\n', '') }}" ]
- [ "{{ Rule_Check['stdout_lines'][0] }}" ]
我希望'modify ltm virtual {{item.0}}规则{{{item.1}} myrule10}'行将与Virtual_Check和Rule_Check列表中的内容一起处理
例如:
modify ltm virtual VS1 rules { myrule1 myrule10 }
modify ltm virtual VS2 rules { myrule2 myrule10 }
modify ltm virtual VS3 rules { myrule3 myrule10 }
modify ltm virtual VS4 rules { myrule4 myrule10 }
答案 0 :(得分:0)
nested
lookup不能满足您的期望:它在第一个元素上创建一个循环,在第二个元素上创建一个子循环,在第三个元素上创建一个子子循环,等等...
您要寻找的是zip
filter,它将使您可以将多个列表组装在一个列表中,并将相同索引的所有项目组合到一个列表中。
下面的示例带有您所质疑的原始样本数据。您只需要适应实际情况即可:
---
- name: zip example
hosts: localhost
gather_facts: false
vars:
servers: [ 'VS1', 'VS2', 'VS3', 'VS4' ]
rules: [ myrule1, myrule2, myrule3, myrule4 ]
tasks:
- name: Show zipped data from servers and rules
debug:
msg: "Server {{ item.0 }} has rule: {{ item.1 }}"
loop: "{{ servers | zip(rules) | list }}"
给出
PLAY [zip example] ********************************************************************************************************************************************************************************************************
TASK [Show zipped data from servers and rules] ****************************************************************************************************************************************************************************
ok: [localhost] => (item=['VS1', 'myrule1']) => {
"msg": "Server VS1 has rule: myrule1"
}
ok: [localhost] => (item=['VS2', 'myrule2']) => {
"msg": "Server VS2 has rule: myrule2"
}
ok: [localhost] => (item=['VS3', 'myrule3']) => {
"msg": "Server VS3 has rule: myrule3"
}
ok: [localhost] => (item=['VS4', 'myrule4']) => {
"msg": "Server VS4 has rule: myrule4"
}
PLAY RECAP ****************************************************************************************************************************************************************************************************************
localhost : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0