ansible - 在 Ansible 剧本中使用 PostgreSQL 角色登录和执行 PostgreSQL 脚本自动化
问题描述
需要一些关于 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;\""
这应该有效。试试看。
推荐阅读
- html - 如何以相反的顺序应用 nth-child 奇数或偶数?
- pytorch - Pytorch - For 循环 LSTM 并尝试第二次向后遍历图形,但缓冲区已被释放
- php - PHP数组计数
- java - “被通缉但未被调用;然而,还有其他与这个模拟“错误的交互
- git - Git:如何只下载一个特定的提交而不克隆存储库的其余部分?
- c# - 从文件名喜欢的目录中获取文件名
- charts - 使用时间戳时 APEX 图表不显示任何内容
- javascript - TS2307:在 Angular 中找不到错误模块本地 geojson
- bogus - Bogus 中的固定长度数字字符串
- vue.js - Vue i18n:根据服务器返回的密钥转换消息