Modules

apt
Installs or removes packages using the apt package manager.
copy
Copies a file from local machine to the hosts.
file
Sets the attribute of a file, symlink, or directory.
service
Starts, stops, or restarts a service.
template
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

usually you will need to use password for the action, add -k (ask-pass) switch for this.

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
Copy file and delete file

You can copy the ansible config file to the target server using below command:

ansible testserver -k -m copy -a 'src=/etc/ansible/ansible.cfg dest=/tmp/ansible.cfg'

Then delete it by below command:

ansible testserver -k -m command -a 'rm -rf /tmp/ansible.cfg removes=/tmp/ansible.cfg'

 

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

 

Example:

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.

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

The hosts file:

[webservers]
testserver ansible_ssh_host=192.168.10.73 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
  tasks:
    - 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: >
        dest=/etc/nginx/sites-enabled/default
        src=/etc/nginx/sites-available/default
        state=link
    - name: copy index.html
      template: src=templates/index.html.j2 dest=/usr/share/nginx/html/index.html
        mode=0644
    - 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"