ansible变量参考:ssh和命令后无法从寄存器变量读取stdout_lines

时间:2019-10-24 23:26:21

标签: ansible ansible-2.x ansible-facts

团队, 我的任务正在运行,她将在从注册变量中提取的主机上执行命令。目前有两台主机,但是将有100台主机投入生产。 我无法读出stdout或stdout_lines。我的任务和输出如下。将其切入删除服务器,然后运行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 on\n/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 on\n/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"
                ]
            }
        ]
    }

从上面的输出中

无法读取标准输出行以验证安装点。

任务:

      - 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

不输出任何内容。

1 个答案:

答案 0 :(得分:1)

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

例如,您的任务是否有机会在不同的主机上运行?每个主机都会注册“ 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