相关文章推荐
豪气的瀑布  ·  service worker - ...·  11 月前    · 
心软的大脸猫  ·  python Image - 简书·  1 年前    · 
乐观的西瓜  ·  Rider ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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
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!

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] =>
  - '# 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

  • Index of all Callback Plugins
  • 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

  • Extended loop variables
  • 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

  • grep a file, but show several surrounding lines?
  • Regarding

    ... send it to the file.txt

    you may have a look into

  • Ansible - Save registered variable to file
  • Ansible: Save registered variables to file
  •     - 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
    

    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
            

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.