如何在 Ansible playbook 中简洁地提取或合并哈希值

发布于 2025-01-09 20:42:32 字数 1484 浏览 1 评论 0原文

为了简化许多简单情况下的打印 shell 输出,我想要一个快速的 Ansible Playbook 片段来仅显示 stdout_linesstderr_lines - 但仍然保持它们不同。

- name: demo output streams
  shell: "echo hi stdout; echo ho stderr 1>&2; false; true"
  register: tout

- debug:
    var: tout

给出这样的:

TASK [debug] **************************************************************
ok: [remote] => {
    "tout": {
        "changed": true,
        "cmd": "echo hi stdout; echo ho stderr 1>&2; false; true",
        "delta": "0:00:00.011020",
        "end": "2022-02-24 16:19:25.812732",
        "failed": false,
        "msg": "",
        "rc": 0,
        "start": "2022-02-24 16:19:25.801712",
        "stderr": "ho stderr",
        "stderr_lines": [
            "ho stderr"
        ],
        "stdout": "hi stdout",
        "stdout_lines": [
            "hi stdout"
        ]
    }
}

我想要的是像这样更简洁的东西,或者可能是拆分的,但仍然由 Playbook 中的单个调试任务引用。 (集成到 shell 任务本身会更理想,但我想这是不可能的。)

TASK [debug] **************************************************************
ok: [remote] => {
    "tout": {
        "stderr_lines": [
            "ho stderr"
        ],
        "stdout_lines": [
            "hi stdout"
        ]
    }
}

falsetrue 的绒毛只是表明我只是想看看命令输出,无需 Ansible 担心成功或失败。

当然,我可以使用多个调试,但是当存在许多此类快速信息任务时,该输出会变得很麻烦。

To simplify printing shell output in many simple situations, I'd like a quick Ansible Playbook snippet to show just the stdout_lines and stderr_lines — but still keeping them distinct.

- name: demo output streams
  shell: "echo hi stdout; echo ho stderr 1>&2; false; true"
  register: tout

- debug:
    var: tout

gives this:

TASK [debug] **************************************************************
ok: [remote] => {
    "tout": {
        "changed": true,
        "cmd": "echo hi stdout; echo ho stderr 1>&2; false; true",
        "delta": "0:00:00.011020",
        "end": "2022-02-24 16:19:25.812732",
        "failed": false,
        "msg": "",
        "rc": 0,
        "start": "2022-02-24 16:19:25.801712",
        "stderr": "ho stderr",
        "stderr_lines": [
            "ho stderr"
        ],
        "stdout": "hi stdout",
        "stdout_lines": [
            "hi stdout"
        ]
    }
}

