ansible变量引用:在ssh和命令之后,无法从寄存器变量读取stdout_lines



团队,我的任务是在从注册变量中提取的主机上运行she-well命令。目前有两台主机,但将有100台正在生产中。我无法读取stdout或stdout_lines。我的任务和输出如下。它将sshing发送到remove服务器,然后运行df-h命令并存储输出。

实际输出(去掉一些文本,但没有任何括号(

ok: [localhost] => {
"raid_info.results": [
{
"ansible_loop_var": "item", 
"changed": true, 
"cmd": "ssh -F /home/svcngcctal.net "df -kh /raid/"", 
"delta": "0:00:02.095839", 
"end": "2019-10-24 22:55:38.323679", 
"failed": false, 
"failed_when_result": false, 
"invocation": {
"module_args": {
"_raw_params": "ssh -F /home/metal.net "df -kh /raid/"", 
"_uses_shell": true, 
"warn": true
}
}, 
"item": {
"nodeType": "4.15.0-45-generic", 
"node_name": "hostB"
}, 
"rc": 0, 
"start": "2019-10-24 22:55:36.227840", 
"stderr": "Warning: Permanently***", 
"stderr_lines": [
"Warning:asd"
], 
"stdout": "Filesystem      Size  Used Avail Use% Mounted onn/dev/sdb1       7.0T  175G  6.5T   3% /raid", 
"stdout_lines": [
"Filesystem      Size  Used Avail Use% Mounted on", 
"/dev/sdb1       7.0T  175G  6.5T   3% /raid"
]
}, 
{
"ansible_loop_var": "item", 
"changed": true, 
"cmd": "ssh -F /home/svcngcc/jenkinstal.net "df -kh /raid/"", 
"delta": "0:00:02.115591",
"invocation": {
"module_args": {
"_raw_params": "ssh -F /home/sal.net "df -kh /raid/"", 
"warn": true
}
}, 
"item": {
"nodeType": "4.15.0-45-generic", 
"node_name": "hostA"
}, 
"rc": 0, 
"start": "2019-10-24 22:55:38.467007", ", 
"stderr_lines": [
"Warning: Permanently "
], 
"stdout": "Filesystem      Size  Used Avail Use% Mounted onn/dev/sdb1       7.0T  176G  6.5T   3% /raid", 
"stdout_lines": [
"Filesystem      Size  Used Avail Use% Mounted on", 
"/dev/sdb1       7.0T  176G  6.5T   3% /raid"
]
}
]
}

从上面的输出中,我无法读取stdout行来验证装载点。。

任务:

- name: "RAID mount check for fscache on GPU Nodes"
shell: ssh -F {{ ssh_cfg_path.stdout }} {{ item.node_name }}.{{ ssh_host }} "df -kh /raid/"
ignore_errors: no
register: raid_info
failed_when: raid_info.rc != 0
with_items: "{{ gpu_nodes }}"
- name: raid_info results1_stdout_lines
debug:
var: raid_info.results[1].stdout_lines

不输出任何内容。。

我无法重现您的结果-我相信您在发布时遗漏了一些信息。

例如,您的任务是否在不同的主机上运行?"raid_info"变量是按主机注册的,因此如果您的任务在不同的主机上运行,这可能会导致您的问题。

我的测试,有一个类似的循环:-playbook.yml

---
- hosts: localhost
gather_facts: false
vars_files:
- packages.yml
tasks:
- name: Run a command
command: "echo {{item}}"
register: my_output
loop: 
- 10.10.80.193
- 10.10.80.194
- name: Print the results
debug:
var: my_output
- name: Print only the second item in the list
debug:
var: my_output.results[1].stdout_lines
  • 结果:
# ansible-playbook -i inventory.yml playbook.yml 
PLAY [localhost] *************************************************************************************************************************
TASK [Run a command] *********************************************************************************************************************
changed: [localhost] => (item=10.10.80.193)
changed: [localhost] => (item=10.10.80.194)
TASK [Print the results] *****************************************************************************************************************
ok: [localhost] => {
"my_output": {
"changed": true, 
"msg": "All items completed", 
"results": [ 
{ <snip>
}, 
{ <snip>
}
]
}
}
TASK [Print only the second item in the list] ********************************************************************************************
ok: [localhost] => {
"my_output.results[1].stdout_lines": [
"10.10.80.194"
]
}
PLAY RECAP *******************************************************************************************************************************
localhost                  : ok=3    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0