Share this page : facebooktwitterlinkedinmailfacebooktwitterlinkedinmail

Installs or removes packages using the apt package manager.
Copies a file from local machine to the hosts.
Sets the attribute of a file, symlink, or directory.
Starts, stops, or restarts a service.
Generates a file from a template and copies it to the hosts.

To get help documentation for the modules, use command:

ansible-doc module_name


Work with commands

When invoking this module, you also need to pass an argument to the module with the -a flag, which is the command to run.

For example, to check the uptime of our server, we can use:

$ ansible testserver -m command -a uptime

Output should look like this:

testserver | success | rc=0 >>
17:14:07 up 1:16, 1 user, load average: 0.16, 0.05, 0.04

The command module is so commonly used that it’s the default module, so we can omit it:

$ ansible testserver -a uptime
Work with apt

If we need root access, we pass in the -s flag to tell Ansible to sudo as root. You will also need to specify the username and password by -u username and --extra-vars "ansible_sudo_pass=yourPassword"

$ ansible testserver -s -m apt -a name=nginx -u sudoer --extra-vars "ansible_sudo_pass=sudoer_password"

To tell Ansible to do the equivalent of apt-get update  before installing the package, change the argument from name=nginx to "name=nginx update_cache=yes"

Work with services

We can control the service via Ansible as well:

ansible test -s -m service -a "name=nginx state=started" -u sudoer --extra-vars "ansible_sudo_pass=sudoer_password"

The action include:

  • started
  • stopped
  • restarted
  • reloaded
  • running



We will deploy nginx on servers and change the configuration file, then copy a home page to the server.

Before the example work, just notice the following:

  • under user home folder, I have following folders: files – for nginx configuration ; templates – for home web page;
  • We also have two files: web-notls.yml – the playbook; hosts – the target hosts to deploy.

The configuration file: ~/files/nginx.conf

server {
 listen 80 default_server;
 listen [::]:80 default_server ipv6only=on;
 root /usr/share/nginx/html;
 index index.html index.htm;
 server_name localhost;
 location / {
 try_files $uri $uri/ =404;

The Template ~/templates/ index.html.j2.

<title>Welcome to ansible</title>
<h1>nginx, configured by Ansible</h1>
<p>If you can see this, Ansible successfully installed nginx.</p>
<p>{{ ansible_managed }}</p>

The hosts file:

testserver ansible_ssh_host= ansible_ssh_port=22

The playbook:

Note that the space matters for the ymal format, make sure it’s exactly like the format bellow.

- name: Configure testserver with nginx
  hosts: webservers
  sudo: yes
    - name: install nginx
      apt: name=nginx update_cache=yes
    - name: copy nginx config file
      copy: src=files/nginx.conf dest=/etc/nginx/sites-available/default
    - name: enable configuration
      file: >
    - name: copy index.html
      template: src=templates/index.html.j2 dest=/usr/share/nginx/html/index.html
    - name: restart nginx
      service: name=nginx state=restarted

Then use the command:

ansible-playbook -i hosts web-notls.yml -u administrator --extra-vars "ansible_sudo_pass=admin_password"