使用Ansible playbook中的PostgreSQL角色登录和执行PostgreSQL脚本自动化

使用Ansible playbook中的PostgreSQL角色登录和执行PostgreSQL脚本自动化,ansible,Ansible,需要有关Ansible脚本的帮助吗 使用Ansible playbook登录并执行所需的一些示例 无需安装PostgreSQL 我们尝试使用下面的命令,但它不起作用 - 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 coun

需要有关Ansible脚本的帮助吗

  • 使用Ansible playbook登录并执行所需的一些示例
无需安装PostgreSQL

我们尝试使用下面的命令,但它不起作用

- 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"; 

您需要记住,您必须转义括号字符,以及;sql语句结尾处需要位于转义括号内。我还建议您让ansible在开始执行shell命令时使用空格,这样带有密码的命令就不会被记录到shell历史记录中

- 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;\""

这应该行得通。试一试。

您好,谢谢您的回复。我尝试了您的建议,但现在得到了以下错误“stderr”:“/bin/sh:psql:command not found”,“stdoutHi”谢谢您的回复。我尝试了您的建议,但现在得到了以下错误“stderr”:“/bin/sh:psql:command not found”stdoutYou需要在执行playbook的机器上安装psql客户端。在Ubuntu上,例如为了更清楚。它必须安装在您在hosts文件中定义的主机上,该主机用作playbook.HI的目标。我们手动尝试使用pgsql命令对同一用户执行该命令。但通过ansible的剧本,它不起作用。