ansible - How do I pass a dictionary to an ansible ad-hoc command?
问题描述
If I have an ansible ad-hoc command that wants a dictionary or list valued argument, like the queries
argument to postgresql_query
, how do I invoke that in ansible
ad-hoc commands?
Do I have to write a one-command playbook? I'm looking for a way to minimise the numbers of layers of confusing quoting (shell, yaml/json, etc) involved.
The ansible docs mention accepting structured forms for variables. So I tried the yaml and json syntax for the arguments:
ansible -m postgresql_query -sU postgres -a '{"queries":["SELECT 1", "SELECT 2"]}'
... but got ERROR! this task 'postgresql_query' has extra params, which is only allowed in the following modules: ...
.
the same is true if I @include
a file with yaml or json contents like
cat > 'query.yml' <<'__END__'
queries:
- "SELECT 1"
- "SELECT 2"
__END__
ansible -m postgresql_query -sU postgres -a @queries.yml
解决方案
您可以在 JSON 变量中定义字典,然后将其作为参数传递:
ansible -m module_name -e '{"dict": {"key": "value"}}' -a "param={{ dict }}"
(参数位置是任意的)
推荐阅读
- python-3.x - UTF-8 字符串无法正确显示字符
- java - 如何获取获取新对象并将其添加到 ArrayList 的方法?
- spring-boot - 在 Spring Boot 中加速 YAML 文件处理
- java - 从迭代器 Bukkit 1.16.5 中获取玩家
- php - 使用推送梁推送通知,无法发送数据/有效负载
- android - 与蓝牙设备配对与连接到蓝牙设备有什么区别?
- c++ - 具有多态性的复制构造函数和赋值运算符
- php - Laravel 8 登录时注册用户
- vb.net - 如何在 Visual Studio 中创建 .exe 文件?
- python - Python烧瓶日志记录:两种不同的格式,但想要一种格式