英文:
How can I call with_items task registered output in another with_items task?
问题
tasks:
- name: 查找ARN
shell: aws acm list-certificates --output text --certificate-statuses ISSUED | grep {{ apache[item].domain }} | awk '{print $2}'
args:
executable: /bin/bash
with_items:
- "clientA"
- "clientB"
- "clientC"
register: sslarn
- debug: msg={{ item.stdout_lines }}
with_items:
- "{{ sslarn.results[item.index].stdout_lines }}"
- name: 重新生成字典组装文件
template:
src: /file/path/to/templates/update-dictionary-template.j2
dest: /file/path/to/dictionary/files/{{ item }}.yml
mode: 0644
with_items:
- "clientA"
- "clientB"
- "clientC"
- name: 组装字典
assemble:
ignore_hidden: yes
src: /file/path/to/dictionary/files/
dest: /file/path/to/dictionary/dictionary.yml
英文:
I'm trying to create a playbook that will lookup the AWS ARN for an SSL certificate for each of our clients and then use the results with a template to update a custom dictionary.
tasks:
- name: Find ARN
shell: aws acm list-certificates --output text --certificate-statuses ISSUED | grep {{ apache[item].domain }} | awk '{print $2}'
args:
executable: /bin/bash
with_items:
- "clientA"
- "clientB"
- "clientC"
register: sslarn
- debug: msg={{ item.stdout_lines }}
with_items:
- "{{ sslarn.results }}"
- name: Remake the dictionary assemble files
template:
src: /file/path/to/templates/update-dictionary-template.j2
dest: /file/path/to/dictionary/files/{{ item }}.yml
mode: 0644
with_items:
- "clientA"
- "clientB"
- "clientC"
- name: Assemble the dictionary
assemble:
ignore_hidden: yes
src: /file/path/to/dictionary/files/
dest: /file/path/to/dictionary/dictionary.yml
I'm able to find the ARN without any issues in the first task. However, when I try to use debug to figure out how to call the ARN per client for the template task I'm unable to figure out how to only get the ARN for the specific client out of the registered results.
答案1
得分: 1
以下是您要翻译的部分:
无论在迭代中使用哪个模块,列表 results 的项始终包括属性 item。使用方便的模式 dict(keys|zip(values))
并创建一个字典,其中 keys 是属性 items 的列表,values 是列表 results。如果您注册了变量 sslarn,请声明该字典
sslarn_dict: "{{ dict(sslarn.results|map(attribute='item')|
zip(sslarn.results)) }}"
例如,给定以下客户端
clients: [clientA, clientB, clientC]
,仅在本地主机上运行,以下迭代
- command: "echo certificate-statuses {{ item }}"
loop: "{{ clients }}"
register: sslarn
给出
<sup>
sslarn_dict:
clientA:
ansible_facts: {discovered_interpreter_python: /usr/bin/python3}
ansible_loop_var: item
changed: true
cmd: [echo, certificate-statuses, clientA]
delta: '0:00:00.003085'
end: '2023-07-11 06:55:46.144448'
failed: false
invocation:
module_args: {_raw_params: echo certificate-statuses clientA, _uses_shell: false,
argv: null, chdir: null, creates: null, executable: null, removes: null, stdin: null,
stdin_add_newline: true, strip_empty_ends: true}
item: clientA
msg: ''
rc: 0
start: '2023-07-11 06:55:46.141363'
stderr: ''
stderr_lines: []
stdout: certificate-statuses clientA
stdout_lines: [certificate-statuses clientA]
clientB:
ansible_loop_var: item
changed: true
cmd: [echo, certificate-statuses, clientB]
delta: '0:00:00.003012'
end: '2023-07-11 06:55:46.367788'
failed: false
invocation:
module_args: {_raw_params: echo certificate-statuses clientB, _uses_shell: false,
argv: null, chdir: null, creates: null, executable: null, removes: null, stdin: null,
stdin_add_newline: true, strip_empty_ends: true}
item: clientB
msg: ''
rc: 0
start: '2023-07-11 06:55:46.364776'
stderr: ''
stderr_lines: []
stdout: certificate-statuses clientB
stdout_lines: [certificate-statuses clientB]
clientC:
ansible_loop_var: item
changed: true
cmd: [echo, certificate-statuses, clientC]
delta: '0:00:00.003018'
end: '2023-07-11 06:55:46.584018'
failed: false
invocation:
module_args: {_raw_params: echo certificate-statuses clientC, _uses_shell: false,
argv: null, chdir: null, creates: null, executable: null, removes: null, stdin: null,
stdin_add_newline: true, strip_empty_ends: true}
item: clientC
msg: ''
rc: 0
start: '2023-07-11 06:55:46.581000'
stderr: ''
stderr_lines: []
stdout: certificate-statuses clientC
stdout_lines: [certificate-statuses clientC]
</sup>
<hr>
现在,您可以在模板中使用字典。例如,
shell> cat update-dictionary-template.j2
{{ item }}: {{ sslarn_dict[item]['stdout'] }}
- template:
src: update-dictionary-template.j2
dest: /tmp/files/{{ item }}.yml
loop: "{{ clients }}"
将创建文件
shell> tree /tmp/files/
/tmp/files/
├── clientA.yml
├── clientB.yml
└── clientC.yml
shell> cat /tmp/files/clientA.yml
clientA: certificate-statuses clientA
shell> cat /tmp/files/clientB.yml
clientB: certificate-statuses clientB
shell> cat /tmp/files/clientC.yml
clientC: certificate-statuses clientC
您可以组装文件 /tmp/dictionary.yml
- assemble:
src: /tmp/files
dest: /tmp/dictionary.yml
给出
shell> cat /tmp/dictionary.yml
clientA: certificate-statuses clientA
clientB: certificate-statuses clientB
clientC: certificate-statuses clientC
但是,您可以在不组装中间文件的情况下获得相同的内容。以下模板提供了相同的结果
- debug:
msg: |
{% for client in clients %}
{{ client }}: {{ sslarn_dict[client]['stdout'] }}
{% endfor %}
但是,最简单的方法是仅从选定的属性创建字典
client_stdout: "{{ sslarn.results|
items2dict(key_name='item', value_name='stdout') }}"
给出
client_stdout:
clientA: certificate-statuses clientA
clientB: certificate-statuses clientB
clientC: certificate-statuses clientC
然后,模板很简单
- debug:
msg: |
{{ client_stdout }}
给出
msg:
clientA: certificate-statuses clientA
clientB: certificate-statuses clientB
clientC: certificate-statuses clientC
<hr>
<sup>
完整测试的示例剧本
- hosts: localhost
vars:
clients: [clientA, clientB, clientC]
sslarn_dict: "{{ dict(sslarn.results|
<details>
<summary>英文:</summary>
Whatever module is used in an iteration the items of the list *results* always comprise the attribute *item*. Use the handy pattern `dict(keys|zip(values))` and create a dictionary where the *keys* is the list of the attribute *items* and *values* is the list *results*. If you register the variable *sslarn* declare the dictionary
```yaml
sslarn_dict: "{{ dict(sslarn.results|map(attribute='item')|
zip(sslarn.results)) }}"
For example, given the clients
clients: [clientA, clientB, clientC]
, running on the localhost only, the below iteration
- command: "echo certificate-statuses {{ item }}"
loop: "{{ clients }}"
register: sslarn
gives
<sup>
sslarn_dict:
clientA:
ansible_facts: {discovered_interpreter_python: /usr/bin/python3}
ansible_loop_var: item
changed: true
cmd: [echo, certificate-statuses, clientA]
delta: '0:00:00.003085'
end: '2023-07-11 06:55:46.144448'
failed: false
invocation:
module_args: {_raw_params: echo certificate-statuses clientA, _uses_shell: false,
argv: null, chdir: null, creates: null, executable: null, removes: null, stdin: null,
stdin_add_newline: true, strip_empty_ends: true}
item: clientA
msg: ''
rc: 0
start: '2023-07-11 06:55:46.141363'
stderr: ''
stderr_lines: []
stdout: certificate-statuses clientA
stdout_lines: [certificate-statuses clientA]
clientB:
ansible_loop_var: item
changed: true
cmd: [echo, certificate-statuses, clientB]
delta: '0:00:00.003012'
end: '2023-07-11 06:55:46.367788'
failed: false
invocation:
module_args: {_raw_params: echo certificate-statuses clientB, _uses_shell: false,
argv: null, chdir: null, creates: null, executable: null, removes: null, stdin: null,
stdin_add_newline: true, strip_empty_ends: true}
item: clientB
msg: ''
rc: 0
start: '2023-07-11 06:55:46.364776'
stderr: ''
stderr_lines: []
stdout: certificate-statuses clientB
stdout_lines: [certificate-statuses clientB]
clientC:
ansible_loop_var: item
changed: true
cmd: [echo, certificate-statuses, clientC]
delta: '0:00:00.003018'
end: '2023-07-11 06:55:46.584018'
failed: false
invocation:
module_args: {_raw_params: echo certificate-statuses clientC, _uses_shell: false,
argv: null, chdir: null, creates: null, executable: null, removes: null, stdin: null,
stdin_add_newline: true, strip_empty_ends: true}
item: clientC
msg: ''
rc: 0
start: '2023-07-11 06:55:46.581000'
stderr: ''
stderr_lines: []
stdout: certificate-statuses clientC
stdout_lines: [certificate-statuses clientC]
</sup>
<hr>
Now, you can use the dictionary in a template. For example,
shell> cat update-dictionary-template.j2
{{ item }}: {{ sslarn_dict[item]['stdout'] }}
- template:
src: update-dictionary-template.j2
dest: /tmp/files/{{ item }}.yml
loop: "{{ clients }}"
will create files
shell> tree /tmp/files/
/tmp/files/
├── clientA.yml
├── clientB.yml
└── clientC.yml
shell> cat /tmp/files/clientA.yml
clientA: certificate-statuses clientA
shell> cat /tmp/files/clientB.yml
clientB: certificate-statuses clientB
shell> cat /tmp/files/clientC.yml
clientC: certificate-statuses clientC
You can assemble the file /tmp/dictionary.yml
- assemble:
src: /tmp/files
dest: /tmp/dictionary.yml
gives
shell> cat /tmp/dictionary.yml
clientA: certificate-statuses clientA
clientB: certificate-statuses clientB
clientC: certificate-statuses clientC
However, you can get the same content without assembling the intermediate files. The template below gives the same result
- debug:
msg: |
{% for client in clients %}
{{ client }}: {{ sslarn_dict[client]['stdout'] }}
{% endfor %}
But, the easiest way is to create a dictionary from selected attributes only
client_stdout: "{{ sslarn.results|
items2dict(key_name='item', value_name='stdout') }}"
gives
client_stdout:
clientA: certificate-statuses clientA
clientB: certificate-statuses clientB
clientC: certificate-statuses clientC
Then, the template is trivial
- debug:
msg: |
{{ client_stdout }}
gives
msg:
clientA: certificate-statuses clientA
clientB: certificate-statuses clientB
clientC: certificate-statuses clientC
<hr>
<sup>
Example of a complete playbook for testing
- hosts: localhost
vars:
clients: [clientA, clientB, clientC]
sslarn_dict: "{{ dict(sslarn.results|map(attribute='item')|
zip(sslarn.results)) }}"
client_stdout: "{{ sslarn.results|
items2dict(key_name='item', value_name='stdout') }}"
tasks:
- command: "echo certificate-statuses {{ item }}"
loop: "{{ clients }}"
register: sslarn
- debug:
var: sslarn_dict|to_yaml
- template:
src: update-dictionary-template.j2
dest: /tmp/files/{{ item }}.yml
loop: "{{ clients }}"
- assemble:
src: /tmp/files
dest: /tmp/dictionary.yml
- debug:
msg: |
{% for client in clients %}
{{ client }}: {{ sslarn_dict[client]['stdout'] }}
{% endfor %}
- debug:
var: client_stdout
- debug:
msg: |
{{ client_stdout }}
</sup>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论