使用Ansible时访问stdout_lines [英] Access to stdout_lines when using Ansible

查看:253
本文介绍了使用Ansible时访问stdout_lines的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

鉴于我已使用var sonarqube_plugins_installed中的shell任务在目录中注册"了文件,

Given I have "registered" files in directory using shell task in a var sonarqube_plugins_installed, when I "debug" this using

- name: Debug
  debug:
    var: sonarqube_plugins_installed.results 

例如,我看到

TASK [sonarqube : Debug] ************************************************************************
ok: [sonarqube] => {
    "sonarqube_plugins_installed.results": [
        {
            "ansible_loop_var": "item", 
            "changed": true, 
            "cmd": "ls  /opt/sonarqube/sonarqube-6.7/extensions/plugins/sonar-build-breaker-plugin-*.jar", 
            "delta": "0:00:00.003748", 
            "end": "2019-09-18 04:04:54.355667", 
            "failed": false, 
            "invocation": {
                "module_args": {
                    "_raw_params": "ls  /opt/sonarqube/sonarqube-6.7/extensions/plugins/sonar-build-breaker-plugin-*.jar", 
                    "_uses_shell": true, 
                    "argv": null, 
                    "chdir": null, 
                    "creates": null, 
                    "executable": null, 
                    "removes": null, 
                    "stdin": null, 
                    "stdin_add_newline": true, 
                    "strip_empty_ends": true, 
                    "warn": true
                }
            }, 
            "item": "build_breaker", 
            "rc": 0, 
            "start": "2019-09-18 04:04:54.351919", 
            "stderr": "", 
            "stderr_lines": [], 
            "stdout": "/opt/sonarqube/sonarqube-6.7/extensions/plugins/sonar-build-breaker-plugin-2.2.jar", 
            "stdout_lines": [
                "/opt/sonarqube/sonarqube-6.7/extensions/plugins/sonar-build-breaker-plugin-2.2.jar"
            ]
        }
    ]
}

如何在另一个任务中访问例如stdout?在另一个任务中,我想使用with_items处理results中的每个项目并输出stdout.

How can I access for example stdout in another task? In another task I want to process each item in results using with_items and output stdout.

这怎么办?

推荐答案

以下应该是一个好的开始:

The following should be a good start:

- name: show stdout for each result
  debug:
    var: item.stdout
  loop: "{{ sonarqube_plugins_installed.results }}"

您猜到了,这已经遍及了results列表中的每个元素.

As you guessed, this is going trough each elements in your results list.

如果要一次打印所有行,另一种方法是通过 jmespath 表达式仅提取所需的信息

An other approach if your want to print all lines at once would be to process the data structure through json_query with the relevant jmespath expression to only extract the needed information

- name: show sdtout list for all results
  debug:
    msg: "{{ sonarqube_plugins_installed | json_query('results[].stdout[]') }}"

您还可以将两者结合起来,并仅在简单的stdout列表上循环

You could also combine the two and loop over the simple list of stdouts only

- name: show stdout for each result
  debug:
    var: item
  loop: "{{ sonarqube_plugins_installed | json_query('results[].stdout[]') }}"

您还可以使用 map 过滤器提取所需的属性:

You could also use the map filter to extract the desired attribute:

- name: show stdout list of all results
  debug:
    msg: "{{ sonarqube_plugins_installed.results | map(attribute='stdout') | list }}"

还有其他可以使用的解决方案,我相信您会在常规jinja2过滤器文档

And there other solutions you can use that I'm sure you will find in the specific ansible filters and general jinja2 filters documentations

这篇关于使用Ansible时访问stdout_lines的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