Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I pass a dictionary to an ansible ad-hoc command?

Tags:

ansible

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
like image 795
Craig Ringer Avatar asked Dec 03 '18 16:12

Craig Ringer


People also ask

How do you pass a command in ansible playbook?

The easiest way to pass Pass Variables value to Ansible Playbook in the command line is using the extra variables parameter of the “ansible-playbook” command. This is very useful to combine your Ansible Playbook with some pre-existent automation or script.

Which option in ad hoc command is used to execute a module?

Basic Commands The ad-hoc command below runs a ping module on all the hosts in the inventory file. Here -m is the option for a module.


1 Answers

You can define a dictionary in a JSON variable to pass it as parameter next:

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

(parameters positions are arbitrary)

like image 102
pedroapero Avatar answered Nov 15 '22 08:11

pedroapero