Getting started with Ansible

Ansible is a great tool to automate the repetitive tasks on your servers. The target nodes will often be Linux but can also be Windows. The management server must be run on Linux.

Installation on a Linux CentOS 7 server.
The Ansible binaries are in the Extra Packages for Enterprise Linux rpm (EPEL).

sudo yum -y update
sudo yum -y wget
wget https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
sudo rpm -iUvh epel-release-latest-7.noarch.rpm

Install Ansible

sudo yum -y install ansible
ansible --version

Create a first playbook script.
Ansible works with tasks in a file called a playbook. The nodes are defined in a hosts file and can be organised in a group. The default location is in /etc/ansible/hosts. Let’s add a Linux server to the Ansible hosts file.

[testservers]
192.168.178.11

Check if we can reach the server.

ansible testservers -m ping --ask-pass --user centos

Create our first playbook test.yml. Note that the position of the statements in a line are important similar as in Python. You can use -vvvv instead of –verbose to see more debugging information.

- hosts: testservers
  vars:
    directory: /home/centos

  tasks:
  - name: Test
    command: "ls {{directory}}"

And run it:

ansible-playbook -i /etc/ansible/hosts --ask-pass --user centos test.yml --verbose

Or create a playbook update-servers.yml which updates your Linux server.

- hosts: testservers
  tasks:
  - name: Upgrade all packages
    yum: name=* state=latest
ansible-playbook --ask-pass --user centos update-servers.yml --verbose

In this post we only touch the surface of Ansible but it might give you an idea what is possible.

References:
https://www.ansible.com/
https://docs.ansible.com/

 

No Comments

Post a Comment