Ansible

Ansible some common usecases

In this blog, we will explore some common usecases which will help you in the automation.

If you want to see the video for this article, click here

Agenda:

  1. Find applications which are installed on a system
  2. Configure bashprofile — add a new line to set java home
  3. Run playbook if variable is defined
  4. Run playbook if folder is exist

Scenario 1: 

  • You want to check list of packages installed on your system.
  • no_log: We have set this variable as true. If you save Ansible output to a log, you expose any secret data in your Ansible output, such as passwords and user names. To keep sensitive values out of your logs, mark tasks that expose them with the no_log: True attribute.Check this link for more details on ansible logging
  • Install the package if not installed else skip the playbook
  • Parsing the json output to find a particular application and using set_fact for setting the output in a new variable
  • fail: This module fails the progress with a custom message using when condition
  • Set a variable if application is not installed
- name: List of all installed packages
  yum:
    list: installed
  register: installed_packages

- set_fact:
test_installed_packages: "{{ installed_packages.results | json_query(query) }}"
vars:
query: "[?name=='vim-minimal']"

- debug:
msg: "{{test_installed_packages}}"

- fail:
msg: Package installed already
when: test_installed_packages| length > 0

- set_fact:
application_installed: False
when: not test_installed_packages

- debug:
msg: "application not installed"
when: not application_installed

Ansible Output if application is found

"msg": [
{
"arch": "x86_64",
"envra": "2:vim-minimal-7.4.160-4.el7.x86_64",
"epoch": "2",
"name": "vim-minimal",
"release": "4.el7",
"repo": "installed",
"version": "7.4.160",
"yumstate": "installed"
}

Scenario 2:

  • Configure bash profile for java_home path
  • lineinfile: This module ensures a particular line is in a file, or replace an existing line using a back-referenced regular expression.
  • This is primarily useful when you want to change a single line in a file only.
- name: Configure bash_profile
lineinfile:
dest: ~/.bash_profile
regexp: '^JAVA_HOME='
line: "JAVA_HOME=/usr/lib/jvm"
become_user: root
become: true
ignore_errors: true

- name: Configure bashrc
lineinfile:
regexp: '^JAVA_HOME='
line: "JAVA_HOME=/usr/lib/jvm"
dest: ~/.bashrc
become_user: root
become: true
ignore_errors: true

Check out this link for more details on lineinfile module in ansible.

Scenario 3:

  • You want to run a playbook only when some variable is defined
  • Use it as if/else condition based on the variable defined
- include: main.yml 
when: VAR_MAIN is defined
- include: test.yml 
when: VAR_TEST is defined
  • Below is the command to run the playbook
ansible-playbook main.yml  -e "VAR_TEST=abc"

Scenario 4:

  • Run the task only when some folder is exist
- name: Check folder exist or not.
stat:
path: "{{ folder_path }}"
register: folder_path_exist

- debug:
msg: "Directory is present"
when: folder_path_exist.stat.exists

- name: Create {{location }} directory
file:
path: "{{location}}"
state: directory
mode: 0777
become: true
when: folder_path_exist.stat.exists ==false

Congratulations, you have successfully learnt some common usecases which will help you in the automation. To know more about the Ansible common usecases visit here.

Please follow and like us: