# Passing arguments using ansible-builtin-command

**URL:** <https://forum.ansible.com/t/passing-arguments-using-ansible-builtin-command/45038>\
**Category:** Get Help\
**Created:** [December 30, 2025, 10:05am UTC](https://forum.ansible.com/t/passing-arguments-using-ansible-builtin-command/45038 "2025-12-30T10:05:39Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![rustydraper](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/rustydraper/32/10056_2.png) [@rustydraper](https://forum.ansible.com/u/rustydraper)\
**Post date:** [December 30, 2025, 10:05am UTC](https://forum.ansible.com/t/passing-arguments-using-ansible-builtin-command/45038/1 "2025-12-30T10:05:39Z")

</div>

I have a command that takes arguments in the form: mycommand --myarg1=myargvalue --myarg2=‘{“mytags”: [{“myint1”: 12345, “mystr1”: “fubarbaz”}]}’

I created one variable with “–myarg1=myargvalue”. That works fine. I created a second variable with the entire dictionary-list-of-dictionary as a string, using double quotes. I attempted to use the variable in a task that calls the command and passes the arguments. The ansible-playbook could not process the command although the variable did seem to be substituted correctly, meaning my string did show up in the debug output.  
I then attempted to combine multiple variables by creating a dictionary, assigning the dictionary as a single element list, and placed that inside another dictionary. That renders properly but the command then fails, as the command needs to have the double quotes around it’s values, which are missing when I create the ansible dictionary.

To get the playbook working, I just put the command in a shell script and called the shell script. I would much prefer calling the command in ansible and providing the arguments from variables if the quoting problem can be solved. I was wondering if anyone has any quote-filtering magic that can be shared.

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [December 30, 2025, 5:31pm UTC](https://forum.ansible.com/t/passing-arguments-using-ansible-builtin-command/45038/2 "2025-12-30T17:31:58Z")

</div>

Hey @rustydraper ! Welcome to _The Forum_.

> [@rustydraper](#):
>
> I then attempted to combine multiple variables by creating a dictionary, assigning the dictionary as a single element list, and placed that inside another dictionary.

You could make it a lot easier for us to help you if you **show us** what you did exactly rather than expect us to correctly **recreate the problem from a description**. We can’t offer a solution until we can reproduce the problem.

Here’s how to properly enter YAML into forum posts:

````
```yaml
- name: Task-name
  ansible.builtin.command: mycommand --myarg1=myargvalue --myarg2=‘{“mytags”: [{“myint1”: 12345, “mystr1”: “fubarbaz”}]}’
[etc.]
```

````

The above doesn’t count; it’s just a copy-n-paste (with some guesswork) of what you started with. But, please, don’t expect us to guess correctly; show us. We really do want to help.

---

<div class="post-metadata">

**Author:** ![flowerysong](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/flowerysong/32/2305_2.png) [@flowerysong](https://forum.ansible.com/u/flowerysong)\
**Post date:** [December 30, 2025, 5:58pm UTC](https://forum.ansible.com/t/passing-arguments-using-ansible-builtin-command/45038/3 "2025-12-30T17:58:09Z")

</div>

A common issue is that the short form of calling the command and shell modules introduces an additional layer of parsing. It is significantly more robust to use the `cmd` argument, e.g instead of:

```yaml
- command: echo hello world

```

you should write

```yaml
- command:
    cmd: echo hello world

```

It’s slightly more typing, but avoids quite a bit of weirdness.

---

<div class="post-metadata">

**Author:** ![U880D](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/u880d/32/10032_2.png) [@U880D](https://forum.ansible.com/u/U880D)\
**Post date:** [December 31, 2025, 9:36am UTC](https://forum.ansible.com/t/passing-arguments-using-ansible-builtin-command/45038/4 "2025-12-31T09:36:34Z")

</div>

A minimal example playbook

```yaml
---
- hosts: localhost
  become: false
  gather_facts: false

  tasks:

    - name: Pass arguments
      command:
        cmd: |
          echo \
          --myarg1=myargvalue \
          --myarg2='{"mytags": [{"myint1": 12345, "mystr1": "fubarbaz"}]}'
      register: result

    - name: Show arguments
      debug:
        msg: "{{ result }}"

```

will result into the expected execution and an output of

```yaml
TASK [Show arguments] ***********************************************
ok: [localhost] =>
  msg:
    changed: true
    cmd:
    - echo
    - |2-

      --myarg1=myargvalue
    - |2-

      --myarg2={"mytags": [{"myint1": 12345, "mystr1": "fubarbaz"}]}
    delta: '0:00:00.004918'
    end: '2025-12-31 10:30:00.223432'
    failed: false
    msg: ''
    rc: 0
    start: '2025-12-31 10:30:00.218514'
    stderr: ''
    stderr_lines: []
    stdout: |2-

      --myarg1=myargvalue
      --myarg2={"mytags": [{"myint1": 12345, "mystr1": "fubarbaz"}]}
    stdout_lines:
    - ''
    - '--myarg1=myargvalue '
    - '--myarg2={"mytags": [{"myint1": 12345, "mystr1": "fubarbaz"}]}'

```

---

<div class="post-metadata">

**Author:** ![rustydraper](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/rustydraper/32/10056_2.png) [@rustydraper](https://forum.ansible.com/u/rustydraper)\
**Post date:** [January 7, 2026, 6:06pm UTC](https://forum.ansible.com/t/passing-arguments-using-ansible-builtin-command/45038/5 "2026-01-07T18:06:13Z")

</div>

Thank you to all who responded.

I was able to get my playbook working using the scaler variable syntax demonstrated in one of the responses.

Russell Draper  
[rusty.draper@mindspring.com](mailto:rusty.draper@mindspring.com)

---

<div class="post-metadata">

**Author:** ![chris](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/chris/32/293_2.png) [@chris](https://forum.ansible.com/u/chris)\
**Post date:** [January 16, 2026, 4:13pm UTC](https://forum.ansible.com/t/passing-arguments-using-ansible-builtin-command/45038/6 "2026-01-16T16:13:37Z")

</div>

I wasn’t aware of this, shouldn’t the examples be updated to reflect this?

> **[Examples - ansible.builtin.command module – Execute commands on targets —...](https://docs.ansible.com/projects/ansible/latest/collections/ansible/builtin/command_module.html#examples)**
>
> Note

---

<div class="post-metadata">

**Author:** ![Parnoud](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/parnoud/32/10132_2.png) [@Parnoud](https://forum.ansible.com/u/Parnoud)\
**Post date:** [January 19, 2026, 8:31pm UTC](https://forum.ansible.com/t/passing-arguments-using-ansible-builtin-command/45038/7 "2026-01-19T20:31:39Z")

</div>

Shouldn’t we use the option argv ?

```auto
# argv (list) arguments, each argument on a separate line, 'args' keyword not necessary
# 'argv' is a parameter, indented one level from the module
- name: Use 'argv' to send a command as a list - leave 'command' empty
  ansible.builtin.command:
    argv:
      - /usr/bin/make_database.sh
      - Username with whitespace
      - dbname with whitespace
    creates: /path/to/database

```

Source : [argv](https://docs.ansible.com/projects/ansible/latest/collections/ansible/builtin/command_module.html#parameter-argv)

---

<div class="post-metadata">

**Author:** ![felixfontein](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/felixfontein/32/7530_2.png) [@felixfontein](https://forum.ansible.com/u/felixfontein)\
**Post date:** [January 19, 2026, 8:50pm UTC](https://forum.ansible.com/t/passing-arguments-using-ansible-builtin-command/45038/8 "2026-01-19T20:50:19Z")

</div>

Depends what you want to do and how readable the result is; both `cmd` and `argv` have their places and each can be better than the other depending on the context. As soon as you template parameters or have some with complex values, `argv` can be the better choice - but of course you can also use the appropriate filters for quoting. But if you run a simple command without templating anything, `cmd` is a lot easier to read and write than `argv`.
