0

Required some help on Ansible script

  • Login and executing some samples required using Ansible playbook.

No need to install PostgreSQL.

We tried with below command but it's not working.

- name: Login to DB and run command 
  command:PGPASSWORD='{{pgpass_filepath}}'; psql -U "{{ db_user }}" -d "{{ db_name }}" -h "{{ db_host }}" -p 5555-c "select count(*) from student"; 

1 Answer 1

1

You need to keep in mind that you have to escape the bracket characters, also the ; at the end of the sql statement needs to be inside the escaped brackets. I would also advise you to let ansible execute the shell command with a white space at the beginning so the command with the password won't be logged to the shell history.

- name: Login to DB and run command
  shell: " PGPASSWORD='{{pgpass_filepath}}' psql -U {{ db_user }} -d {{ db_name }} -h {{ db_host }} -p 5555 -c \"select count(*) from student;\""

This should work. Give it a try.

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

8 Comments

Hi Thanks for your reply.I tried with your suggestion but I am getting the following error now "stderr": "/bin/sh: psql: command not found", "stdout
Hi Thanks for your reply.I tried with your suggestion but I am getting the following error now "stderr": "/bin/sh: psql: command not found", "stdout
You need to have the psql client installed on the machine where you execute the playbook. On Ubuntu e.g. packages.ubuntu.com/search?keywords=postgresql-client
To be clearer. It has to be installed on the host you defined in the hosts file and which is used as target of the playbook.
HI Manually we tried from pgsql command its for fine for the same user. But through ansible playbook its not working.
|

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.