0

I have a task

- name: DELEGATED ADMIN ACCOUNTS - check, get and send to the file domain.list
  shell: /opt/zimbra/bin/zmprov -l gaaa -v zimbraIsDelegatedAdminAccount

and after this task I got output

changed: [Shrrah] => {
    "changed": true,
    "cmd": [
        "sh",
        "/home/information_domain.sh"
    ],
    "delta": "0:00:02.495922",
    "end": "2022-03-29 10:25:16.936051",
    "invocation": {
        "module_args": {
            "_raw_params": "sh /home/information_domain.sh",
            "_uses_shell": false,
            "argv": null,
            "chdir": null,
            "creates": null,
            "executable": null,
            "removes": null,
            "stdin": null,
            "stdin_add_newline": true,
            "strip_empty_ends": true,
            "warn": false
        }
    },
    "msg": "",
    "rc": 0,
    "start": "2022-03-29 10:25:14.440129",
    "stderr": "",
    "stderr_lines": [],
    "stdout": "# name admin@shrrah.esquimail.com\nzimbraIsDelegatedAdminAccount: FALSE\n\n# name prueba5@prueba5.com\n\n# name prueba7@prueba7.com\nzimbraIsDelegatedAdminAccount: TRUE\n\n# name prueba9@prueba9.com",
    "stdout_lines": [
        "# name admin@shrrah.esquimail.com",
        "zimbraIsDelegatedAdminAccount: FALSE",
        "",
        "# name prueba5@prueba5.com",
        "",
        "# name prueba7@prueba7.com",
        "zimbraIsDelegatedAdminAccount: TRUE",
        "",
        "# name prueba9@prueba9.com"
    ]
}

I need to get data with n# name prueba7@prueba7.com\nzimbraIsDelegatedAdminAccount: TRUE from "stdout" or from "stdout_lines" in format:

prueba7@prueba7.com zimbraIsDelegatedAdminAccount: TRUE

or

prueba7@prueba7.com
zimbraIsDelegatedAdminAccount: TRUE

and send it to the file.txt. Number of lines can be different (one o more users with domain).

I have no idea how I can do this, is this possible? If you know could you please help with advice? Thank you!

U880D
  • 8,601
  • 6
  • 24
  • 40
Oleg
  • 161
  • 1
  • 2
  • 10

2 Answers2

3

You may have a look into debug – Print statements during execution, Using Variables and Return Values.

---
- hosts: localhost
  become: true
  gather_facts: false

  vars:
    RESULT:
      STDOUT_LINES:
        - "# name admin@shrrah.esquimail.com"
        - "zimbraIsDelegatedAdminAccount: FALSE"
        - ""
        - "# name prueba5@prueba5.com"
        - ""
        - "# name prueba7@prueba7.com"
        - "zimbraIsDelegatedAdminAccount: TRUE"
        - ""
        - "# name prueba9@prueba9.com"

  tasks:

  - name: Show STDOUT_LINES
    debug:
      msg: "{{ RESULT.STDOUT_LINES }}"

resulting into an output only of

TASK [Show STDOUT_LINES] *****************
ok: [localhost] =>
  msg:
  - '# name admin@shrrah.esquimail.com'
  - 'zimbraIsDelegatedAdminAccount: FALSE'
  - ''
  - '# name prueba5@prueba5.com'
  - ''
  - '# name prueba7@prueba7.com'
  - 'zimbraIsDelegatedAdminAccount: TRUE'
  - ''
  - '# name prueba9@prueba9.com'

and if Ansible Callback plugin is configured to YAML instead of JSON.

To get lines containing certain strings only you may Loop over the list based on a Condition

  - name: Show lines with TRUE only
    debug:
      msg: "{{ item }}"
    when: "'TRUE' in item"
    loop: "{{ RESULT.STDOUT_LINES }}"

resulting into an output of

