SlideShare a Scribd company logo
1 of 29
Download to read offline
“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

More Related Content

Recently uploaded

Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 

Recently uploaded (20)

Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 

Featured

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
 

Featured (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