BACK TO ARTICLES

Ansible Automation: Variables Introduction & How to Create Bulk Users in CentOS Linux Using Variables? (Step By Step)

Published on August 24, 2020

Fakhar ul Hassan

Infrastructure Consultant, Cloud Architect & Automation, DevOps, Infrastructure as Code (IaC), Ansible, Terraform

Ansible Automation: Variables Introduction & How to Create Bulk Users in CentOS Linux Using Variables? (Step By Step)

Introduction to Ansible Variables

=================================

To store values in Playbooks, Ansible provides the facility of using variables. With the use of Ansible variables, the creation and maintenance of Ansible projects becomes very easy.

The Ansible variables can be used in many ways, some of which are listed below:

1- Package Installation/Removal 

2- User Creation/Deletion

3- Files Copying/Removal

4- Services Start/Stop

etc.

Variables Naming

=================

Variable names can be a combination of letters, numbers and underscores only. It is important to note that the variable names can only start with letters only.

Variables Scope

================

  1. Global Scope Variables

   The global scope Ansible variables are set by:

  • Ansible Configuration
  • Command Line
  • Environment Variables
  1. Host Scope Variables

The host scope Ansible variables are directly associated with individual hosts, for example:

  •  Host Inventory Variables
  •  Host Fact Gathering Variables
  •  Host Registered Task Output Variables

 3. Play Scope Variables

The play scope Ansible variables are set in the play or in the related structures.

Variable Precedence

=====================

Following is the Ansible variable precedence (from highest to lowest):

No alt text provided for this image

Note: For detailed description of Ansible variable precedence, please visit Ansible Documentation

How to Create Bulk Users Using Variables? (Step By Step)

===========================================================

No alt text provided for this image

Prerequisites:

  1. One Ansible Control Node
  2. Two Ansible managed hosts (You may use as many as you want)
  3. Network access between control node and managed nodes
  4. Host names of all three nodes should be registered with DNS server or appropriate entries should be present in the /etc/hosts files (on all three nodes).
  5. User SSH keys should have already been generated at control node and shared with managed nodes (see this article to configure SSH Keys: http://tiny.cc/ro75fz
  6. A group named ‘testgroup’ has been created on both target managed hosts (i.e. ‘CentOS-ManHost-160’ and ‘CentOS-ManHost-170’).
  7. User ‘fakhar’ has been made a sudo user on both target managed hosts (i.e. ‘CentOS-ManHost-160’ and ‘CentOS-ManHost-170’).

Note: In this article, we have used one user “fakhar” on all three nodes. Its SSH keys have been generated at control node and shared on both managed hosts.

Step-1: Create an inventory file at control node (i.e. CentOS-Ctrl-Node), containing the host names of both managed servers, as shown in below image:

No alt text provided for this image

Step-2: Write an Ansible playbook, to create bulk users using variables, as shown below:

No alt text provided for this image

Step-3 (Optional): Run the ‘tree’ command at control node, where your playbook is placed (this is just to show you as how the files are placed/organized). See image below:

No alt text provided for this image

Step-4 (Optional): On both managed hosts, check the status of users before the execution of playbook. See image below, no such users already exist:

No alt text provided for this image

Step-5: Run the playbook using command ‘ansible-playbook -i inventory createUsers.yml’ at control node. See image below. 

No alt text provided for this image

The playbook has been executed successfully.

Step-6: Check the result on the target managed hosts. All the users (i.e. user1, user2, user3, user4, user5) have been created successfully. See image below:

No alt text provided for this image
Written by

Fakhar ul Hassan

Infrastructure Consultant, Cloud Architect & Automation, DevOps, Infrastructure as Code (IaC), Ansible, Terraform