0

I am trying to connect to postgres installed on windows server using ansible but unable to do it. I am running ansible on my MAC laptop and able to connect to postgres using pgadmin.

When I am running query using ansible, I am getting error.

TASK [Access postgresql] ****************************************************************************************************************
task path: /Users/arun/Learning/ansible-winrm/postgres.yml:9
Using module file /Users/arun/.ansible/collections/ansible_collections/community/postgresql/plugins/modules/postgresql_query.py
Pipelining is enabled.
<5X.2X.X4.X8> ESTABLISH WINRM CONNECTION FOR USER: Administrator on PORT 5986 TO 5X.2X.X4.X8
EXEC (via pipeline wrapper)
fatal: [5X.2X.X4.X8]: FAILED! => {
    "changed": false,
    "module_stderr": "Exception calling \"Create\" with \"1\" argument(s): \"At line:4 char:21\r\n+ def _ansiballz_main():\r\n+                     ~\r\nAn expression was expected after '('.\r\nAt line:13 char:27\r\n+     except (AttributeError, OSError):\r\n+                           ~\r\nMissing argument in parameter list.\r\nAt line:15 char:29\r\n+     excludes = set(('', '.', scriptdir))\r\n+                             ~\r\nMissing expression after ','.\r\nAt line:15 char:30\r\n+     excludes = set(('', '.', scriptdir))\r\n+                              ~~~~~~~~~\r\nUnexpected token 'scriptdir' in expression or statement.\r\nAt line:15 char:29\r\n+     excludes = set(('', '.', scriptdir))\r\n+                             ~\r\nMissing closing ')' in expression.\r\nAt line:15 char:39\r\n+     excludes = set(('', '.', scriptdir))\r\n+                                       ~\r\nUnexpected token ')' in expression or statement.\r\nAt line:15 char:40\r\n+     excludes = set(('', '.', scriptdir))\r\n+                                        ~\r\nUnexpected token ')' in expression or statement.\r\nAt line:22 char:7\r\n+     if sys.version_info < (3,):\r\n+       ~\r\nMissing '(' after 'if' in if statement.\r\nAt line:22 char:30\r\n+     if sys.version_info < (3,):\r\n+                              ~\r\nMissing expression after ','.\r\nAt line:22 char:25\r\n+     if sys.version_info < (3,):\r\n+                         ~\r\nThe '<' operator is reserved for future use.\r\nNot all parse errors were reported.  Correct the reported errors and try again.\"\r\nAt line:10 char:1\r\n+ $exec_wrapper = [ScriptBlock]::Create($split_parts[0])\r\n+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\r\n    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException\r\n    + FullyQualifiedErrorId : ParseException\r\n \r\nThe expression after '&' in a pipeline element produced an object that was not valid. It must result in a command \r\nname, a script block, or a CommandInfo object.\r\nAt line:11 char:2\r\n+ &$exec_wrapper\r\n+  ~~~~~~~~~~~~~\r\n    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException\r\n    + FullyQualifiedErrorId : BadExpression\r\n ",
    "module_stdout": "",
    "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error",
    "rc": 1
}

My postgres.yml file looks like below

- hosts: windows
  remote_user: Administrator
 
  tasks:
  - name: Access postgresql
    community.postgresql.postgresql_query:
        login_user: postgres
        login_password: mypassword
        query: SELECT datname FROM pg_database
        db: postgres
0

1 Answer 1

1

The errors you see are caused by the fact that Ansible is attempting to use PowerShell to execute Python code, which isn't ever going to work. Looking at the documentation for Ansible on Windows, it looks as if only a small number of modules are actually supported for Windows:

What modules are available

Most of the Ansible modules in core Ansible are written for a combination of Linux/Unix machines and arbitrary web services, though there are various Windows-only modules. These are listed in the “windows” subcategory of the Ansible module index.

In addition, the following core modules/action-plugins work with Windows:

  • add_host
  • assert
  • async
  • debug
  • fail
  • fetch
  • group_by
  • include_vars
  • meta
  • pause
  • raw
  • script
  • set_fact
  • setup
  • slurp
  • template (also: win_template)

That suggests you're not going to be able to run the postgresql_query modules on your Windows host.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.