SlideShare ist ein Scribd-Unternehmen logo
1 von 29
Downloaden Sie, um offline zu lesen
“Ansible is an IT automation tool. It can configure systems, deploy software, and
orchestrate more advanced IT tasks such as continuous deployments or zero
downtime rolling updates.”
Ansible Documentation
giovanni.albero@fazland.com
giovanni.albero@fazland.com
WHOAMI
@albero92
Giovanni Albero
Developer Jr.
giovanni.albero@fazland.com
Overview
giovanni.albero@fazland.com
CASE
I need an environment to develop a web application
SOLUTION (probably)
I’ll create a Virtual Machine (VM), when It’s ready, I’ll first install nginx, then
php-fpm, MySQL and so on.
…I start to work but shortly after… I’ve broken the VM!!!!
I have to start over, and I’ll spend a lot of time to configure a machine and I
don't know how many times I’ll have to do it again.
But what can I do to automate this process?
Example
giovanni.albero@fazland.com
giovanni.albero@fazland.com
giovanni.albero@fazland.com
There is Ansible!
what do I need?
a computer (with Unix distributions or OS X) of course!
2.6 or 2.7 installedwith
If you don’t have pip installed, install it
$ sudo easy_install pip
Ansible uses the following Python modules
$ sudo pip install paramiko PyYAML Jinja2 httplib2
$ sudo pip install ansible
And now it’s time to install Ansible
giovanni.albero@fazland.com
Now in your mind there is a question, why is windows not in
the list of OS supported?
Because you can install a Unix distribution on your pc,
with a lot of benefits for your mental health :)
giovanni.albero@fazland.com
Used by
giovanni.albero@fazland.com
Now we understand how Ansible works
Ansible communicates with remote machines over SSH
From Ansible 1.3 and later (Now 1.9) it will try to use OpenSSH when
possible, this enables ControlPersist.
There are enterprise Linux 6 OS (Red Hat Enterprise Linux and derivates such
as CentOS) where the version of OpenSSH may be too old to support
ControlPersist, but no problem Ansible will fallback into using implementation
of OpenSSH called paramiko. For OS X, Ubuntu, Fedora there isn’t this
problem.
https://developer.rackspace.com/blog/speeding-up-ssh-session-creation/
OpenSSH
giovanni.albero@fazland.com
Structure
giovanni.albero@fazland.com
Inventory
[webservers]
foo.example.com
bar.example.com
[dbservers]
one.example.com
two.example.com
three.example.com
The things in brackets are group
names, which are used in
classifying systems and deciding
what systems you are controlling
at what times and for what
purpose.
If you have hosts that run on non-standard SSH ports you can put the port number after the
hostname
badwolf.example.com:5309
hosts is an INI-like
giovanni.albero@fazland.com
Playbook
---
- hosts: all
sudo: yes
tasks:
- name: Update apt-cache
apt: update_cache=yes
- name: Install htop
apt: pkg=htop state=present
yaml file
giovanni.albero@fazland.com
Variables
---
- hosts: all
sudo: yes
vars:
package_1: curl
tasks:
- name: Update apt-cache
apt: update_cache=yes
- name: “Install {{ package_1 }}”
apt: pkg=“{{ package_1 }}” state=present
giovanni.albero@fazland.com
Loops
---
- hosts: all
sudo: yes
tasks:
- name: Update apt-cache
apt: update_cache=yes
- name: “Install {{ item }}”
apt: pkg=“{{ item }}” state=present
with_items:
- htop
- curl
- unzip
giovanni.albero@fazland.com
Conditionals
---
- hosts: all
sudo: yes
tasks:
- name: copy authorized_keys
copy:
src: authorized_keys
dest: /home/ubuntu/.ssh/authorized_keys
owner: ubuntu
when: environment == “Stage”
giovanni.albero@fazland.com
Provisioner file
site.yml
---
hosts: all
sudo: yes
tasks:
- name: install nginx
apt: name=nginx update_cache=yes
- name: copy nginx config file
copy: src=files/nginx.conf dest=/etc/nginx/sites-available/default
- name: enable configuration
file: >
dest=/etc/nginx/sites-enabled/default
src=/etc/nginx/sites-available/default
state=link
- name: copy index.html
template: src=templates/index.html.j2 dest=/usr/share/nginx/html/index.html mode=0644
- name: restart nginx
service: name=nginx state=restarted
giovanni.albero@fazland.com
DEMO 1
ansible-playbook
giovanni.albero@fazland.com
But If I had more tasks
---
- hosts: servers
vars_files:
- vars.yml
gather_facts: false
sudo: true
tasks:
- name: Create the project directory.
file: state=directory path={{ project_root }}
- name: Create user.
user: home={{ project_root }}/home/ name={{ project_name }} state=present
- name: Update the project directory.
file: group={{ project_name }} owner={{ project_name }} mode=755 state=directory path={{ project_root }}
- name: Create the code directory.
file: group={{ project_name }} owner={{ project_name }} mode=755 state=directory path={{ project_root }}/code/
- name: Install required system packages.
apt: pkg={{ item }} state=installed update-cache=yes
with_items: {{ system_packages }}
- name: Install required Python packages.
easy_install: name={{ item }}
with_items: {{ python_packages }}
- name: Mount code folder.
mount: fstype=vboxsf opts=uid={{ project_name }},gid={{ project_name }} name={{ project_root }}/code/ src={{ project_name }} state=mounted
only_if: "$vm == 1"
- name: Create the SSH directory.
file: state=directory path={{ project_root }}/home/.ssh/
only_if: "$vm == 0"
- name: Upload SSH known hosts.
copy: src=known_hosts dest={{ project_root }}/home/.ssh/known_hosts mode=0600
only_if: "$vm == 0"
- name: Upload SSH key.
copy: src=key dest={{ project_root }}/home/.ssh/id_rsa mode=0600
only_if: "$vm == 0"
- name: Create the SSL directory.
file: state=directory path={{ project_root }}/home/ssl/
- name: Upload SSL private key.
copy: src=files/ssl/{{ project_name }}.pem dest={{ project_root }}/home/ssl/{{ project_name }}.pem
- name: Upload SSH public key.
copy: src=files/ssl/{{ project_name }}.key.encrypted dest={{ project_root }}/home/ssl/{{ project_name }}.key
- name: Change permissions.
shell: chown -R {{ project_name }}:{{ project_name }} {{ project_root }}
- name: Install nginx configuration file.
copy: src=files/conf/nginx.conf dest=/etc/nginx/sites-enabled/{{ project_name }}
notify: restart nginx
- name: Install init scripts.
copy: src=files/init/{{ item }}.conf dest=/etc/init/{{ project_name }}_{{ item }}.conf
with_items: {{ initfiles }}
- name: Create database.
shell: {{ project_root }}/env/bin/python {{ project_root }}/code/webapp/manage.py sqlcreate --router=default | sudo -u postgres psql
handlers:
- include: handlers.yml
- include: deploy.yml
- hosts: servers
vars_files:
- vars.yml
gather_facts: false
sudo: true
tasks:
- name: Restart services.
service: name={{ project_name }}_{{ item }} state=restarted
with_items: {{ initfiles }}
it’s not readable and maintainable
giovanni.albero@fazland.com
Group Vars & Host Vars
You can create a separate variable file for each host and each group
group_vars/dbservers
db_primary_host: rhodeisland.example.com
db_replica_host: virginia.example.com
db_name: widget_production
db_user: widgetuser
db_password: pFmMxcyD;Fc6)6
rabbitmq_host:pennsylvania.example.com
host_vars/foo.example.com
http_port: 80
security_port: 2555
giovanni.albero@fazland.com
Roles
- project organization tool
- reusable components
roles/mongodb/tasks/main.yml
Tasks
roles/mongodb/files/
Holds files to be uploaded to hosts
roles/mongodb/templates/
Holds Jinja2 template files
roles/mongodb/vars/main.yml
Variables that shouldn’t be overridden
giovanni.albero@fazland.com
Templates
server {
charset utf-8;
listen 80 default_server;
server_name {{ host }};
root {{ wp_root }};
client_max_body_size {{ client_max_body_size }};
…
giovanni.albero@fazland.com
Templates
In playbook
- name: Upload configuration File
template: src=default.j2 dest="/etc/nginx/sites-available/default"
giovanni.albero@fazland.com
Provisioning
site.yml #master playbook
hosts #inventory
group_vars/
group1 # here we assign variables to particular groups
group2
host_vars/
hostname1 # if systems need specific variables, put them
here
hostname2
roles/
common/ # this hierarchy represents a "role"
files/ # <-- files for use with the copy resource
templates/ # <-- files for use with the template resource
tasks/ # <-- tasks file can include smaller files
vars/ # <-- variables associated with this role
webservers/
…
…
applicationservers/
…
…
databaseservers/
…
…
giovanni.albero@fazland.com
DEMO 2
ansible-playbook
giovanni.albero@fazland.com
And on web services?
(such as AWS)
giovanni.albero@fazland.com
New host file called Dynamic Inventory
https://raw.githubusercontent.com/ansible/ansible/devel/plugins/inventory/ec2.py
remember to export
AWS_ACCESS_KEY_ID
AWS_SECRET_ACCESS_KEY
giovanni.albero@fazland.com
DEMO 3
ansible-playbook
giovanni.albero@fazland.com
@albero92
Thanks!
https://github.com/giovannialbero1992/ansible

Weitere ähnliche Inhalte

Kürzlich hochgeladen

"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rick Flair
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxLoriGlavin3
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxLoriGlavin3
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embeddingZilliz
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfMounikaPolabathina
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxBkGupta21
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 

Kürzlich hochgeladen (20)

"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embedding
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdf
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptx
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 

Empfohlen

How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at WorkGetSmarter
 

Empfohlen (20)

How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 

Ansible from Introduction to Amazon AWS

  • 1. “Ansible is an IT automation tool. It can configure systems, deploy software, and orchestrate more advanced IT tasks such as continuous deployments or zero downtime rolling updates.” Ansible Documentation giovanni.albero@fazland.com
  • 4. giovanni.albero@fazland.com CASE I need an environment to develop a web application SOLUTION (probably) I’ll create a Virtual Machine (VM), when It’s ready, I’ll first install nginx, then php-fpm, MySQL and so on. …I start to work but shortly after… I’ve broken the VM!!!! I have to start over, and I’ll spend a lot of time to configure a machine and I don't know how many times I’ll have to do it again. But what can I do to automate this process? Example
  • 7. giovanni.albero@fazland.com There is Ansible! what do I need? a computer (with Unix distributions or OS X) of course! 2.6 or 2.7 installedwith If you don’t have pip installed, install it $ sudo easy_install pip Ansible uses the following Python modules $ sudo pip install paramiko PyYAML Jinja2 httplib2 $ sudo pip install ansible And now it’s time to install Ansible
  • 8. giovanni.albero@fazland.com Now in your mind there is a question, why is windows not in the list of OS supported? Because you can install a Unix distribution on your pc, with a lot of benefits for your mental health :)
  • 10. giovanni.albero@fazland.com Now we understand how Ansible works Ansible communicates with remote machines over SSH From Ansible 1.3 and later (Now 1.9) it will try to use OpenSSH when possible, this enables ControlPersist. There are enterprise Linux 6 OS (Red Hat Enterprise Linux and derivates such as CentOS) where the version of OpenSSH may be too old to support ControlPersist, but no problem Ansible will fallback into using implementation of OpenSSH called paramiko. For OS X, Ubuntu, Fedora there isn’t this problem. https://developer.rackspace.com/blog/speeding-up-ssh-session-creation/ OpenSSH
  • 12. giovanni.albero@fazland.com Inventory [webservers] foo.example.com bar.example.com [dbservers] one.example.com two.example.com three.example.com The things in brackets are group names, which are used in classifying systems and deciding what systems you are controlling at what times and for what purpose. If you have hosts that run on non-standard SSH ports you can put the port number after the hostname badwolf.example.com:5309 hosts is an INI-like
  • 13. giovanni.albero@fazland.com Playbook --- - hosts: all sudo: yes tasks: - name: Update apt-cache apt: update_cache=yes - name: Install htop apt: pkg=htop state=present yaml file
  • 14. giovanni.albero@fazland.com Variables --- - hosts: all sudo: yes vars: package_1: curl tasks: - name: Update apt-cache apt: update_cache=yes - name: “Install {{ package_1 }}” apt: pkg=“{{ package_1 }}” state=present
  • 15. giovanni.albero@fazland.com Loops --- - hosts: all sudo: yes tasks: - name: Update apt-cache apt: update_cache=yes - name: “Install {{ item }}” apt: pkg=“{{ item }}” state=present with_items: - htop - curl - unzip
  • 16. giovanni.albero@fazland.com Conditionals --- - hosts: all sudo: yes tasks: - name: copy authorized_keys copy: src: authorized_keys dest: /home/ubuntu/.ssh/authorized_keys owner: ubuntu when: environment == “Stage”
  • 17. giovanni.albero@fazland.com Provisioner file site.yml --- hosts: all sudo: yes tasks: - name: install nginx apt: name=nginx update_cache=yes - name: copy nginx config file copy: src=files/nginx.conf dest=/etc/nginx/sites-available/default - name: enable configuration file: > dest=/etc/nginx/sites-enabled/default src=/etc/nginx/sites-available/default state=link - name: copy index.html template: src=templates/index.html.j2 dest=/usr/share/nginx/html/index.html mode=0644 - name: restart nginx service: name=nginx state=restarted
  • 19. giovanni.albero@fazland.com But If I had more tasks --- - hosts: servers vars_files: - vars.yml gather_facts: false sudo: true tasks: - name: Create the project directory. file: state=directory path={{ project_root }} - name: Create user. user: home={{ project_root }}/home/ name={{ project_name }} state=present - name: Update the project directory. file: group={{ project_name }} owner={{ project_name }} mode=755 state=directory path={{ project_root }} - name: Create the code directory. file: group={{ project_name }} owner={{ project_name }} mode=755 state=directory path={{ project_root }}/code/ - name: Install required system packages. apt: pkg={{ item }} state=installed update-cache=yes with_items: {{ system_packages }} - name: Install required Python packages. easy_install: name={{ item }} with_items: {{ python_packages }} - name: Mount code folder. mount: fstype=vboxsf opts=uid={{ project_name }},gid={{ project_name }} name={{ project_root }}/code/ src={{ project_name }} state=mounted only_if: "$vm == 1" - name: Create the SSH directory. file: state=directory path={{ project_root }}/home/.ssh/ only_if: "$vm == 0" - name: Upload SSH known hosts. copy: src=known_hosts dest={{ project_root }}/home/.ssh/known_hosts mode=0600 only_if: "$vm == 0" - name: Upload SSH key. copy: src=key dest={{ project_root }}/home/.ssh/id_rsa mode=0600 only_if: "$vm == 0" - name: Create the SSL directory. file: state=directory path={{ project_root }}/home/ssl/ - name: Upload SSL private key. copy: src=files/ssl/{{ project_name }}.pem dest={{ project_root }}/home/ssl/{{ project_name }}.pem - name: Upload SSH public key. copy: src=files/ssl/{{ project_name }}.key.encrypted dest={{ project_root }}/home/ssl/{{ project_name }}.key - name: Change permissions. shell: chown -R {{ project_name }}:{{ project_name }} {{ project_root }} - name: Install nginx configuration file. copy: src=files/conf/nginx.conf dest=/etc/nginx/sites-enabled/{{ project_name }} notify: restart nginx - name: Install init scripts. copy: src=files/init/{{ item }}.conf dest=/etc/init/{{ project_name }}_{{ item }}.conf with_items: {{ initfiles }} - name: Create database. shell: {{ project_root }}/env/bin/python {{ project_root }}/code/webapp/manage.py sqlcreate --router=default | sudo -u postgres psql handlers: - include: handlers.yml - include: deploy.yml - hosts: servers vars_files: - vars.yml gather_facts: false sudo: true tasks: - name: Restart services. service: name={{ project_name }}_{{ item }} state=restarted with_items: {{ initfiles }} it’s not readable and maintainable
  • 20. giovanni.albero@fazland.com Group Vars & Host Vars You can create a separate variable file for each host and each group group_vars/dbservers db_primary_host: rhodeisland.example.com db_replica_host: virginia.example.com db_name: widget_production db_user: widgetuser db_password: pFmMxcyD;Fc6)6 rabbitmq_host:pennsylvania.example.com host_vars/foo.example.com http_port: 80 security_port: 2555
  • 21. giovanni.albero@fazland.com Roles - project organization tool - reusable components roles/mongodb/tasks/main.yml Tasks roles/mongodb/files/ Holds files to be uploaded to hosts roles/mongodb/templates/ Holds Jinja2 template files roles/mongodb/vars/main.yml Variables that shouldn’t be overridden
  • 22. giovanni.albero@fazland.com Templates server { charset utf-8; listen 80 default_server; server_name {{ host }}; root {{ wp_root }}; client_max_body_size {{ client_max_body_size }}; …
  • 23. giovanni.albero@fazland.com Templates In playbook - name: Upload configuration File template: src=default.j2 dest="/etc/nginx/sites-available/default"
  • 24. giovanni.albero@fazland.com Provisioning site.yml #master playbook hosts #inventory group_vars/ group1 # here we assign variables to particular groups group2 host_vars/ hostname1 # if systems need specific variables, put them here hostname2 roles/ common/ # this hierarchy represents a "role" files/ # <-- files for use with the copy resource templates/ # <-- files for use with the template resource tasks/ # <-- tasks file can include smaller files vars/ # <-- variables associated with this role webservers/ … … applicationservers/ … … databaseservers/ … …
  • 26. giovanni.albero@fazland.com And on web services? (such as AWS)
  • 27. giovanni.albero@fazland.com New host file called Dynamic Inventory https://raw.githubusercontent.com/ansible/ansible/devel/plugins/inventory/ec2.py remember to export AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY