O
O
oni__ino2016-04-06 14:46:43
Ansible
oni__ino, 2016-04-06 14:46:43

How to display an error message msg="" in ansible and not the entire stdout variable?

localhost: ubuntu 14.04
ansible: v2.0.1.0 (default settings)
client_host: redhat 5

- name: Register variable (using with_items)
  command: mountpoint "{{ item }}"
  with_items: ["/mnt/iso", "/mnt/test"]
  register: RTR
  ignore_errors: yes

- debug: var=RTR

- name: Fail if return code is not 0
  fail: msg="{{RTR.results[0].stdout}}"
  when: item.rc != 0
  with_items: "{{RTR.results}}"

I get the output:
TASK [proj-***-test : Fail if return code is not 0] **************************
skipping: [proj-***-test] => (item={u'stdout': u'/mnt/iso is a mountpoint', u'cmd': [u'mountpoint', u'/mnt/iso'], u'end': u'2016-04-06 14:29:38.181588', '_ansible_no_log': False, u'warnings': [], u'changed': True, u'start': u'2016-04-06 14:29:38.171837', u'delta': u'0:00:00.009751', 'item': u'/mnt/iso', u'rc': 0, 'invocation': {'module_name': u'command', u'module_args': {u'creates': None, u'executable': None, u'chdir': None, u'_raw_params': u'mountpoint "/mnt/iso"', u'removes': None, u'warn': True, u'_uses_shell': False}}, 'stdout_lines': [u'/mnt/iso is a mountpoint'], u'stderr': u''}) 
failed: [proj-***-test] => (item={u'changed': True, u'stdout': u'/mnt/test is not a mountpoint', '_ansible_no_log': False, 'stdout_lines': [u'/mnt/test is not a mountpoint'], u'warnings': [], 'item': u'/mnt/test', u'cmd': [u'mountpoint', u'/mnt/test'], 'failed': True, u'delta': u'0:00:00.010001', u'stderr': u'', u'rc': 1, 'invocation': {'module_name': u'command', u'module_args': {u'creates': None, u'executable': None, u'chdir': None, u'_raw_params': u'mountpoint "/mnt/test"', u'removes': None, u'warn': True, u'_uses_shell': False}}, u'end': u'2016-04-06 14:29:38.435078', u'start': u'2016-04-06 14:29:38.425077'}) => {"failed": true, "item": {"_ansible_no_log": false, "changed": true, "cmd": ["mountpoint", "/mnt/test"], "delta": "0:00:00.010001", "end": "2016-04-06 14:29:38.435078", "failed": true, "invocation": {"module_args": {"_raw_params": "mountpoint \"/mnt/test\"", "_uses_shell": false, "chdir": null, "creates": null, "executable": null, "removes": null, "warn": true}, "module_name": "command"}, "item": "/mnt/test", "rc": 1, "start": "2016-04-06 14:29:38.425077", "stderr": "", "stdout": "/mnt/test is not a mountpoint", "stdout_lines": ["/mnt/test is not a mountpoint"], "warnings": []}, "msg": "/mnt/iso is a mountpoint"}
  to retry, use: --limit @test-playbook.retry

PLAY RECAP *********************************************************************
proj-***-test               : ok=5    changed=0    unreachable=0    failed=1

Why does output follow this strange pattern u' ** ': u' ** ', u' ** ' --- like this u'stdout': u'/mnt/iso is a mountpoint', u'cmd' and not, for example, with transitions to the next line, it’s not very easy to read, maybe I’m picking on The
question is how to get an answer only in "stdout": "/mnt/test is not a mountpoint", or "stdout_lines": "/mnt/test is not a mountpoint" because if you specify msg="{{RTR.results[0].stdout_lines}}" or RTR.results[0].results.stdout_lines swears like this: fatal: [*****]: FAILED! => {"failed": true, "msg": "'unicode object' has no attribute 'stdout_lines'"}
Output from variable string - debug: var=RTR
TASK [proj-***-test : debug] *************************************************
ok: [proj-***-test] => {
    "RTR": {
        "changed": true, 
        "failed": true, 
        "msg": "One or more items failed", 
        "results": [
            {
                "_ansible_no_log": false, 
                "changed": true, 
                "cmd": [
                    "mountpoint", 
                    "/mnt/iso"
                ], 
                "delta": "0:00:00.009751", 
                "end": "2016-04-06 14:29:38.181588", 
                "invocation": {
                    "module_args": {
                        "_raw_params": "mountpoint \"/mnt/iso\"", 
                        "_uses_shell": false, 
                        "chdir": null, 
                        "creates": null, 
                        "executable": null, 
                        "removes": null, 
                        "warn": true
                    }, 
                    "module_name": "command"
                }, 
                "item": "/mnt/iso", 
                "rc": 0, 
                "start": "2016-04-06 14:29:38.171837", 
                "stderr": "", 
                "stdout": "/mnt/iso is a mountpoint", 
                "stdout_lines": [
                    "/mnt/iso is a mountpoint"
                ], 
                "warnings": []
            }, 
            {
                "_ansible_no_log": false, 
                "changed": true, 
                "cmd": [
                    "mountpoint", 
                    "/mnt/test"
                ], 
                "delta": "0:00:00.010001", 
                "end": "2016-04-06 14:29:38.435078", 
                "failed": true, 
                "invocation": {
                    "module_args": {
                        "_raw_params": "mountpoint \"/mnt/test\"", 
                        "_uses_shell": false, 
                        "chdir": null, 
                        "creates": null, 
                        "executable": null, 
                        "removes": null, 
                        "warn": true
                    }, 
                    "module_name": "command"
                }, 
                "item": "/mnt/test", 
                "rc": 1, 
                "start": "2016-04-06 14:29:38.425077", 
                "stderr": "", 
                "stdout": "/mnt/test is not a mountpoint", 
                "stdout_lines": [
                    "/mnt/test is not a mountpoint"
                ], 
                "warnings": []
            }
        ]
    }
}

And take a look at my question, if you know how to help...
Why do not wraps work in the ansible log ...

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question