What I want is something more concise like this, or perhaps split, but still referenced by a single debug task in the Playbook. (Integrated into the shell task itself would be even more ideal, but I guess that's off the table.)

TASK [debug] **************************************************************
ok: [remote] => {
    "tout": {
        "stderr_lines": [
            "ho stderr"
        ],
        "stdout_lines": [
            "hi stdout"
        ]
    }
}

The false and true fluff just indicates that I simply want to see the command outputs, without Ansible-worrying about success or failure.

Of course I could use multiple debug's, but that output becomes cumbersome when there are many of these quick-info tasks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

别念他 2025-01-16 20:42:32

你可以做这样的事情:

- name: demo output streams
  shell: "echo hi stdout; echo ho stderr 1>&2; false; true"
  register: tout

- debug:
    var: result
  vars: 
    result: "{{ {'tout': [{'stdout_lines': tout.stdout_lines}, {'stderr_lines': tout.stderr_lines}]} }}"

或者

- debug:
    var: result
  vars: 
    result: "{{ {'tout': { 'stdout_lines': tout.stdout_lines, 'stderr_lines': tout.stderr_lines} } }}"

如果你想要一些易于阅读的东西,创建一个自定义插件:(这里只有std)

#!/usr/bin/python
class FilterModule(object):
    def filters(self):
        return {
            'onlystd': self.onlystd
        }
 
    def onlystd(self, out, name = ''):
        result= { 'stdout_lines': out['stdout_lines'], 'stderr_lines': out['stderr_lines'] }
        if name != '':
            result= { name: result }
        
        return result

剧本来使用它的

- name: "make this working"
  hosts: localhost
  vars:

  tasks:
    - name: demo output streams
      shell: "echo hi stdout; echo ho stderr 1>&2; false; true"
      register: tout

    - debug:
        var: tout | onlystd('tout') #if you want a name

    - debug:
        var: tout | onlystd   #without name

结果:

ok: [localhost] => {
    "tout | onlystd('tout')": {
        "tout": {
            "stderr_lines": [
                "ho stderr"
            ],
            "stdout_lines": [
                "hi stdout"
            ]
        }
    }
}

ok: [localhost] => {
    "tout | onlystd": {
        "stderr_lines": [
            "ho stderr"
        ],
        "stdout_lines": [
            "hi stdout"
        ]
    }
}

you could do something like this:

- name: demo output streams
  shell: "echo hi stdout; echo ho stderr 1>&2; false; true"
  register: tout

- debug:
    var: result
  vars: 
    result: "{{ {'tout': [{'stdout_lines': tout.stdout_lines}, {'stderr_lines': tout.stderr_lines}]} }}"

or using

- debug:
    var: result
  vars: 
    result: "{{ {'tout': { 'stdout_lines': tout.stdout_lines, 'stderr_lines': tout.stderr_lines} } }}"

if you want something easy to read, create a custom plugin: (here onlystd)

#!/usr/bin/python
class FilterModule(object):
    def filters(self):
        return {
            'onlystd': self.onlystd
        }
 
    def onlystd(self, out, name = ''):
        result= { 'stdout_lines': out['stdout_lines'], 'stderr_lines': out['stderr_lines'] }
        if name != '':
            result= { name: result }
        
        return result

playbook to use it

- name: "make this working"
  hosts: localhost
  vars:

  tasks:
    - name: demo output streams
      shell: "echo hi stdout; echo ho stderr 1>&2; false; true"
      register: tout

    - debug:
        var: tout | onlystd('tout') #if you want a name

    - debug:
        var: tout | onlystd   #without name

results:

ok: [localhost] => {
    "tout | onlystd('tout')": {
        "tout": {
            "stderr_lines": [
                "ho stderr"
            ],
            "stdout_lines": [
                "hi stdout"
            ]
        }
    }
}

ok: [localhost] => {
    "tout | onlystd": {
        "stderr_lines": [
            "ho stderr"
        ],
        "stdout_lines": [
            "hi stdout"
        ]
    }
}
情域 2025-01-16 20:42:32

使用 selectattr过滤由过滤器 dict2items在你的字典上。然后使用 items2dict

鉴于:

- name: demo output streams
  shell: "echo hi stdout; echo ho stderr 1>&2; false; true"
  register: tout

- debug:
    var: tout | dict2items | selectattr('key', 'in', _keys_filter) | items2dict
  vars:
    _keys_filter:
      - stdout_lines
      - stderr_lines

这将产生:

TASK [demo output streams] ***************************************************
changed: [localhost]

TASK [debug] *****************************************************************
ok: [localhost] => 
  tout | dict2items | selectattr('key', 'in', _keys_filter) | items2dict:
    stderr_lines:
    - ho stderr
    stdout_lines:
    - hi stdout

Use selectattr to filter the keys given by the result of the filter dict2items on your dictionary. Then swap it back to a dictionary with items2dict.

Given:

- name: demo output streams
  shell: "echo hi stdout; echo ho stderr 1>&2; false; true"
  register: tout

- debug:
    var: tout | dict2items | selectattr('key', 'in', _keys_filter) | items2dict
  vars:
    _keys_filter:
      - stdout_lines
      - stderr_lines

This would yield:

TASK [demo output streams] ***************************************************
changed: [localhost]

TASK [debug] *****************************************************************
ok: [localhost] => 
  tout | dict2items | selectattr('key', 'in', _keys_filter) | items2dict:
    stderr_lines:
    - ho stderr
    stdout_lines:
    - hi stdout
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文