0% found this document useful (0 votes)
18 views

Python+Ansible

Uploaded by

Popa Alex
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Python+Ansible

Uploaded by

Popa Alex
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 38

Module 6:

Introduction to Python

www.newpharma-development.ro
• About Python
• Keywords
Agenda • Variables
• Conditionals and Loops
• Functions
• Modules
• Resources

www.newpharma-development.ro
About Python

www.newpharma-development.ro
• Python is a popular programming language that is
reliable, flexible, easy to learn, open-source
• Is a high-level, interpreted, interactive, and object-
oriented programming language
• Supported by both a strong developer community and
many free libraries (modules)
• Python supports all manners of development, including
web applications, web services, desktop apps, scripting,
data science, scientific computing, and Jupyter
notebooks
• Can run on all operating systems
• Python scripts have .py extension

www.newpharma-development.ro
Keywords

www.newpharma-development.ro
• Python has defined 35 special reserved words (keywords)
that have specific meanings and purposes and
• Python keywords can’t be used for anything but those
specific purposes
• These keywords are always available

www.newpharma-development.ro
Variables

www.newpharma-development.ro
• Variables are containers for storing data values

• Variables can store data of different types, and different


types can do different things

• Python has different data types built-in by default

• Commonly used data types:


• Text type – string
• Numeric type – integer, float, complex
• Sequence type – list, tuples
• Boolean type – bool

www.newpharma-development.ro
• Variables are declared by specifying a variable name and
assigning a value - <variable_name> = <value>

www.newpharma-development.ro
Conditionals and Loops

www.newpharma-development.ro
• To run (or not run) a given code block depending on
whether certain conditions are met we use conditional
statements – if-else statement

www.newpharma-development.ro
• Loops are a common way of iterating multiple times and
performing some actions in each, Python provides two
types of loops:
• For loops

• While loops

www.newpharma-development.ro
Functions

www.newpharma-development.ro
• A function is a block of code which only runs when it is
called.
• You can pass data, known as parameters, into a
function.
• Python comes with built-in functions

www.newpharma-development.ro
• You define a function with the def keyword, then write
the function identifier (name) followed by parentheses
and a colon
• Make sure you indent with a tab or 4 spaces, and then
specify what you want the function to do for you
• To call this function, write the name of the function
followed by parentheses:

www.newpharma-development.ro
Modules

www.newpharma-development.ro
• Consider a module to be the same as a code library
• A file containing a set of functions you want to include in
your application
• To install a module we run the following command
python –m pip install <module_name>
• To import the module in our python script we use the
import keyword
• Objects (functions, variables) in the module are only
accessible when prefixed with <module_name> via dot
notation

www.newpharma-development.ro
www.newpharma-development.ro
Resources

www.newpharma-development.ro
• PyPI · The Python Package Index
• BeginnersGuide/Programmers - Python Wiki
• https://realpython.com/python-first-steps/
• Python Tutorials – Real Python
• Download Python | Python.org

www.newpharma-development.ro
Thank you!

www.newpharma-development.ro
Infrastructure as a code
with Ansible

www.newpharma-development.ro
Agenda Why we need Ansible ?
What is Ansible ?
Ansible Dictionary
Demo

www.newpharma-development.ro
Repetitive Jobs

• Building Virtual machine templates


• OS install and configuration
• Network setup
• User creations
• Software installation and configuration
• Software deployments
• Deploy application code
• Run database migrations
• Server maintenance
• SSH into every server and restart a service
• Write complex scripts to log into every server to update a package

www.newpharma-development.ro
Configuration Management Tools

• Puppet
• Chef
• Saltstack
• Ansible

www.newpharma-development.ro
What is Ansible ?

• Simple and easy IT automation • Continuous delivery


• Push model via OpenSSH, agentless • Require sequential success of
multiple processes
• Provisioning
• PXE booting and kickstarting bare- • Security automation
metal servers or VMs, or creating • Automate and standardize
virtual or cloud instances from threat-scanning and firewall
templates updates
• Configuration management • Orchestration
• Idempotent server state definitions • Define how multiple
configurations interact
• Application deployment
• One-command standard
deployments to update applications
across many machines

www.newpharma-development.ro
How Does Ansible Work?

www.newpharma-development.ro
How Does Ansible Work?

www.newpharma-development.ro
How to install Ansible ?

Control node
• Any UNIX-like machine
• Python 3.8 or newer
• Check that pip is available
• Execute: python3 -m pip install --user ansible

• https://docs.ansible.com/ansible/latest/installatio
n_guide/installation_distros.html#

www.newpharma-development.ro
How does Ansible Work ?

Ansible Modules
• short programs that Ansible distributes to all nodes
• can be executed via playbooks or ad-hoc
Ansible Inventory
• file to track which hosts are part of your infrastructure
Ansible Playbooks
• define how, in which order, on which machines, and what modules
should be executed
Ansible Tasks
• modules with arguments

www.newpharma-development.ro
How does Ansible Work ?

Ansible Inventory
default: /etc/ansible/hosts

www.newpharma-development.ro
How does Ansible Work ?

Ansible Tasks

www.newpharma-development.ro
How does Ansible Work ?

Ansible Plays
• Series of tasks with designated user and host

www.newpharma-development.ro
Ansible Ad-Hoc Commands ?

Ad-hoc commands allow basic commands using Ansible


inventories.

ansible [pattern] -m [module] -a "[module options]"

www.newpharma-development.ro
Ansible Roles?

In Ansible, the role is the primary mechanism for breaking a


playbook into multiple files. This simplifies writing complex
playbooks, and it makes them easier to reuse.

Roles are not playbooks. Roles are small functionality which


can be independently used but have to be used within
playbooks. There is no way to directly execute a role.

https://galaxy.ansible.com/

www.newpharma-development.ro
Resources

• https://docs.ansible.com/
• https://www.redhat.com/en/topics/automation/learning-
ansible-tutorial

www.newpharma-development.ro
Homework
• Install Ansible on the virtual machine
• Create a playbook to execute on localhost, using module shell,
the bash script from homework related to module 1
• Create a playbook to do the following:
• Create a user devops-ansible
• Create folders devops, ansible inside the user’s home: /home/devops-
ansible/
• Install packages: wget, curl, htop.
• Create a python script to take as input variables user, job and to
display: My name is ‘user’. I’m a ‘job’ .
Ex: Enter your Name: John
Enter your job: Devops
My name is John. I’m a Devops.

www.newpharma-development.ro
Thank you!

www.newpharma-development.ro

You might also like