DEV Community

Cover image for ANSIBLE FOR BEGINNERS.
Gloria Adhiambo
Gloria Adhiambo

Posted on

ANSIBLE FOR BEGINNERS.

Introduction

Ansible is an IAC tool that uses YAML and Jinja2 templates in automation of infrastructure processes e.g provisioning, configuration management, and service deployment. Ansible can be run from ansible tower or using two types of machine i.e control node and managed node. A control node is where you install ansible and ansible configs. A managed node is the node that you need to apply changes to listed in the inventory file in ansible configuration. Control node connects to the managed nodes using ssh connections and executes modules (a piece of code used that uses specific arguments.)

Installing ansible:

Note: Ansible is installed in the control node.
Ansible can be installed using pip. This also ensures Python dependencies are installed.

Image description

Use pip to install ansible as shown below:

Image description

Installing on Rhel use:

Image description

On Ubuntu use:

Image description

Check if ansible is installed correctly:

Image description

After Installation, ansible configuration file is stored in /etc/ansible/ansible.cfg. The config file contains sections including defaults and privilege escalation i.e:

[defaults]
inventory = path to your inventory(/etc/inventory)
remote_user = user for ssh login
ask_pass = true

[privilege_escalation]
become = true (user can sudo)
become_method = sudo
become_user = root
become_ask_pass = true.

USING ANSIBLE

With ansible installed, we can now run ad hoc commands or run ansible tasks. Ad hoc commands execute single ansible tasks faster i.e ansible host-pattern -m module [-a 'module arguments'] [-i inventory]
An example ad hoc command to add users: ansible -m user -a β€œname=webber state=present -i hosts. Where -m stands for module in this case user, -a stands for arguments and -i for inventory/ hosts.

Use modules to run easy tasks for beginners. Use a file saved as a yaml file for example playbook.yml.
Use ansible-playbook playbook.yaml to run the file. Below is a snippet of different modules you can use.

Image description

Common Ansible Terms:

1. Modules: a piece of code used that uses specific arguments for example the file, yum, user e.tc modules.

2.Task: As shown above, a task is the section in ansible that outlines a single procedure to be completed by ansible.

3. Inventory: The /etc/inventory file or any other file referencing the location of managed hosts.

4. Playbook: A file that contains one or more plays/tasks to be run in order. The snippet above is a simple playbook.

5. Facts: Contains information gotten from managed hosts during execution of an ansible file. For example, successful ssh connections, changes made or not made to the infra etc.

6. Roles: Makes ansible easier to use and reuse by using a directory structured approach to ansible tasks. The directories used in roles include: defaults, handlers, meta, tasks, templates, and vars.

7. Ansible Galaxy: public library of Ansible content. Can be used to import roles in the registry to your local machine.

References & Resources

β€’ Ansible Community Documentation: [https://docs.ansible.com/ansible_community.html)

Disclaimer

Opinions presented in this article are personal and belong solely to me, and do not represent people or organizations associated with me in a professional or personal way. All the information on this site is provided "as is" with no guarantee of completeness, accuracy or the results obtained from the use of this information

Top comments (0)