Ansible,获取多个寄存器命令的结果时出错
Ansible, error in getting results for multiple register command
我尝试得到如下结果:
- name: Populate known_hosts
hosts: spectrum_scale
gather_facts: no
tags: set_known_hosts
become: true
tasks:
- name: Get hosts list from known_host
ansible.builtin.shell:
cmd: "cat /root/.ssh/known_hosts |
grep {{ hostvars[item].ansible_fqdn }}"
with_items: "{{groups['spectrum_scale']}}"
ignore_errors: yes
register: hosts2add
- name: Scan for SSH client keys
ansible.builtin.shell:
cmd: "ssh-keyscan {{ hostvars[h2a_r.item].ansible_fqdn }},
{{ hostvars[h2a_r.item].ansible_default_ipv4.address }}
2>/dev/null"
loop: "{{ hosts2add.results }}"
loop_control:
loop_var: h2a_r
when: h2a_r.rc
register: ssh_scan
- name: Display debug
ansible.builtin.debug:
msg: "FINGERPRINT TO ADD {{ ss_r.stdout_lines }}"
# var: ss_r.stdout_lines
loop: "{{ ssh_scan.results }}"
loop_control:
loop_var: ss_r
我收到错误:
{"msg": "The task includes an option with an undefined variable. The error was: 'dict object' has no attribute 'stdout_lines'
真正奇怪的是,如果使用 var 而不是 msg,我得到了我需要的值
var: ss_r.stdout_lines
我找到了一个旧的 post (),但建议的解决方案似乎对我不起作用
感谢您的帮助
我不确定你在找什么,但 result
是一个包含你存储的所有值的字典。
如果你要使用 loop
for 循环你需要传递 dict2items
例如:
- name: Display debug
ansible.builtin.debug:
msg: "FINGERPRINT TO ADD {{ ss_r.stdout_lines }}"
# var: ss_r.stdout_lines
loop: "{{ ssh_scan.results | dict2items }}"
loop_control:
loop_var: ss_r
可能会解决您之前所说的错误消息。
如果您已经在 known_hosts
文件中拥有所有主机,则上面的代码有效。在这种情况下,总是执行命令并定义变量。
如果你的 known_hosts
是空的,而你 运行 "Display debug" printing var 将会有如下输出:
"ss_r.stdout_lines": "VARIABLE IS NOT DEFINED!"
因此,为了使任务失败,您需要指定:
when: ss_r.stdout_lines is defined
为了完整起见,这里有完整的任务:
- name: Display debug
ansible.builtin.debug:
msg: "FINGERPRINT TO ADD {{ ss_r.stdout_lines }}"
loop: "{{ ssh_scan.results }}"
loop_control:
loop_var: ss_r
when: ss_r.stdout_lines is defined
我尝试得到如下结果:
- name: Populate known_hosts
hosts: spectrum_scale
gather_facts: no
tags: set_known_hosts
become: true
tasks:
- name: Get hosts list from known_host
ansible.builtin.shell:
cmd: "cat /root/.ssh/known_hosts |
grep {{ hostvars[item].ansible_fqdn }}"
with_items: "{{groups['spectrum_scale']}}"
ignore_errors: yes
register: hosts2add
- name: Scan for SSH client keys
ansible.builtin.shell:
cmd: "ssh-keyscan {{ hostvars[h2a_r.item].ansible_fqdn }},
{{ hostvars[h2a_r.item].ansible_default_ipv4.address }}
2>/dev/null"
loop: "{{ hosts2add.results }}"
loop_control:
loop_var: h2a_r
when: h2a_r.rc
register: ssh_scan
- name: Display debug
ansible.builtin.debug:
msg: "FINGERPRINT TO ADD {{ ss_r.stdout_lines }}"
# var: ss_r.stdout_lines
loop: "{{ ssh_scan.results }}"
loop_control:
loop_var: ss_r
我收到错误:
{"msg": "The task includes an option with an undefined variable. The error was: 'dict object' has no attribute 'stdout_lines'
真正奇怪的是,如果使用 var 而不是 msg,我得到了我需要的值
var: ss_r.stdout_lines
我找到了一个旧的 post (
感谢您的帮助
我不确定你在找什么,但 result
是一个包含你存储的所有值的字典。
如果你要使用 loop
for 循环你需要传递 dict2items
例如:
- name: Display debug
ansible.builtin.debug:
msg: "FINGERPRINT TO ADD {{ ss_r.stdout_lines }}"
# var: ss_r.stdout_lines
loop: "{{ ssh_scan.results | dict2items }}"
loop_control:
loop_var: ss_r
可能会解决您之前所说的错误消息。
如果您已经在 known_hosts
文件中拥有所有主机,则上面的代码有效。在这种情况下,总是执行命令并定义变量。
如果你的 known_hosts
是空的,而你 运行 "Display debug" printing var 将会有如下输出:
"ss_r.stdout_lines": "VARIABLE IS NOT DEFINED!"
因此,为了使任务失败,您需要指定:
when: ss_r.stdout_lines is defined
为了完整起见,这里有完整的任务:
- name: Display debug
ansible.builtin.debug:
msg: "FINGERPRINT TO ADD {{ ss_r.stdout_lines }}"
loop: "{{ ssh_scan.results }}"
loop_control:
loop_var: ss_r
when: ss_r.stdout_lines is defined