首页 > 解决方案 > ansible 变量参考:ssh 和命令后无法从寄存器变量中读取 stdout_lines

问题描述

团队,我的任务正在运行,她将在从注册变量中提取的主机上执行命令。目前有两个主机,但生产中将有 100 个。我无法读出标准输出或标准输出线。我的任务和输出如下。它连接到删除服务器,然后运行 ​​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

不输出任何东西..

标签: ansibleansible-2.xansible-facts

解决方案


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

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


推荐阅读