首页 > 解决方案 > 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

标签: ansible

解决方案


您可以在 JSON 变量中定义字典,然后将其作为参数传递:

ansible -m module_name -e '{"dict": {"key": "value"}}' -a "param={{ dict }}" 

(参数位置是任意的)


推荐阅读