Ansible ships with a number of modules (called the ‘module library’) that can be executed directly on remote hosts or through Playbooks. Users can also write their own modules. These modules can control system resources, like services, packages, or files (anything really), or handle executing system commands.
Let’s review how we execute three different modules from the command line:
ansible webservers -m service -a "name=httpd state=running"
ansible webservers -m ping
ansible webservers -m command -a "/sbin/reboot -t now"
Each module supports taking arguments. Nearly all modules take key=value arguments, space delimited. Some modules take no arguments, and the command/shell modules simply take the string of the command you want to run.
From playbooks, Ansible modules are executed in a very similar way:
- name: reboot the servers
action: command /sbin/reboot -t now
All modules technically return JSON format data, though if you are using the command line or playbooks, you don’t really need to know much about that. If you’re writing your own module, you care, and this means you do not have to write modules in any particular language – you get tho choose.
Most modules other than command are idempotent, meaning they will seek to avoid changes to the system unless a change needs to be made. When using ansible playbooks, these modules can trigger ‘change events’. Unless otherwise noted, any given module does support change hooks.
Let’s see what’s available in the Ansible module library, out of the box:
Manages apt-packages (such as for Debian/Ubuntu).
pkg:
state:
for ‘latest’ (see yum, below) is in work.
Example action from ansible Playbooks:
apt pkg=foo ensure=removed
apt pkg=foo ensure=installed
The command module takes the command name followed by a list of arguments, space delimited.
If you want to run a command through the shell (say you are using ‘<’, ‘>’, ‘|’, etc), you actually want the ‘shell’ module instead. The ‘command’ module is much more secure as it’s not affected by the user’s environment.
The given command will be executed on all selected nodes. It will not be processed through the shell, so variables like “$HOME” and operations like “<”, “>”, “|”, and “&” will not work. As such, all paths to commands must be fully qualified.
This module does not support change hooks and returns the return code from the program as well as timing information about how long the command was running for.
Example action from ansible Playbooks:
command /sbin/shutdown -t now
The copy module moves a file on the local box to remote locations. In addition to the options listed below, the arguments available to the file module can also be passed to the copy module.
src:
dest:
This module also returns md5sum information about the resultant file.
Example action from ansible Playbooks:
copy src=/srv/myfiles/foo.conf dest=/etc/foo.conf owner=foo group=foo mode=0644
Runs the discovery program ‘facter’ on the remote system, returning JSON data that can be useful for inventory purposes.
Requires that ‘facter’ and ‘ruby-json’ be installed on the remote end.
This module is informative only - it takes no parameters & does not support change hooks, nor does it make any changes on the system. Playbooks do not actually use this module, they use the setup module behind the scenes.
Sets attributes of files and directories, or removes files/directories. All parameters available to the file module are also available when running the copy or template modules.
dest:
state:
mode:
owner:
group:
Example action from ansible Playbooks:
file dest=/etc/foo.conf owner=foo group=foo mode=0644
file dest=/some/path owner=foo group=foo state=directory
file dest/path/to/delete state=absent
Deploys software (or files) from git checkouts.
repo:
dest:
version:
Example action from ansible Playbooks:
git repo=git://foosball.example.org/path/to/repo.git dest=/srv/checkout version=release-0.22
Similar to the facter module, this returns JSON inventory data. Ohai data is a bit more verbose and nested than facter.
Requires that ‘ohai’ be installed on the remote end.
This module is information only - it takes no parameters & does not support change hooks, nor does it make any changes on the system.
Playbooks should not call the ohai module, playbooks call the setup module behind the scenes instead.
A trivial test module, this module always returns the integer 1 on successful contact.
This module does not support change hooks and is informative only - it takes no parameters & does not support change hooks, nor does it make any changes on the system.
Controls services on remote machines.
state:
name:
Example action from ansible Playbooks:
service name=httpd state=started
service name=httpd state=stopped
service name=httpd state=restarted
Writes a JSON file containing key/value data, for use in templating. Call this once before using the template module. Playbooks will execute this module automatically as the first step in each play using the variables section, so it is unnecessary to make explicit calls to setup within a playbook.
If facter or ohai are installed, variables from these programs will also be snapshotted into the JSON file for usage in templating. These variables are prefixed with facter_ and ohai_ so it’s easy to tell their source. All variables are then bubbled up to the caller.
anything:
- Any other parameters can be named basically anything, and set a key=value pair in the JSON file for use in templating.
Example action from ansible Playbooks:
vars:
ntpserver: 'ntp.example.com'
xyz: 1234
Example action from /usr/bin/ansible:
ansible -m all setup -a "ntpserver=ntp.example.com xyz=1234"
The shell module takes the command name followed by a list of arguments, space delimited. It is almost exactly like the command module but runs the command through the shell rather than directly.
The given command will be executed on all selected nodes.
If you want to execute a command securely and predicably, it may be better to use the ‘command’ module instead. Best practices when writing playbooks will follow the trend of using ‘command’ unless ‘shell’ is explicitly required. When running ad-hoc commands, use your best judgement.
This module does not support change hooks and returns the return code from the program as well as timing information about how long the command was running for.
Example action from a playbook:
shell somescript.sh >> somelog.txt
Templates a file out to a remote server. Call the setup module prior to usage if you are not running from a playbook. In addition to the options listed below, the arguments available to the file module can also be passed to the copy module.
src:
dest:
This module also returns md5sum information about the resultant file.
Example action from a playbook:
template src=/srv/mytemplates/foo.j2 dest=/etc/foo.conf owner=foo group=foo mode=0644
Creates user accounts, manipulates existing user accounts, and removes user accounts.
name:
comment:
gid:
shell:
createhome:
password:
state:
Example action from ansible Playbooks:
user name=mdehaan comment=awesome passwd=awWxVV.JvmdHw createhome=yes
user name=mdehaan state=absent
Will install, upgrade, remove, and list packages with the yum package manager.
pkg:
state:
list:
Example action from ansible Playbooks:
yum pkg=httpd ensure=latest
yum pkg=httpd ensure=removed
yum pkg=httpd ensure=installed
See also