Atul Agrawal
Atul Agrawal

Reputation: 1520

Unable to execute postgres sql script from ansible

I want to execute a postgresql sql which is fetched from my git resource. I want to first fetch all the scripts from git repository and then wants to execute scripts from a particular directory which i taking as an input in ansible script.This is my complete ansible script

- hosts: "{{ HOST }}"
  become: true
  become_user: "admin"
  become_method: su
  gather_facts: true
  vars:
    app_path: "{{ app_path }}"
    app_dir: "{{ app_dir }}"

  tasks:
    - stat:
        path: "{{ app_path }}/{{ app_dir }}"
      register: exist

    - name: create "{{ app_path }}/{{ app_dir }}" directory
      file: dest="{{ app_path }}/{{ app_dir }}" state=directory 
      when: exist.stat.exists != True


    - git: 
        repo: http://git-url/postgres-demo.git
        dest: "{{ app_path }}/{{ app_dir }}"
        version: "{{ GIT_TAG }}"
        refspec: '+refs/heads/{{ GIT_TAG }}:refs/remotes/origin/{{ GIT_TAG }}'
        update: yes
        force: true
      register: cloned  

    - name: Execute postgres dump files
      shell: "/usr/bin/psql -qAtw -f  {{ item }}"
      with_fileglob:
          - "{{ app_path }}/{{ app_dir }}/{{ scripts_dir }}/*"
      register: postgres_sql
      become: true
      become_user: "postgres"
      become_method: su

Above script is executed successfully but postgres step throwing me following waning:

[WARNING]: Unable to find '/home/admin/postgres-dev/test' in expected paths.

When i checked my postgresql db,I don't find the table that i want to create using this scripts.

Upvotes: 0

Views: 780

Answers (1)

Konstantin Suvorov
Konstantin Suvorov

Reputation: 68289

All lookups in Ansible are executed on local ansible host.

So:

with_fileglob:
  - "{{ app_path }}/{{ app_dir }}/{{ scripts_dir }}/*"

uses fileglob lookup, which searches files on localhost!

You should refactor this code to use find module first to find all required scripts, register its output and then loop with with_items over registered variable to execute scripts.

Upvotes: 1

Related Questions