0

I ran into a (for me) unexpected issue with ansible when trying to execute the following:

# myplaybook.ansible.yml
- hosts: myhost2

  tasks: 
  - name: Execute pgsql query
    community.postgresql.postgresql_query:
      ...

where myhost1 is the controller executing ansible-playbook -v myplaybook.ansible.yml. On the target host myhost2 community.postgresql.postgresql_query is installed with ansible-galaxy install and the dependencies (notably psycopg2) satisfied.

The problem:

ansible bombs out before actually evaluating the playbook because it can't find the postgresql_query module on the controller, but the whole purpose of running this on myhost2 is to avoid having to install such dependencies on the controller in the first place.

Similarly, I'd also like to be able to execute the above like this, which obviously doesn't work for the same reason:

# myplaybook.ansible.yml
- hosts: myhost3

  tasks: 
  - name: Execute pgsql query
    delegate_to: myhost2
    community.postgresql.postgresql_query:
      ...

The Question

How to only evaluate an ansible module called in a task on the target or delegate host?

ppenguin
  • 155
  • 1
  • 11
  • 3
    It is not how Ansible works. When you have a playbook, each task would be evaluated on the controller and this will create a Python script that the controller will ship to the managed node in order for them to execute it. If the controller doesn't know about the module, the controller won't be able to be generate the Python script and the task will of course fail. – β.εηοιτ.βε May 23 '21 at 11:44
  • 1
    Additionally, you install ansible modules on the "controller" node not on the "controlled" nodes, that's how it works and that's how things are designed. You might be confused by being required to install "psycopg2" on the controlled node: that's just a module-specific dependency, required by the remote python to interact with the remote Postgres. – Lester May 23 '21 at 13:22
  • @β.εηοιτ.βε Thanks, I was (wrongly) assuming that if the controlled node has a full-blown ansible install, it would be used on a "higher" level. You might want to turn the comment into an answer (to close this question)? – ppenguin May 23 '21 at 14:24
  • @Lester thanks, that could indeed have been a next concern. For now I'm just making sure to test on the same controller as is used in "production" (i.e. my CI-runner), so de-facto everything is `localhost` now. – ppenguin May 23 '21 at 14:28

0 Answers0