[Ansible] – Running commands and using modules

Following the syntax:

ansible hostgroup -m module [-a ‘module arguments’] [-i inventory]

Note that it will allow you executed actions in the host via the module chose. For example:

$ansible all -m ping

The above command will call the module ping for all hosts in the inventory. Which means that it will return a JSON output with the result of a ping into each one as follows.

myserver.domain.com | SUCCESS => {
    "changed": false,
    "ping": "pong"

Following another example.

$ansible webservers -m command -a ‘cat /tmp/myfile’ -u myuser

The above command will execute the command ‘cat /tmp/myfile’ with the user ‘myuser’ for each host defined in the group [webservers] in the inventory of this configuration.


The modules contain implementations of actions that can be helpful and useful. For example, the module command will allow you to execute commands as for example cat, cp, and mv, and ls -la, etc …

The module shell will open a $bin/bash shell. Following the link for its documentation.

Note that in the same way that these modules can be used in the command line they can be used in the ansible scripts like the following example.

- name: return the content of the file
  command: cat /tmp/myfile
  register: myfile

Check here the list of all modules available and see Adding modules and plugins locally to know how to use your own modules locally. Also, it is possible developer modules to be used with Ansible.




Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s