create a directory test at /home/user using ansible-adhoc command

Viewed 3805

I am trying to create directory using below command

ansible app -m file -a "path=/home/user/test mode = 777 state = directory" -b

I am getting below error message. Could any one advise me what I am doing wrong here?

ERROR! this task 'file' has extra params, which is only allowed in the following modules: ansible.builtin.raw, ansible.legacy.add_host, ansible.builtin.meta, ansible.legacy.include, ansible.legacy.import_role, script, ansible.legacy.raw, group_by, ansible.builtin.shell, ansible.legacy.win_command, include, shell, include_vars, ansible.builtin.import_tasks, add_host, ansible.builtin.include_vars, ansible.legacy.include_role, ansible.builtin.include_role, ansible.legacy.include_vars, ansible.legacy.win_shell, ansible.legacy.group_by, import_tasks, ansible.builtin.set_fact, ansible.builtin.command, ansible.builtin.include_tasks, include_tasks, ansible.builtin.script, ansible.builtin.include, raw, meta, ansible.legacy.set_fact, ansible.builtin.add_host, ansible.legacy.script, ansible.legacy.import_tasks, win_command, ansible.builtin.win_shell, include_role, win_shell, set_fact, ansible.legacy.shell, ansible.legacy.command, import_role, ansible.legacy.meta, ansible.builtin.import_role, ansible.legacy.include_tasks, ansible.builtin.group_by, ansible.builtin.win_command, command

3 Answers

The simplest example of an ad hoc command to create a directory using Ansible is

ansible nodes -a "mkdir /BYANSIBLE"

Try this:

ansible app -m file -a "dest=/home/user/test state=directory"

or

ansible localhost -m file -a "dest=/home/user/test state=directory"

In general, for an Ansible ad-hoc command, any module arguments are supplied using the -a option, as one single string. Due to this, the way Ansible distinguishes the individual arguments, is by spaces.

You need to remove the extra spaces you have on either side of the equal signs for the ad-hoc command to work.

Hence, the following would work:

ansible app -m file -a "path=/home/user/test mode=755 state=directory" -b

Also, check out some of the related examples in the Ansible docs.

Side note: I've modified the permissions to 755 (read/write/execute for owner + read/execute for group & others). Its almost never a good idea to give 777 permissions (full read/write/execute) on directories.

Related