Ansible Ansible Ansible - Part 1
November 14, 2020
ansible devops debianOf all the Devops tools I have learnt this year, Ansible is my favourite. The ability to connect to different servers and perform different tasks in a way you can expect is superb. I have installed different flavour Linux Virtual Machines alongside my Raspberry Pi's and it connects to each one and does its thing as I expect.
The idea of automating everything, having it act in an expected and repeatable way is superb. I have written bash scripts that try to do this in the past, but fall over at scale and repeatability. Ansible is great for this.
In the process of learning ansible I have an inventory with 1 control node and 4 machines
[x] localhost (debian 10) [Control node]
[x] virtual machine (centos 8)
[x] virtual machine (ubuntu 20)
[x] virtual machine (alpine linux 3.12)
[x] raspberry pi (raspbian)
With this as my personal cloud, i can connect with ansible via ssh and run a whole range of tasks including:
[x] install and configure a webserver, mysql and install a laravel github repo
[x] run a git pull to update the repo
[x] update the OS and software
[x] install a docker container on a server
[x] check the status of the machines
Ansible provides a number of Facts for a machine which can be seen with this command:
ansible localhost -m setup --tree /tmp/facts
it is a json file of around 600 lines of Facts including for my localhost machine:
"ansible_facts":
{
"ansible_os_family": "Debian",
"ansible_architecture": "x86_64",
"ansible_memtotal_mb": 7417,
}
There is also extensive details about hardrives, IP addresses, python versions, users and the computer in general.
Similarly, Ansible provides details of the services running on the machine:
ansible localhost -m service_facts --tree /tmp/services
it provides a 1000 line Json file, with details of the services running on the machine, here is an example of the result:
"ansible_facts":
{
"services":
"apache2":
{
"name": "apache2",
"source": "sysv",
"state": "running"
},
"apache2.service":
{
"name": "apache2.service",
"source": "systemd",
"state": "running",
"status": "enabled"
},
}
Also available is a list of package facts
ansible localhost -m package_facts --tree /tmp/package
This resulted in a json file with over 20000 lines, with details of everything installed on the machine, here is a section from the result:
"ansible_facts":
{
"packages":
{
"apache2": [
{
"arch": "amd64",
"category": "httpd",
"name": "apache2",
"origin": "Debian",
"source": "apt",
"version": "2.4.46-1"
}],
"zeal": [
{
"arch": "amd64",
"category": "doc",
"name": "zeal",
"origin": "",
"source": "apt",
"version": "1:0.6.1-1+b1"
}],
}
}
This provides a great deal of info about the machine, from which you could manage the the machines with conditionals like this.
- hosts: localhost
roles:
- role: debian_stock_config
when: ansible_facts['os_family'] == 'Debian'
One thing I really like about Ansible is it is pretty obvious what the code is doing, and logical in how it does it. All these commands in this first part are ad-hoc, as in they are single commands you run in the command line.
If you would like to contact me with this form on londinium.com, ilminster.net or via Twitter @andylondon