TASK [Show lines with TRUE only] *******************************
ok: [localhost] => (item=zimbraIsDelegatedAdminAccount: TRUE) =>
  msg: 'zimbraIsDelegatedAdminAccount: TRUE'

Further Documenation


If you like to have the line before included, you could use an approach like

  - name: Show lines with TRUE and line before
    debug:
      msg: "{{ RESULT.STDOUT_LINES[ansible_loop.index0 - 1] }}\n{{ item }}"
    when: "'TRUE' in item"
    loop: "{{ RESULT.STDOUT_LINES }}"
    loop_control:
      extended: true
      label: "{{ ansible_loop.index0 }}"

resulting into an output of

TASK [Show lines with TRUE and line before] *************************************************************************************************************************************
ok: [localhost] => (item=6) =>
  msg: |-
    # name prueba7@prueba7.com
    zimbraIsDelegatedAdminAccount: TRUE

Further Documentation


Since you are using the shell module, you could use also an approach like

- name: DELEGATED ADMIN ACCOUNTS - check, get and send to the file domain.list
  shell: 
    cmd: /opt/zimbra/bin/zmprov -l gaaa -v zimbraIsDelegatedAdminAccount | grep -B 1 TRUE

and gather only result lines which are true an the line before.

Further Q&A


Regarding

... send it to the file.txt

you may have a look into

U880D
  • 8,601
  • 6
  • 24
  • 40
2

Create a dictionary

    - set_fact:
        info: "{{ info|d({})|combine({_key: _val}) }}"
      loop: "{{ stdout.split('#')[1:] }}"
      vars:
        _list: "{{ item.split('\n')|map('trim') }}"
        _key: "{{ _list.0.split(' ')|last }}"
        _val: "{{ _list[1:]|select()|map('from_yaml')|combine }}"

gives

  info:
    admin@shrrah.esquimail.com:
      zimbraIsDelegatedAdminAccount: false
    prueba5@prueba5.com: {}
    prueba7@prueba7.com:
      zimbraIsDelegatedAdminAccount: true
    prueba9@prueba9.com: {}

Then, the template is trivial. Either print all items

    - copy:
        content: |-
          {% for k,v in info.items() %}
          {{ k }}
          {{ v|to_nice_yaml }}
          {% endfor %}
        dest: file.txt

gives

shell> cat file.txt 
admin@shrrah.esquimail.com
zimbraIsDelegatedAdminAccount: false

prueba5@prueba5.com
{}

prueba7@prueba7.com
zimbraIsDelegatedAdminAccount: true

prueba9@prueba9.com
{}

, or explicitly select item(s)

    - copy:
        content: |-
          prueba7@prueba7.com
          {{ info['prueba7@prueba7.com']|to_nice_yaml }}
        dest: file.txt

gives

shell> cat file.txt 
prueba7@prueba7.com
zimbraIsDelegatedAdminAccount: true

Note

Additional attributes will be parsed too, e.g.

    stdout_lines: [
      "# name admin@shrrah.esquimail.com",
      "zimbraIsDelegatedAdminAccount: FALSE",
      "",
      "# name prueba5@prueba5.com",
      "",
      "# name prueba7@prueba7.com",
      "zimbraIsDelegatedAdminAccount: TRUE",
      "zimbraIsDelegatedRootAccount: TRUE",
      "",
      "# name prueba9@prueba9.com"
    ]

will give

  info:
    admin@shrrah.esquimail.com:
      zimbraIsDelegatedAdminAccount: false
    prueba5@prueba5.com: {}
    prueba7@prueba7.com:
      zimbraIsDelegatedAdminAccount: true
      zimbraIsDelegatedRootAccount: true
    prueba9@prueba9.com: {}

and consequently

shell> cat file.txt 
prueba7@prueba7.com
zimbraIsDelegatedAdminAccount: true
zimbraIsDelegatedRootAccount: true
Vladimir Botka
  • 58,131
  • 4
  • 32
  • 63