postgresql - Ansible playbook 使用 shell 命令更改用户
问题描述
我是 Ansible 的新手,愿意编写一个切换到另一个用户的小剧本,进入 Postgres 数据库,进行一些更改,退出数据库并切换回原始用户。
这是我手动执行的操作,但我想将这些命令放入剧本中:
sudo su - postgres
psql postgres
DROP DATABASE scm;
CREATE DATABASE scm OWNER scm ENCODING 'UTF8';
\q ##This will quit the database
exit ##This will quit postgres user back to original user
我开始将其写入剧本,但似乎不起作用:
---
- name: TEST
hosts: master_servers
tasks:
- name: Delete DB
shell:
cmd: psql postgres | DROP DATABASE scm; | CREATE DATABASE scm OWNER scm ENCODING 'UTF8'; | \q | exit
become: yes
become_user: postgres
这是我得到的错误:
fatal: [xx.xx.xx.xx]: FAILED! => {
"msg": "Failed to change ownership of the temporary files Ansible needs to create despite connecting as a privileged user. Unprivileged become user would be unable to read the file."
}
提前致谢!
解决方案
我建议您使用postgresql_db模块(由 Ansible 核心团队维护)。
您可以以幂等方式创建、删除或以其他方式操作 PostgreSQL 数据库(您应始终避免使用shell
和cmd
模块,因为它们在设计上不是幂等的)。
推荐阅读
- rpc - 从 NodeJS(在机器#2 中)调用 Python 脚本(在机器#1 中)
- asp.net-core - 函数返回文件时加载 API 定义失败
- java - Spring Boot +CrudRepository + RestAPI +MySSQL - 自定义查询不起作用
- android - Recycler View 不显示数据
- c - 从 C 中的另一个函数访问二维数组
- angular - 如何避免在 Angular 测试阶段出现以下问题:Chrome Headless has not capture in 60000 ms, kill
- security - TCP/IP 序列预测盲重置欺骗 DoS
- mysql - 获取每个组中的最新记录 - mysql(按最大日期/最大主键)
- r - R数据框按变量中的列名过滤
- c - 填充 SKB 并削减 8 个字节