KEMBAR78
Converting Bash Scripts To Playbooks: Automate All The Things | PDF | Software Development | Computer Programming
0% found this document useful (0 votes)
271 views17 pages

Converting Bash Scripts To Playbooks: Automate All The Things

This document discusses converting Bash scripts to Ansible playbooks for automation. Some key points discussed include examining the script flow and functions to determine how to structure the playbook, converting conditionals and system commands to Ansible tasks, and verifying functionality after each conversion. Converting scripts allows taking advantage of Ansible's idempotent features but may require reworking the script flow and splitting functions across multiple files.

Uploaded by

protos
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
271 views17 pages

Converting Bash Scripts To Playbooks: Automate All The Things

This document discusses converting Bash scripts to Ansible playbooks for automation. Some key points discussed include examining the script flow and functions to determine how to structure the playbook, converting conditionals and system commands to Ansible tasks, and verifying functionality after each conversion. Converting scripts allows taking advantage of Ansible's idempotent features but may require reworking the script flow and splitting functions across multiple files.

Uploaded by

protos
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Converting Bash

scripts to Playbooks
Automate all the things

David Glaser
Senior Technical Account Manager

1
The Automation Journey often involves
converting scripts to Ansible Playbooks.

This is a perfect time to reexamine how these


scripts work and utilize Ansible idempotent
features.

2
Why Convert?
The Shortcut Trap

Ansible includes shell, command, and script modules. These allow for
direct running of scripts on remote hosts. This presents problems:

▸ No idempotency checking

▸ Output is simply the script output

▸ Does not support check_mode

3
Conversion Misnomers
What they don’t tell you

▸ Converting will likely not be 1 for 1

▸ There may not be an ansible module for everything

▸ The ansible playbook will likely be longer than the bash script, but
easier to read

▸ The flow of the script may have to be reworked

4
Bash vs Ansible
Features

Bash is a great scripting language, but all Ansible handles redundancy, idempotency, and
redundancy, idempotency, and error checking error checking when using supported modules
must be done manually

▸ Multiple functions in each task(line) ▸ One function per task*

▸ Non-linear flow (functions) through script ▸ Linear flow through playbook

▸ Learning curve for syntax and formatting ▸ Learning curve for formatting only**

* Some tasks perform multiple functions, but these are limited to permission changes to the working file, etc.
** Ok, there’s some syntax learning, but it’s a on module by module basis
Converting Concepts

We’ll focus on a method to follow to convert bash


scripts to Ansible Playbooks

▸ Temporary script file

▸ Examine script flow

▸ Converting conditionals

▸ Examine system commands and arguments

▸ Verify functionality

6
Temporary script file

Much of the planning for conversion means moving code around. Using a
temporary file will assure that any changes can be tested or backed out.

7
Examine Script Flow

Bash has functions, but Ansible does not. It’s possible to include groups
of tasks using include and import modules however

▸ Look over the Script Flow, are functions used?

・ If so, are they used multiple times?

Used Multiple Times


Used Once
▸ These lines(tasks) will be in their own file and
Place the function in line in the script where it
included or imported into the main playbook
is called
▸ Any variables that are ‘sent’ to the function
need to be registered in the playbook.
8
Examine Script Flow

9
Writing the playbook

Rewriting the script into a linear flow will make converting to a playbook
easier

▸ Once the flow is linear, start working on playbook(s)

▸ Define variables that are needed at the play level as much as possible

▸ Work on one task (or bash statement) at a time, converting it to an


Ansible task

▸ When writing blocks with conditions, make it falsifiable so you can


control when the block is run to test with

10
Convert conditionals

Bash if, for, and while statements are defined at the beginning of the
conditional. In ansible they are at the end. Use block: to group
conditionals together

▸ Variables are not quoted in conditionals

▸ Conditionals can be joined using and, or and ().

▸ Blocks can be nested which each level having its own conditionals

▸ Ansible does not have an if-else construct, so use two conditionals,


one for each test

11
Convert conditionals

12
Examine System Commands and Arguments

▸ Identify Ansible modules that will accomplish each task

・ Note which modules are needed

・ Note which arguments are required for each

▸ If a module doesn’t exist in Ansible, is one available online (Galaxy,


Automation Hub), or possible to create?

・ If yes, include the containing collection in the playbook


directory or other location that is available

・ If no, default to shell or command to run the command

13
Examine System Commands and Arguments

COMMAND ARGUMENTS

14
Verify Functionality

Test the Ansible Playbook after every task (or set of tasks) is added.

▸ This cuts down on testing at the end of the playbook


▸ Assists in verifying system is in proper order for next task

15
Q&A

16
Thank you
Red Hat is the world’s leading provider of enterprise
open source software solutions. Award-winning support,
training, and consulting services make Red Hat a trusted
adviser to the Fortune 500.

linkedin.com/company/red-hat facebook.com/redhatinc

youtube.com/user/RedHatVideos twitter.com/RedHat

17

You might also like