Below is my manual command-line DB command execution output:

Note: I changed the connection string on purpose... Let me know if you need 
the correct connection string to check and test anything real time?

    # mongo --quiet --eval 'db=db.getSiblingDB("efendibey"); 
printjson(db.stats())' 
mongodb://42y2jn2Su31W9sS7hn:[email protected]:60076/?authSource=efendibey
    {
            "db" : "efendibey",
            "collections" : 15,
            "views" : 0,
            "objects" : 389,
            "avgObjSize" : 463.32647814910024,
            "dataSize" : 180234,
            "storageSize" : 479232,
            "indexes" : 15,
            "indexSize" : 405504,
            "totalSize" : 884736,
            "scaleFactor" : 1,
            "fsUsedSize" : 39113961472,
            "fsTotalSize" : 42256773120,
            "ok": 1
    }

I want this output as is to be sent back to the executor using ansible. 
Thus, above is the expected output I desire.

Below is my ansible playbook for the above command execution and print:

    - name: Execute DB command
      hosts: localhost
      gather_facts: false
    
    
      tasks:
    
        - name: Execute DB command
          raw: "{{ item }}"
          register: dboutput
          loop:
            - "mongo --quiet --eval 'db=db.getSiblingDB(\"efendibey\"); 
printjson(db.stats())' 
mongodb://42y2jn2Su31W9sS7hn:[email protected]:60076/?authSource=efendibey"
    
        - name: DEBUG DB output
          debug:
            msg: "here: {{ item.stdout }}"
          loop: "{{ dboutput.results }}"

However, the formatting it lost as visible from the output below:

    TASK [Execute DB command] 
**************************************************************************************************************************************************************************************
    changed: [localhost] => (item=mongo --quiet --eval 
'db=db.getSiblingDB("efendibey"); printjson(db.stats())' 
mongodb://42y2jn2Su31W9sS7hn:[email protected]:60076/?authSource=efendibey)
    
    TASK [DEBUG DB output] 
*****************************************************************************************************************************************************************************************
    ok: [localhost] => (item={'rc': 0, 'stdout': '{\n\t"db" : 
"efendibey",\n\t"collections" : 15,\n\t"views" : 0,\n\t"objects" : 
389,\n\t"avgObjSize" : 463.32647814910024,\n\t"dataSize" : 
180234,\n\t"storageSize" : 479232,\n\t"indexes" : 15,\n\t"indexSize" : 
405504,\n\t"totalSize" : 884736,\n\t"scaleFactor" : 1,\n\t"fsUsedSize" : 
39113928704,\n\t"fsTotalSize" : 42256773120,\n\t"ok" : 1\n}\n', 
'stdout_lines': ['{', '\t"db" : "efendibey",', '\t"collections" : 15,', 
'\t"views" : 0,', '\t"objects" : 389,', '\t"avgObjSize" : 
463.32647814910024,', '\t"dataSize" : 180234,', '\t"storageSize" : 
479232,', '\t"indexes" : 15,', '\t"indexSize" : 405504,', '\t"totalSize" : 
884736,', '\t"scaleFactor" : 1,', '\t"fsUsedSize" : 39113928704,', 
'\t"fsTotalSize" : 42256773120,', '\t"ok" : 1', '}'], 'stderr': '', 
'stderr_lines': [], 'changed': True, 'failed': False, 'item': 'mongo 
--quiet --eval \'db=db.getSiblingDB("efendibey"); printjson(db.stats())\' 
mongodb://42y2jn2Su31W9sS7hn:[email protected]:60076/?authSource=efendibey',
 
'ansible_loop_var': 'item'}) => {
        "msg": "here: {\n\t\"db\" : \"efendibey\",\n\t\"collections\" : 
15,\n\t\"views\" : 0,\n\t\"objects\" : 389,\n\t\"avgObjSize\" : 
463.32647814910024,\n\t\"dataSize\" : 180234,\n\t\"storageSize\" : 
479232,\n\t\"indexes\" : 15,\n\t\"indexSize\" : 405504,\n\t\"totalSize\" : 
884736,\n\t\"scaleFactor\" : 1,\n\t\"fsUsedSize\" : 
39113928704,\n\t\"fsTotalSize\" : 42256773120,\n\t\"ok\" : 1\n}\n"
    }

I wish to eventually use `blockinfile` to dump and then read back the 
output of DB execution to a flat file.

I tried `regex_replace` filter but the new lines and other formatting 
remain disturbed and i cannot get the desired output.

    - name: Replace escaped characters with actual newline and tab
      set_fact:
        formatted_data: "{{ item.stdout | regex_replace('\\\\n', '\\n') | 
regex_replace('\\\\t', '\\t') }}" 
      loop: "{{ dboutput.results }}"

Note: In case it is difficult to get the output as is... I would like the 
output to be decently formatted on newlines.

Can you please suggest how can I?

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/cb5b3ddd-0558-4af7-9e59-ae87532121aen%40googlegroups.com.

Reply via email to