Table of contents
1.
Introduction🧑‍🏫
2.
Installation Process
2.1.
Control Machine Requirements
2.2.
Installation through Apt on Ubuntu Machine
3.
YAML Syntax
4.
YAML Basics
5.
Frequently Asked Questions
5.1.
Can Ansible set an environment variable?
5.2.
How can I view Ansible environment variables?
5.3.
What is an Ansible configuration file?
5.4.
What is the language used by Ansible?
5.5.
Is coding necessary for Ansible?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Ansible Environment Setup

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction🧑‍🏫

So, are you wondering what Ansible is? And how can it help in the deployment of various IT tools? Let’s explore and discuss Ansible Environment Setup.

Ansible Environment Setup

Before discussing Ansible Environment Setup, firstly let us know about Ansible. Ansible is an open-source IT engine that automates the deployment of many IT tools, including intra-service orchestration, cloud provisioning, and application deployment. It has no agents or custom security infrastructures, making deployment simple.

Ansible uses playbooks to describe automation jobs, and playbooks use YAML, a straightforward language that is simple for humans to understand, read, and write.  YAML is a human-readable data serialization language that is frequently used for configuration files, but it can be used in various applications where data is stored. The advantage is that even IT infrastructure support guys can read, understand, and debug the playbook as necessary (YAML – It is in human-readable form).

Ansible

Ansible is designed for multi-tier deployment. Ansible models IT infrastructure by describing how all systems are interrelated rather than managing one system simultaneously. Because Ansible is entirely agentless, it connects your nodes using SSH(by default). However, Ansible allows you to use the option of using another connection method, such as Kerberos.

Ansible pushes "Ansible Modules," which are small programs, after connecting to your nodes. When the modules have finished running on your nodes, Ansible will remove them. Simple text files are used by Ansible to manage your inventory (These are the hosts' files). Ansible uses a hosts file where users can group hosts and use playbooks to control specific groups' actions.

Now we will learn about the Ansible Environment Setup. 

Installation Process

Installation Process

When we talk about deployment, there are primarily two types of machines −

  • Control machine − a machine that enables us to manage other machines.
  • Remote machine − machines handled or controlled by a control machine.

A single control machine can handle several remote machines. Therefore, we need to install Ansible on the control machine to manage remote machines.

So let us first understand Control Machine Requirements.

Control Machine Requirements

Control Machine Requirements

Any machine with Python 2 (versions 2.6 or 2.7) or Python 3 (versions 3.5 and higher) installed can run Ansible. Ansible, by default, manages remote machines via ssh.

No databases are added by Ansible. It cannot be started or run without any daemons. Ansible does not leave any software running or installed on remote machines while managing them. Therefore, while moving to a new version, there is no question about how to upgrade it. The latest release can be installed via Apt, yum, pkg, pip, OpenCSW, pacman, etc.

Installation through Apt on Ubuntu Machine

ubuntu

You need configure PPA on your machine in order to install Ansible. You should run the next line of code for this −

$ sudo apt-get update 
$ sudo apt-get install software-properties-common 
$ sudo apt-add-repository ppa:ansible/ansible $ sudo apt-get update 
$ sudo apt-get install ansible

Hurray! Ansible Environment Setup is all done. Now you are ready to use Ansible to manage remote machines after running the above line of code. To determine the version and if Ansible was properly installed or not, simply run ansible-version.

YAML Syntax

YAML can be understood as a human-readable data serialization language that is generally used for writing configuration files.

YAML Syntax

YAML is an abbreviation that highlights that it is a markup language, not a document, depending on who you ask. It can also stand for yet another markup language or YAML ain't markup language. It could be utilized to store data.

This is a short overview to correct YAML syntax, which is used to express Ansible playbooks (our configuration management language).

We favor YAML over other commonly used data formats like XML or JSON because it is easier for people to read and write. Additionally, most programming languages include libraries that can be used to work with YAML.

To see this in practice, you might also want to read Working with Playbooks at the same time.

YAML Basics

Almost all YAML files for Ansible start with a list. The items in the list are all lists of key/value pairs, often called hashes or dictionaries. As a result, we must have understood how to write lists and dictionaries in YAML.

YAML has one more small quirk. Regardless of whether they are associated with Ansible or not, all YAML files may optionally start with “ —-” and end with “ ....”. This indicates the start and end of a document and is a part of the YAML format.

A list's members are all lines that start with a "-" (a dash and a space), starting at the same level of indentation:

---
# A list of employees
- Angela
- Oliver
- Sussain
- Mukti
...

 

YAML uses simple key-value pair to represent the data. The representation of  the dictionary is in key: value pair (the colon must be followed by a space):

Example: An Employee record

--- #Optional YAML start syntax 
John: 
   name: John Doe
  Employee id: 22674 
   Candidate id: 2488876 
   sex: male 
… #Optional YAML end syntax 

These dictionaries and lists can also be written in an abbreviated form:

---
Jon: {name:Jon Doe, job: Developer, skill:Wordpress}
Employees: ['Angela', 'Oliver', 'Steeve', 'Manisha']
…

These are the called “Flow collections”.

Now that we know lists and dictionaries, let us write List inside Dictionaries where the value of a key is the list.

---  
Jon: 
   name: Jon Doe 
   employee id: 22865 
   job: front-end developer 
   sex: male 
   skills: 
      - Html
      - Css 
      - Javascript
… 

Similarly, we can also make a list of dictionaries by repeating the upper syntax.

We can also represent Boolean (True/false) values in YAML, where boolean values can be case insensitive. Let's see an example:

create_key: yes
needs_agent: no
knows_oop: True
likes_emacs: TRUE
uses_cvs: false

 

If you want to be compatible with the default yamllint options, use lowercase "true" or "false" for boolean values in dictionaries.

That’s all you need to know about YAML to start writing Ansible playbooks.

Now it’s time for the questions. Let us now move to FAQs.

FAQs

Frequently Asked Questions

Can Ansible set an environment variable?

At the task level, you can directly do the Ansible Environment Setup. By defining environment settings as variables in your play and using them in a task the same way you would any stored Ansible variable, you may reuse environment settings. By defining environment settings in a group vars file, you may store them for reuse across multiple playbooks.

How can I view Ansible environment variables?

The 'lookup' plugins, which let you access system data, and the 'env' plugin, which was written for accessing the environment variables, can be used to access the environment variables that are available on the local server.

What is an Ansible configuration file?

This file governs the behaviour of all interactions made by the control node, is the brain and the heart. The default configuration file for Ansible is located in the /etc/ansible/ansible. cfg.

What is the language used by Ansible?

Ansible uses YAML templates that are readable by humans so that users can program repetitive tasks without needing to learn an advance programming language. You can write your own modules or use the ones that come with Ansible to automate tasks.

Is coding necessary for Ansible?

Ansible is an open-source, free application that is simple to set up and utilize. Playbooks from Ansible don't need any specialised coding knowledge. Without the necessity for configuration files, Ansible may be used to perform basic tasks like rebooting from the command line or ensuring that a service is operating.

Conclusion

In this article, we studied about one of the automation engines, which is called Ansible and Ansible Environment Setup. We also discussed the Ansible YAML Syntax and YAML Basics. You can also go through the given articles if you are

eager to enhance your knowledge regarding Ansible.

Ansible interview questions part-1

Ansible interview questions part-2

Ansible interview questions part-3

 

Thank you img

 

Please refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. And also, enroll in our courses and refer to the mock test and problems available. Have a look at the interview experiences and interview bundle for placement preparations.

Do upvote our blog to help other ninjas grow.

Happy Learning!

 

Live masterclass