I have a task , want to do something when a specified database is not in postgresql using command “psql --list”
-
name: check if db exists
tags:
¦ - checkdbexists
¦ - test
command: “psql --list”
register: dbexists -
name: debug dbexist
tags:
¦- test
¦- debug
¦- ok
debug: var=dbexists.results
and the debug message like below
TASK [eus-developers : debug dbexist] ********************************************************** ok: [hs053.abc.com] => { "dbexists.results": [ { "_ansible_ignore_errors": null, "_ansible_item_result": true, "_ansible_no_log": false, "changed": false, "item": { "dhome": "/home/changch/D", "name": "changch", "nb": "hc75.abc.com", "neohome": "/home/changch/D/eus", "ve": "/home/changch/D/eus/.ve", "ws": "hs055.abc.com" }, "skip_reason": "Conditional result was False", "skipped": true }, { "_ansible_ignore_errors":, "_ansible_item_result": true, "_ansible_no_log": false, "changed": false, "item": { "dhome": "/home/changp/D", "name": "changp", "nb": "hc77.abc.com", "neohome": "/home/changp/D/eus", "ve": "/home/changp/D/eus/.ve", "ws": "hs051.abc.com" }, "skip_reason": "Conditional result was False", "skipped": true }, { "_ansible_ignore_errors": null, "_ansible_item_result": true, "_ansible_no_log": false, "_ansible_parsed": true, "changed": true, "cmd": [ "psql", "--list" ], "delta": "0:00:00.051539", "end": "2018-09-14 22:32:10.953211", "failed": false, "invocation": { "module_args": { "_raw_params": "psql --list", "_uses_shell": false, "chdir": null, "creates": null, "executable": null, "removes": null, "stdin": null, "warn": true } }, "item": { "dhome": "/home/um/D", "name": "um", "nb": "hc79.abc.com", "neohome": "/home/um/D/eus", "ve": "/home/um/D/eus/.ve", "ws": "hs053.abc.com" }, "rc": 0, "start": "2018-09-14 22:32:10. 901672", "stderr": "", "stderr_lines": [], "stdout": " List of databases\n Name | Owner | E ncoding | Collate | Ctype | Access privileges \n-----------+----------+----------+-------------+-------------+-----------------------\n postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | \n template0 | postgres | UTF8 | en_US.UTF- 8 | en_US.UTF-8 | =c/postgres +\n | | | | | postgres=CTc/postgres\n template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +\n | | | | | postgres=CTc/postgres\n(3 rows)", "stdout_lines": [ " List of databases", " Name | Owner | Encoding | Collate | Ctype | Access privileges ", "-----------+----------+----------+-------------+-------------+---------------------- -", " postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | ", " template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/
I first try using stdout_lines
- name: debug dbexist
tags:
¦- test
¦- debug
¦- ok
debug: #var=dbexists.results
¦msg: “{{ item.stdout_lines }}”
with_items: “{{dbexists.results}}”
the error is no such attr
FAILED! => {“msg”: “The task includes an option with an undefined variable. The error was: ‘dict object’ has no attribute ‘stdout_lines’\n\nThe error appears to have been in ‘developers/tasks/main.yml’: line 150, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- name: debug dbexist\n ^ here\n”}
and I try item.rc / item.start / item.stdou , all no luck !
Can anybody please help ? any suggestions ? thanks !