• Important: We recently upgraded our forum software - please let us know if you run into any issues.

Getting started with ansible

dos2unix

Member
First you need to make sure you have python installed. But I really can't think of any modern distros that
don't come with python installed. You can check this by typing....

python -V (make sure you use an upper case "V")

This should return something like
> Python 2.7.16

You can also use python3

python3 -V
> Python 3.7.3

If you accidentally typed a lower case "v" and you ended up in a python shell your prompt will change to something like this

>>>>

If that happens, just type quit() ...with the parenthesis.
To exit the python shell.

The next requirement is a C/C++ compiler. You don't really need to know how to do any C/C++ programming,
but this used to compile python modules that you will need to download.

For Fedora/Redhat/CentOS you can use...

sudo dnf group install -y "C Development Tools and Libraries" (for older versions use yum instead of dnf)

For Ubuntu you can use...

sudo apt install build-essential

The third requirement is something called pip. This is like apt-get or yum for python modules.

For Fedora/Redhat/CentOS you can use...

sudo dnf install python3-pip (if you don't have python3 installed just use python2-pip )

For Ubuntu you can use...

sudo apt-get -y install python3-pip

Now all of the requirements are done. Let's install ansible.

pip3 install ansible

This will take several seconds or so depending on your network speed and computer.

Now we will run your first ansible command.

ansible localhost -m setup

The will return several hundred lines of output about your computer. Maybe even some things you didn't know about your computer.

By default this outputs in JSON format. But there are ways to change that.

Next we will learn how to optimize ansible, and use playbooks.
 
Last edited:


wizardfromoz

Super Moderator
Staff member
Gold Supporter
Nice Thread, @dos2unix :). Maybe in your next Post, you could just explain a little to The Viewers, what ansible is

Cheers

Wizard
 

dos2unix

Member
Good point. (assumptions are dangerous).

Ansible is a tool that combines ssh and and python to run various commands. If you don't have ssh access or python installed you won't really be able to run ansible.
This was developed with cooperation from many Redhat developers, but it is opensource. Ansible is most commonly used in two ways (or a combination of both).

If you managing many systems, (say a datacenter with hundreds of computers in it, or perhaps a classroom with 30 or so computers in it) ansible lets you run the
same set of commands on all of these computers all at once. Obviously this saves you from having to manually login to hundreds of computers and run the same
command over and over again. The other way it is used, is to run the same command frequently on a remote computer(s). Kind of like "cron" for a remote system.
You can edit files, change configurations, stop and start services, do upgrades and more all from a central console.

Not only can you configure hundreds of computers at once, but you can also query the current configuration of them. What is the IP address? Netmask? Gateway?
What kernel is it running? How much RAM does it have? How hot are the CPUs running? How full are the hard drives? Does that account I deleted last week still
exist on any of these computers?

Most of the time when you run ansible you will be running it in a "playbook". Playbooks are are written in yaml. This may sound difficult, but once you see some examples
you might not think it is that difficult. One thing to know about yaml files, is that they are very sensitive to indentations. They don't like tabs as spaces, but rather have you press
the space key as many times as is necessary. A lot of the syntax depends on indentation spacing.

A lot of the power of ansible comes from modules. Many are built-in, but some have to be downloaded after everything else is installed. Here is a web-page with a list of
the modules. https://docs.ansible.com/ansible/latest/modules/list_of_all_modules.html

Here is an example of a simple yaml playbook

---
- name: Print Facts
hosts: all
vars:
validate_certs: "no"

tasks:

- name: Display Unit Info
debug:
msg: " Hostname: {{ ansible_hostname }}, Date: {{ ansible_date_time.iso8601 }}, IP Address: {{ ansible_default_ipv4.address }},
Gateway: {{ ansible_default_ipv4.gateway }}, Netmask: {{ ansible_default_ipv4.netmask }},
Memory: {{ ansible_memory_mb.real.total|round|int }}, CPU Cores: {{ ansible_processor_vcpus }}, Python Version: {{ ansible_python_version }},
MAC Address: {{ ansible_default_ipv4.macaddress }}, Kernel Version: {{ ansible_kernel }},
Linux Version: {{ ansible_distribution }} {{ ansible_distribution_version }}"
...

Notice the indentations. These are required to be this way. Remember use the space key, not the tab key. Normally if the next line needs to be indented,
it will be indented two spaces. Yaml, files typically start with 3 dashes. "---" and end end with 3 dots. "..."
They will sometimes works even if you don't follow these conventions, but since the spec calls for this, it's probably a good habit to get into.

You can put the playbook file in any directory you want to. It doesn't really matter where it goes too much. But usually you will build up a collection of playbook files
you want run for different systems. For example the web servers running httpd or nginx, will likely have a different playbook that the database servers running postgresql.
We can name this playbook something like "general_linux_info.yml"

You need to a create host file that looks like this... (replace the IP addresses with whatever you have)

[webservers]
10.1.0.1
10.1.0.2
10.1.0.3
[databases]
10.1.0.7
10.1.0.8
10.1.0.9

If you don't have a lot of virtual machines or servers, you can do this with even a single computer, just create a text file with your IP address in it and nothing else. For example...

192.168.0.5

You can use your own local IP address if you want to.
Name it "ansible_hosts" if you want. The name doesn't really matter. You should put this in the same directory as your playbooks.

Make sure you are in the directory where the playbook yaml and host file is, you run the playbook like this...

sudo ansible-playbook general_linux_info.yml -i ansible_hosts -v

This will create an output like this.

TASK [Display Unit Info] **********************************************************************************************
ok: [10.10.38.37] => {
"msg": " Hostname: my-computer, Date: 2019-05-13T15:05:14Z, IP Address: 10.0.3.37, Gateway: 10.0.3.1, Netmask: 255.255.255.224, Memory: 15923, CPU Cores: 4, Python Version: 2.7.16, MAC Address: b8:xx:xx:xx:xx:a1, Kernel Version: 5.0.13-300.fc30.x86_64, Linux Version: Fedora 30"
}

If you have 30 computers in your host file you created, you will see 30 lines like this.
In my playbook above, I have a line that says "hosts: all". I could change this to "hosts: webservers" and the script would only run against my web servers, not my database servers.

If you want a lot more control over how ansible works... make a directory called /etc/ansible
Then download this file there. https://raw.githubusercontent.com/ansible/ansible/devel/examples/ansible.cfg
Name it ansible.cfg

For the most part the flags and options in this file are self explanatory, but I will go over most of them in the next post.
One way to make sure ansible is using your configuartion file you just installed is to use the version command.

[[email protected] playbooks]# ansible --version
ansible 2.7.10
config file = /etc/ansible/ansible.cfg
configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /usr/local/lib/python3.7/site-packages/ansible
executable location = /usr/local/bin/ansible
python version = 3.7.3 (default, Mar 27 2019, 13:36:35) [GCC 9.0.1 20190227 (Red Hat 9.0.1-0.8)]

You should see an output similar to the above. Note whether the config file is empty or has a file listed.
That's all I have time for right now.
 

Members online


Top