Ansible Command Module With Examples

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:

ansible web -m command -a "cp /tmp/devops.txt  /home/project/devops.txt"

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:

- name: copy a file by using ansible command module 

  command: cp /tmp/devops.txt /home/project/devops.txt

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:

- name: Execute the script by using ansible command module
  command: "/bin/sh {{ jboss_dir }}/bin/add-user.sh”

it will execute that add-user.sh 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 :

- name: running command module with args
 
  command:  /usr/bin/add_user.sh
 
  args:
 
    creates: "/devops/jboss/abc.txt"

here add_user.sh 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.

creates:

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.

chdir:

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:

- name: ansible command module with_items

  command: "{{ item }}"

  with_items:

    - cp /tmp/httpd.service /usr/lib/systemd/system/httpd.service

    - mv /usr/lib/systemd/system/httpd.service /usr/lib/systemd/system/httpd.service.bk

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:

- name: Running "make install" for httpd 2.4

  command: 'make install chdir=/usr/local/src/httpd-2.4/'

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

Leave a Reply

Your email address will not be published. Required fields are marked *