首页 > 解决方案 > 在 Ansible 剧本中使用 PostgreSQL 角色登录和执行 PostgreSQL 脚本自动化

问题描述

需要一些关于 Ansible 脚本的帮助

无需安装 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"; 

标签: ansible

解决方案


您需要记住,您必须转义括号字符,还有 ; 在 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;\""

这应该有效。试试看。


推荐阅读