【问题标题】:ansible variable reference : unable to read stdout_lines from register variable after ssh and commandansible 变量参考:ssh 和命令后无法从寄存器变量中读取 stdout_lines
【发布时间】:2019-10-24 23:26:21
【问题描述】:

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

不输出任何东西..

【问题讨论】:

  • 该输出已被修改 - 有许多错误使其无效。你能显示- debug: var=raid_info.results的实际输出吗?
  • 你是对的:为简洁起见,我这样做了。现在用等括号更新了输出。但是,我确实删除了不必要的信息。每个括号 9 个。 ][ 和 }{

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


【解决方案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   

【讨论】:

  • 我的任务是在从注册变量中提取的主机上运行 shell 命令。目前有两个主机,但生产中将有 100 个。
  • 我的示例也是如此 - 它在主机(在本例中为 localhost)上运行 shell 命令。如果您在多台主机上运行任务,请注意已注册的变量是按主机注册的(如我上面的回答中所述)。因此,您需要从正确的主机获取结果(通过在同一主机上运行第二个任务,或使用hostvars[]
  • 您能否详细说明您的答案,那么在我的情况下,在所有主机上运行 shell 命令时,我的任务会如何?
  • 太棒了!。我必须像您在任务中所做的那样在 raid_info 上设置调试,然后我才能得到它。但一个问题是我应该如何管理这个规模?因为我将拥有 100 个节点。我不能打印调试 100 次。我可以循环调试吗?
  • 调试只为您(和我)提供,因此在故障排除期间我们可以看到变量及其内容。对于生产,您将使用执行实际操作的模块替换 debug 模块:)。而回答最后一个问题,是的,当然——debug 只是另一个模块,你可以在使用时循环使用它就好了。
猜你喜欢
  • 2020-01-08
  • 2015-11-15
  • 2011-06-29
  • 1970-01-01
  • 2012-10-06
  • 1970-01-01
  • 1970-01-01
  • 2021-05-11
  • 1970-01-01
相关资源
最近更新 更多