如何在Ansible中处理此结果

时间:2019-03-18 10:37:07

标签: ansible

在Ansible中处理数据时遇到问题。 我的主要目标是清除ADRCI日志。

第一次,我列出了使用外壳模块向用户Oracle和Grid启动的数据库

- name: Generate List of databases started with user oracle
  shell: "ps -ef | grep smon | grep -v grep | grep oracle | awk '{print $NF}' | awk -F '_' '{print $NF}'"
  register: list_oracle_bases

然后我想用其他shell命令清除房屋:

- name: Get databases homes
      register: get_homes
      environment:
        ORACLE_SID: "{{ item }}"
        ORAENV_ASK: "NO"
      become: true
      become_user: oracle
      become_method: su
      shell: ". oraenv 1>/dev/null 2>&- && adrci exec='show homes' | grep -v 'ADR Homes:'"
      with_items:
        - "{{ list_oracle_bases.stdout_lines }}"

我的问题来了,我得到了{{get_homes}}

的结果
ok: [host1] => {
    "msg": {
        "changed": true,
        "msg": "All items completed",
        "results": [
            {
                "_ansible_ignore_errors": null,
                "_ansible_item_label": "ANSIBLE1",
                "_ansible_item_result": true,
                "_ansible_no_log": false,
                "_ansible_parsed": true,
                "changed": true,
                "cmd": ". oraenv 1>/dev/null 2>&- && adrci exec='show homes' | grep -v 'ADR Homes:'",
                "delta": "0:00:00.028970",
                "end": "2019-03-18 11:29:29.708709",
                "failed": false,
                "invocation": {
                    "module_args": {
                        "_raw_params": ". oraenv 1>/dev/null 2>&- && adrci exec='show homes' | grep -v 'ADR Homes:'",
                        "_uses_shell": true,
                        "argv": null,
                        "chdir": null,
                        "creates": null,
                        "executable": null,
                        "removes": null,
                        "stdin": null,
                        "warn": true
                    }
                },
                "item": "ANSIBLE1",
                "rc": 0,
                "start": "2019-03-18 11:29:29.679739",
                "stderr": "",
                "stderr_lines": [],
                "stdout": "diag/rdbms/ansible1/ANSIBLE1",
                "stdout_lines": [
                    "diag/rdbms/ansible1/ANSIBLE1"
                ]
            }
        ]
    }
}

我试图获取get_homes.results.stdout_lines,但是当我想在调试语句中显示它时出现以下错误。

FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'list object' has no attribute 'stdout_lines'\n\nThe error appears to have been in '/tmp/sylvain/roles/test/tasks/query.yml': line 59, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n    - debug:\n      ^ here\n"}

在变量中获取该stdout_lines(“ diag / rdbms / ansible1 / ANSIBLE1”)的正确方法是什么。

致谢

西尔万

1 个答案:

答案 0 :(得分:0)

results是一个数组,因为您是在循环(with_items)中创建它的。正确的语法是:

get_homes.results[0].stdout_lines

第一个,其余的,等等。

为了获得主目录列表,您必须通过所需的属性过滤results数组。最后,您必须flatten个数组数组。这是mcve

---
- name: filter
  hosts: localhost
  connection: local
  tasks:

    - shell: |-
        echo ~{{ item }}
      with_items:
        - man
        - lp
        - mail
      register: get_homes
      ignore_errors: true
      changed_when: false

    - debug: var=get_homes

    - set_fact:
        homes: >-
          {{ get_homes.results | map(attribute='stdout_lines') | list | flatten }}

    - debug: var=homes