Posts

Showing posts with the label ansible-playbook

Ansible variables, Lists, Dictionaries

Image
 There are many boring tasks in your daily job which can be automated easily if you know some of the tools like here, Ansible. Let's explore more on how to use the variables in the playbooks. In this post we will be covering : Basic datatypes List variables and using them Dictionary variable and accessing them Variables and Datatypes in Ansible In Ansible variables can be defined under global tasks or they can be defined at local to a task level. support all the Python supported datatypes. --- # Filename: varibles_datatypes.yml - name: varibles in ansible hosts: localhost gather_facts: false vars: a: "Vybhava Technologies" b: yes n: 100 m: 500.99 tasks: - debug: msg: - "a= {{ a }} a type: {{ a |type_debug }}" - "b= {{ b }} b type: {{ b |type_debug }}" - "n= {{ n }} n type: {{ n |type_debug }}" - "m= {{ m }} m type: {{ m |type_debug }}" The...

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...