ansible command module

Ansible Command Module:

Ansible command module is used to run any commands or run any scripts in the remote target machine.

With the command module, you can run any Linux or shell commands in the target machine.

ansible command module ad hoc:

here web is my target server hostname, you can mention your server hostname or group name, so this command will execute on this server.

-m represents module name

-a represents arguments

copy a file by using ansible command module:

when you execute this task the devops.txt file in tmp will be copied to /home/project in the remote target server.

so this will completely be executed on the target server(machine)

NOTE: this task is not copying a file from ansible master to target server, this file copying from one location to another location in target server(machine).

run a script by using ansible command module:

it will execute that shell script

here jboss_dir is a  variable. this variable value is defined in the default.yml file in the role or any other variable files.

running ansible command module with creates args :

here script will run only when /devops/jboss/abc.txt doesn’t exist

if the file has already existed then this task will skip

here creates is an argument, you can pass chdir argument depends on your requirement.


in this argument we will provide the path of a file, If the file already exists then this step won’t be run, it will skip that task.


here we will provide a path to the directory, so the commands will execute in this directory or Change the shell into this directory before running the command.

ansible command module with multiple commands or with_items:

It will Copy the httpd_service file from temp to system and

It will rename the file from httpd.service to httpd.service.bk in the remote target server.

so we use with_items to run multiple commands in a loop. here you can pass any number of commands depends on your requirement.

ansible command module with chdir:

it will execute ’make install’ command in /usr/local/src/httpd-2.4/   not in ansible default user directory

or shell will be changed into this directory before running the make install command.



  • ansible command module with arguments
  • multiple arguments ansible command module output
  • examples  ansible command module multiple commands
  • command module ansible playbook example
  • ansible command module multiple lines
  • ansible command module ignore the error