Posts

Showing posts with the label ansible-playbook

Ansible Variables Tutorial: Data Types, Lists, Dictionaries, and Practical Examples

Image
Hello DevOps enthusiasts! Many repetitive and time-consuming tasks in our daily work can be automated effectively using tools like Ansible . However, to build powerful and reusable automation, one of the most important concepts you need to master is variables . Variables allow you to write flexible playbooks that can adapt to different environments, applications, and operating systems without hardcoding values. In this article, we'll explore how variables work in Ansible and learn how to use different data types, lists, and dictionaries through practical examples. Ansible Variables Overview What We'll Learn By the end of this article, you will be able to: Understand variable fundamentals in Ansible Work with common Ansible data types Create and access list variables Manipulate list elements Create and use dictionary variables Access dictionary keys and values Debug variable types using type_debug Build more reusable and maintainable playbooks Why are Variables important  Imagi...

Ansible the lineinfile, blockinfile and replace modules

Image
 Hello !! This post is for exploring the "lineinfile" vs 'blockinfile' and "replace" modules. The replace and the lineinfile use the path parameter to mark: The file to modify. lineinfile module is used to ensure[s] a particular line is in a file, or [to] replace an existing line using a back-referenced (backref) regular expression (regex). Use the replace module if you want to change multiple, similar lines The "dest" parameter is used for modules creating new files like template or copy modules. Just replace dest with path and both provided examples should work as expected. Adding lines in the file Adding a line in a file, if a file does not exist then it will create it. --- # Filename: adding_line.yml - name: testing LineInFile module hosts: localhost tasks: - name: Add a line to a file if the file does not exist lineinfile: path: /tmp/hosts line: 192.168.1.99 ansiblectl.devopshunter.com ctl cre...

Ansible 5: Commands and their examples

Image
Hello Automation specialists, This post is for trying all the Ansible Command-line tools,  it's like a cheat sheet for ansible CLI with executed examples to better understand their usage. Ansible command-line utilities can be executed only on the box where the Ansible engine is installed and running. ansible  ansible command is used for define and run a single task 'playbook' against a set of hosts. ansible command is an extra-simple tool/framework/API for doing 'remote operations'.   Case 1: ansible -i inventory.yml all --list-hosts # all ansible -i inventory.yml dbserver --list-hosts #Specific group ansible -i inventory.yml common --list-hosts # same as all if inventory is yml Listing host using ansible command Case 2: # inventory set in the ansible.cfg ansible all --list-hosts # a group specific ansible web --list-hosts # checking for a specific host in a group ansible web --list-hosts -l 192.168.33.22...

Ansible 9 Custom Roles - Reusability

Image
Hello Guys, welcome back to DevSecOps Automations!! In this post, we will be exploring the Custom role create and usage in a playbook, which is a most industry requirement. It depends on your use cases. It's always recommended to write a role if you have a complex set of tasks consist of handlers and jinja templates. Roles break down a complex playbook into simple and multiple reusable plays easy to read! In the last post, we have learned about how ansible-galaxy helps us to install, create, modify the ready-made roles which are provided by the Ansible community freely on the Galaxy site. What are the roles why we should use them in Ansible? As per my understanding so far following points : A role can be defined when we have to do one or many tasks It is a set of tasks with a single objective (for example reboot of box - stop all process, reboot, start process) You can organize the code in more readable form using roles it is like functions in Python or C  Here the main objective i...