首页 > 解决方案 > 如何使用带有动态参数的 ansible postgress 查询

问题描述

我有一些 ansible 角色,我需要动态传递部分数据库名称,由于某些限制,我无法摆脱 sql 文件,所以我尝试了下面的代码但没有用

- name: Select query to my_test db with named_args
  postgresql_query:
    db: test_db
    path_to_script: myscript.sql 
    named_args:
      schema: mytest 
      table_pfix: test
  

myscript.sql 文件是

set SCHEMA %(schema)s; 
select  count(*) from my_%(table_pfix)s;

当我运行这个角色时,我收到语法错误

{"changed" : false, "msg": "can not execute sql schema 'set SCHEMA %(schema)s;\nslect  count(*) from my_%(table_pfix)s;'
{'scheam':mytest,'table_pfix':'test'}: synatax error at near \"'my_test'\" LINE 2 : slect count(*) from my_'test';

标签: sqlpostgresqlansible

解决方案


推荐阅读