Skip to main content

Ansible


Ansible Introduction Ansible Work

Server Automation


Ansible designed as a server automation tool.

  • It runs one task on many servers instead of one at a time.

Modules


Modules are the backbone of the ansible. Each module is a discrete unit of code that executes a command and returns data.

  • It is the thing actually doing the work

Service module manipulates services The comman module runs arbitary commands. The yum module can install, remove or otherwise manipulate software packages.

tip

Modules can be thought of as similar to plugins.

Modules all have different flags and variables that can and should be set so that they can do thier job.

Modules should be Idempotent and it returns JSON data

You can use ansible-doc <<module-name>> to get more information about specific modules.

$ ansible-doc -l | wc -l (this will return total modules)

$ ansible-doc -l

$ ansible-doc -l | grep docker

$ ansible-doc -l | grep docker-swarm

Playbooks


Ansible groups different commands and tasks together in playbooks.

  • Comparable to bash script but significantly more powerful and customizable.

Idempotent


The key concept of ansble is Idempotency.

  • Changes only happen once regardless of the number of times it's run.

Ad-Hoc Commands


Outside the playbooks you can run ad-hoc commands. comparable to one line commands in bash but more powerful.


syntax: ansible [pattern] -m modulename -a "module options"
Ex: ansible webservers -m service -a "name=httpd state=restarted"

$ ansible all -m firewalld -a "service=https permanent=yes state=enabled" -b
$ansible all -m firewalld -a "port=8080/tcp permanent=yes state=enabled" -b

Agentless


Unlike other automation tool ansible is agentless.

  • it doesn't require additional software on the target servers.

Sample Playbook


$ vim nginx.yml

Ansible Nginx Playbook

$ ansible-playbook nginx.yml

$ ansible all -m setup

=> Installation



$ ssh-keygen

$ ssh-copy-id userid@localhost

$ sudo yum install ansible -y

$ ansible --version

$ sudo vim /etc/ansible/hosts

add 127.0.0.1 and save

$ ansible all -m ping

$ ansible all --list-hosts

$ ansible webservers --list-hosts