# What is free\_form command and cmd parameter in command module

**URL:** <https://forum.ansible.com/t/what-is-free-form-command-and-cmd-parameter-in-command-module/4660>\
**Category:** Get Help\
**Tags:** ad-hoc\
**Created:** [April 1, 2024, 2:55pm UTC](https://forum.ansible.com/t/what-is-free-form-command-and-cmd-parameter-in-command-module/4660 "2024-04-01T14:55:13Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![aaron1989041](https://avatars.discourse-cdn.com/v4/letter/a/ee59a6/32.png) [@aaron1989041](https://forum.ansible.com/u/aaron1989041)\
**Post date:** [April 1, 2024, 2:55pm UTC](https://forum.ansible.com/t/what-is-free-form-command-and-cmd-parameter-in-command-module/4660/1 "2024-04-01T14:55:13Z")

</div>

1. when using command module " Either a free form command or `cmd` parameter is required". what is “free form command” and “cmd parameter”?
2. when executing `ansible localhost -m command -a "cmd=date"`, nothing happens.but if I put `cmd: date` in a task, it worked. so “cmd parameter” can only be used in playbook but not in ad hoc command？
3. it there and document about which parameter can’t be used in ad hoc command?

---

<div class="post-metadata">

**Author:** ![bcoca](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/bcoca/32/9269_2.png) [@bcoca](https://forum.ansible.com/u/bcoca)\
**Post date:** [April 1, 2024, 3:09pm UTC](https://forum.ansible.com/t/what-is-free-form-command-and-cmd-parameter-in-command-module/4660/2 "2024-04-01T15:09:39Z")

</div>

> [@aaron1989041](#):
>
> out which parameter can’t be used in ad hoc command?

none, they should all work as ‘adhoc’ creates a temporary play and task and uses the commands you pass for that

---

<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:** [April 1, 2024, 3:39pm UTC](https://forum.ansible.com/t/what-is-free-form-command-and-cmd-parameter-in-command-module/4660/3 "2024-04-01T15:39:48Z")

</div>

It looks to me like adhoc supports exactly two ways of parsing `-a` values:

1. If the value beings with `{` and ends with `}`, the contents will be parsed as JSON. So `ansible localhost -m command -a '{"cmd": "date"}'` works as expected.

2. Otherwise, `ansible.parsing.splitter.parse_kv()` will be used with `check_raw=True` (since the action is `command`), which only splits off the following parameters: `creates`, `removes`, `chdir`, `executable`, `warn`, `stdin`, `stdin_add_newline`, `strip_empty_ends`. Since `cmd` is not on that list, `cmd=date` will be passed as `_raw_params` on and thus treated as the command to execute.

So if you don’t want to provide JSON, you are limited to a small explicit set of parameters you can use for `command` and all other actions in `MODULE_REQUIRE_ARGS`. The current list of actions is `command`, `win_command`, `ansible.windows.win_command`, `shell`, `win_shell`, `ansible.windows.win_shell`, `raw`, `script` (with `ansible.builtin.` and `ansible.legacy.` prepended to everything that has no `.` and also added to the list).

For all other actions, the complete `-a` value is split up (as expected).

---

<div class="post-metadata">

**Author:** ![bcoca](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/bcoca/32/9269_2.png) [@bcoca](https://forum.ansible.com/u/bcoca)\
**Post date:** [April 1, 2024, 4:17pm UTC](https://forum.ansible.com/t/what-is-free-form-command-and-cmd-parameter-in-command-module/4660/4 "2024-04-01T16:17:40Z")

</div>

I had just finished looking into it deeper, but felix already explained the issue. I’ll be adding notes to the ‘module\_require\_args’ modules to warn about this issue.

---

<div class="post-metadata">

**Author:** ![bcoca](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/bcoca/32/9269_2.png) [@bcoca](https://forum.ansible.com/u/bcoca)\
**Post date:** [April 1, 2024, 4:23pm UTC](https://forum.ansible.com/t/what-is-free-form-command-and-cmd-parameter-in-command-module/4660/5 "2024-04-01T16:23:37Z")

</div>

We also have a fix planned, but it requires many pieces.

- controller argspec parsing
- modules declare free\_form (attribute)"
- use the previous 2 to pass the information to splitter so it knows what are options vs ‘free form’

---

<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:** [April 1, 2024, 7:34pm UTC](https://forum.ansible.com/t/what-is-free-form-command-and-cmd-parameter-in-command-module/4660/6 "2024-04-01T19:34:52Z")

</div>

Is there a way to specify additional task level keywords and values to ad hoc invocations? Lots of them are covered by specific parameters / options to the `ansible` command itself, but more recent ones like `timeout` don’t have obvious ways to twiddle their knobs.

---

<div class="post-metadata">

**Author:** ![shertel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/shertel/32/1241_2.png) [@shertel](https://forum.ansible.com/u/shertel)\
**Post date:** [April 1, 2024, 7:43pm UTC](https://forum.ansible.com/t/what-is-free-form-command-and-cmd-parameter-in-command-module/4660/7 "2024-04-01T19:43:29Z")

</div>

At least in the case of `timeout`, there’s an env var for config [Ansible Configuration Settings — Ansible Community Documentation](https://docs.ansible.com/ansible/latest/reference_appendices/config.html#task-timeout).

`ANSIBLE_TASK_TIMEOUT=10 ansible localhost -m ping`

---

<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:** [April 1, 2024, 7:53pm UTC](https://forum.ansible.com/t/what-is-free-form-command-and-cmd-parameter-in-command-module/4660/8 "2024-04-01T19:53:45Z")

</div>

Ah, true. Yet one more one-off though. It is rather scattered, reflecting the evolutionary nature of the thing.

I’m wondering if the “planned fix that requires many pieces” that @bcoca alluded to might address this more generically.

But on 2nd thought, it may be easier to throw together a simple one-task playbook than deal with yet more rarely needed/used flags.

---

<div class="post-metadata">

**Author:** ![aaron1989041](https://avatars.discourse-cdn.com/v4/letter/a/ee59a6/32.png) [@aaron1989041](https://forum.ansible.com/u/aaron1989041)\
**Post date:** [April 2, 2024, 1:43am UTC](https://forum.ansible.com/t/what-is-free-form-command-and-cmd-parameter-in-command-module/4660/9 "2024-04-02T01:43:33Z")

</div>

> [@felixfontein](#):
>
> a small explicit set of parameters

```auto
# ansible localhost -m command -a '{"cmd": "date"}'
localhost | FAILED | rc=2 >>
[Errno 2] No such file or directory

```

🥲

---

<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:** [April 2, 2024, 4:24am UTC](https://forum.ansible.com/t/what-is-free-form-command-and-cmd-parameter-in-command-module/4660/10 "2024-04-02T04:24:10Z")

</div>

@aaron1989041 which version of ansible-core are you running? I get

```auto
localhost | CHANGED | rc=0 >>
Tue Apr 2 06:23:38 AM CEST 2024

```

when running it 🙂 (using the latest ansible-core devel version right now).

---

<div class="post-metadata">

**Author:** ![aaron1989041](https://avatars.discourse-cdn.com/v4/letter/a/ee59a6/32.png) [@aaron1989041](https://forum.ansible.com/u/aaron1989041)\
**Post date:** [April 2, 2024, 5:36am UTC](https://forum.ansible.com/t/what-is-free-form-command-and-cmd-parameter-in-command-module/4660/11 "2024-04-02T05:36:12Z")

</div>

```auto
# ansible --version
ansible 2.9.27
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.5 (default, Jun 28 2022, 15:30:04) [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)]

```

---

<div class="post-metadata">

**Author:** ![aaron1989041](https://avatars.discourse-cdn.com/v4/letter/a/ee59a6/32.png) [@aaron1989041](https://forum.ansible.com/u/aaron1989041)\
**Post date:** [April 2, 2024, 6:20am UTC](https://forum.ansible.com/t/what-is-free-form-command-and-cmd-parameter-in-command-module/4660/12 "2024-04-02T06:20:28Z")

</div>

```auto
# ansible --version
ansible 2.9.27
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.5 (default, Jun 28 2022, 15:30:04) [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)]

```

---

<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:** [April 2, 2024, 7:13pm UTC](https://forum.ansible.com/t/what-is-free-form-command-and-cmd-parameter-in-command-module/4660/13 "2024-04-02T19:13:58Z")

</div>

@aaron1989041 please note that Ansible 2.9 is quite old and will have been End of Life for almost two years soon ([Releases and maintenance — Ansible Core Documentation](https://docs.ansible.com/ansible-core/devel/reference_appendices/release_and_maintenance.html#ansible-core-support-matrix)).

I would assume that this way of passing JSON arguments was only added in a newer version of ansible-base/ansible-core.

---

<div class="post-metadata">

**Author:** ![jborean](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jborean/32/1525_2.png) [@jborean](https://forum.ansible.com/u/jborean)\
**Post date:** [April 2, 2024, 11:41pm UTC](https://forum.ansible.com/t/what-is-free-form-command-and-cmd-parameter-in-command-module/4660/14 "2024-04-02T23:41:34Z")

</div>

> I would assume that this way of passing JSON arguments was only added in a newer version of ansible-base/ansible-core.

Added in 2.14 [Add support for json in adhoc -a by jborean93 · Pull Request #78114 · ansible/ansible · GitHub](https://github.com/ansible/ansible/pull/78114) (specifically for this use case).

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [May 2, 2024, 11:41pm UTC](https://forum.ansible.com/t/what-is-free-form-command-and-cmd-parameter-in-command-module/4660/15 "2024-05-02T23:41:40Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
