ansible basics - adfinis sygroup · 2020-04-28 · $ ansible test -i inventory.txt -u root -m...

38
Ansible Basics

Upload: others

Post on 11-Jun-2020

9 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Ansible Basics - Adfinis SyGroup · 2020-04-28 · $ ansible test -i inventory.txt -u root -m command -a "ls -l /" Hands-on :: Basics 02 Create some tasks and the first playbook

AnsibleBasics

Page 2: Ansible Basics - Adfinis SyGroup · 2020-04-28 · $ ansible test -i inventory.txt -u root -m command -a "ls -l /" Hands-on :: Basics 02 Create some tasks and the first playbook

Besmart.Thinkopensource.

Page 3: Ansible Basics - Adfinis SyGroup · 2020-04-28 · $ ansible test -i inventory.txt -u root -m command -a "ls -l /" Hands-on :: Basics 02 Create some tasks and the first playbook

AnsibleHands-onLearningbydoing

Page 4: Ansible Basics - Adfinis SyGroup · 2020-04-28 · $ ansible test -i inventory.txt -u root -m command -a "ls -l /" Hands-on :: Basics 02 Create some tasks and the first playbook

Hands-on::Basics01InstallAnsibleandtakethefirststeps

Page 5: Ansible Basics - Adfinis SyGroup · 2020-04-28 · $ ansible test -i inventory.txt -u root -m command -a "ls -l /" Hands-on :: Basics 02 Create some tasks and the first playbook

Basics01-InstallationInstallAnsibleonyourmachine:

RHEL&CentOS(requiresEPEL)

$sudoyuminstallansible

Debian&Ubuntu

$sudoapt-getinstallansible

Page 6: Ansible Basics - Adfinis SyGroup · 2020-04-28 · $ ansible test -i inventory.txt -u root -m command -a "ls -l /" Hands-on :: Basics 02 Create some tasks and the first playbook

Basics01-InstallationCheckifyouhavethelatestAnsibleversion:

$ansible--version$manansible

Page 7: Ansible Basics - Adfinis SyGroup · 2020-04-28 · $ ansible test -i inventory.txt -u root -m command -a "ls -l /" Hands-on :: Basics 02 Create some tasks and the first playbook

Basics01-InstallationAddyourSSHpublickeytotheauthorized_keysfileonthetargetnode:

[email protected]

Page 8: Ansible Basics - Adfinis SyGroup · 2020-04-28 · $ ansible test -i inventory.txt -u root -m command -a "ls -l /" Hands-on :: Basics 02 Create some tasks and the first playbook

Basics01-InstallationCreateaworkingdirectoryforthisworkshop:

$mkdir~/ansible_workshop$cd~/ansible_workshop

Page 9: Ansible Basics - Adfinis SyGroup · 2020-04-28 · $ ansible test -i inventory.txt -u root -m command -a "ls -l /" Hands-on :: Basics 02 Create some tasks and the first playbook

Basics01-InventoryCreatethefileinventory.txtcontainingyourtestnode:

[test]192.168.122.10

Page 10: Ansible Basics - Adfinis SyGroup · 2020-04-28 · $ ansible test -i inventory.txt -u root -m command -a "ls -l /" Hands-on :: Basics 02 Create some tasks and the first playbook

Basics01-Ad-hoccommandsExecuteyourfirstad-hoccommands:

$ansibletest-iinventory.txt-uroot-mping$ansibletest-iinventory.txt-uroot-mcommand-a"df-h"$ansibletest-iinventory.txt-uroot-mcommand-a"ls-l/"

Page 11: Ansible Basics - Adfinis SyGroup · 2020-04-28 · $ ansible test -i inventory.txt -u root -m command -a "ls -l /" Hands-on :: Basics 02 Create some tasks and the first playbook

Hands-on::Basics02Createsometasksandthefirstplaybook

Page 12: Ansible Basics - Adfinis SyGroup · 2020-04-28 · $ ansible test -i inventory.txt -u root -m command -a "ls -l /" Hands-on :: Basics 02 Create some tasks and the first playbook

Basics02-FactsExplorethefactsofyourtestnode:

$ansibletest-iinventory.txt-uroot-msetup

Page 13: Ansible Basics - Adfinis SyGroup · 2020-04-28 · $ ansible test -i inventory.txt -u root -m command -a "ls -l /" Hands-on :: Basics 02 Create some tasks and the first playbook

Basics02-PlaybooksCreatethefilewebserver.ymlwiththefollowingcontent:

----hosts:testtasks:

-name:installnginxpackage:name:nginxstate:present

-name:startnginxserviceservice:name:nginxstate:started

Page 14: Ansible Basics - Adfinis SyGroup · 2020-04-28 · $ ansible test -i inventory.txt -u root -m command -a "ls -l /" Hands-on :: Basics 02 Create some tasks and the first playbook

Basics02-PlaybooksRuntheplaybookagainstyourtestnode:

$ansible-playbookwebserver.yml-iinventory.txt-uroot

Wasitsuccessful?Checkifthewebserverisrunninginyourbrowser!

Page 15: Ansible Basics - Adfinis SyGroup · 2020-04-28 · $ ansible test -i inventory.txt -u root -m command -a "ls -l /" Hands-on :: Basics 02 Create some tasks and the first playbook

Basics02-PlaybooksGetdebuggingoutputbyaddingtheverboseflag(s):

$ansible-playbookwebserver.yml-iinventory.txt-uroot-v

Addmore-vparameterstogetevenmoreoutput

Page 16: Ansible Basics - Adfinis SyGroup · 2020-04-28 · $ ansible test -i inventory.txt -u root -m command -a "ls -l /" Hands-on :: Basics 02 Create some tasks and the first playbook

Basics02-RolesCreateanewrolecalled"nginx":

$mkdir-proles/nginx/tasks

Addtheprevioustaskstothetasksfileroles/nginx/tasks/main.yml:

----name:installnginxpackage:name:nginxstate:present

-name:startnginxserviceservice:name:nginxstate:started

Page 17: Ansible Basics - Adfinis SyGroup · 2020-04-28 · $ ansible test -i inventory.txt -u root -m command -a "ls -l /" Hands-on :: Basics 02 Create some tasks and the first playbook

Basics02-RolesIncludethenewnginxroleinthewebserver.ymlplaybook:

----hosts:testroles:-nginx

Executetheplaybookagain,whathappens?

Page 18: Ansible Basics - Adfinis SyGroup · 2020-04-28 · $ ansible test -i inventory.txt -u root -m command -a "ls -l /" Hands-on :: Basics 02 Create some tasks and the first playbook

Hands-on::Basics03Makeyourplaybookmoredynamicwithvariables

Page 19: Ansible Basics - Adfinis SyGroup · 2020-04-28 · $ ansible test -i inventory.txt -u root -m command -a "ls -l /" Hands-on :: Basics 02 Create some tasks and the first playbook

Basics03-VariablesCreateahost_varsandgroup_varsdirectoryinyourworkingdir:

$mkdirhost_varsgroup_vars

Yourdirectorynowshouldlooklikethis:

ansible_workshop|--group_vars|--host_vars|--inventory.txt|--roles|--webserver.yml

Page 20: Ansible Basics - Adfinis SyGroup · 2020-04-28 · $ ansible test -i inventory.txt -u root -m command -a "ls -l /" Hands-on :: Basics 02 Create some tasks and the first playbook

Basics03-CommonroleCreateanadditionalrolecalled"common"includingthefollowingdirectories:

common|--defaults|--tasks|--vars

Page 21: Ansible Basics - Adfinis SyGroup · 2020-04-28 · $ ansible test -i inventory.txt -u root -m command -a "ls -l /" Hands-on :: Basics 02 Create some tasks and the first playbook

Basics03-CommonroleAddthedefaultsvarslistedbelow:

---common_packages:-ntp-iptables

Page 22: Ansible Basics - Adfinis SyGroup · 2020-04-28 · $ ansible test -i inventory.txt -u root -m command -a "ls -l /" Hands-on :: Basics 02 Create some tasks and the first playbook

Basics03-CommonroleThenewroleshouldtakecareofinstallingseveralpackages:

----name:installcommonpackagespackage:name:"{{item}}"state:presentwith_items:"{{common_packages}}"

Page 23: Ansible Basics - Adfinis SyGroup · 2020-04-28 · $ ansible test -i inventory.txt -u root -m command -a "ls -l /" Hands-on :: Basics 02 Create some tasks and the first playbook

Basics03-TestingIncludethenewroleintoyourplaybookandgiveitaspin:

Whathappens?

Whatpackageswereinstalled?

Page 24: Ansible Basics - Adfinis SyGroup · 2020-04-28 · $ ansible test -i inventory.txt -u root -m command -a "ls -l /" Hands-on :: Basics 02 Create some tasks and the first playbook

Basics03-group_varsCreatethefilegroup_vars/testwiththefollowingvariables:

---common_packages:-ntp-iptables-vim-zsh

Page 25: Ansible Basics - Adfinis SyGroup · 2020-04-28 · $ ansible test -i inventory.txt -u root -m command -a "ls -l /" Hands-on :: Basics 02 Create some tasks and the first playbook

Basics03-TestingExecutetheplaybookasecondtime:

Arethereanychanges?

Ifyes,why?

Page 26: Ansible Basics - Adfinis SyGroup · 2020-04-28 · $ ansible test -i inventory.txt -u root -m command -a "ls -l /" Hands-on :: Basics 02 Create some tasks and the first playbook

Basics03-RolevarsCreatethefileroles/common/vars/main.ymlwiththefollowingvariables:

---common_packages:-ntp-iptables-vim-zsh-tcpdump-wget-curl-rsync

Page 27: Ansible Basics - Adfinis SyGroup · 2020-04-28 · $ ansible test -i inventory.txt -u root -m command -a "ls -l /" Hands-on :: Basics 02 Create some tasks and the first playbook

Basics03-TestingExecutetheplaybookathirdtime:

Wait,whathappenednow?

Pleaseexplaintome!

Page 28: Ansible Basics - Adfinis SyGroup · 2020-04-28 · $ ansible test -i inventory.txt -u root -m command -a "ls -l /" Hands-on :: Basics 02 Create some tasks and the first playbook

Hands-on::Basics04Generatefilesdynamicallywithtemplates

Page 29: Ansible Basics - Adfinis SyGroup · 2020-04-28 · $ ansible test -i inventory.txt -u root -m command -a "ls -l /" Hands-on :: Basics 02 Create some tasks and the first playbook

Basics04-PreparationAddthemissingdirectoriesinournginxrole:

defaultshandlerstemplatesvars

Page 30: Ansible Basics - Adfinis SyGroup · 2020-04-28 · $ ansible test -i inventory.txt -u root -m command -a "ls -l /" Hands-on :: Basics 02 Create some tasks and the first playbook

Basics04-DefaultsAddthefollowingvariabletothedefaultsvars:

---nginx_welcome_messages:-"Ansibleiscool!"-"Itevengetsbetter!"-"Myfirstloop!"

Page 31: Ansible Basics - Adfinis SyGroup · 2020-04-28 · $ ansible test -i inventory.txt -u root -m command -a "ls -l /" Hands-on :: Basics 02 Create some tasks and the first playbook

Basics04-TemplateCreatethenewtemplatecalled index.html.j2 :

{%formessageinnginx_welcome_messages%}<p>{{message}}</p>{%endfor%}

Page 32: Ansible Basics - Adfinis SyGroup · 2020-04-28 · $ ansible test -i inventory.txt -u root -m command -a "ls -l /" Hands-on :: Basics 02 Create some tasks and the first playbook

Basics04-TasksWhichmoduledoweneedtorenderthetemplateandcopyitto/usr/share/nginx/www?

Addanewtaskwhichrendersthetemplate

Createabackupoftheoldindex.htmlfile

Page 33: Ansible Basics - Adfinis SyGroup · 2020-04-28 · $ ansible test -i inventory.txt -u root -m command -a "ls -l /" Hands-on :: Basics 02 Create some tasks and the first playbook

Basics04-TestingExecuteyourmodifiedplaybooktodeploythenewwebsite:

Diditwork?

Whichmessagesaredisplayed?

Page 34: Ansible Basics - Adfinis SyGroup · 2020-04-28 · $ ansible test -i inventory.txt -u root -m command -a "ls -l /" Hands-on :: Basics 02 Create some tasks and the first playbook

Basics04-HandlersImaginethisisamorecomplexwebapplication:

Restartthenginxserviceiftheindex.htmlfilechanged

Page 35: Ansible Basics - Adfinis SyGroup · 2020-04-28 · $ ansible test -i inventory.txt -u root -m command -a "ls -l /" Hands-on :: Basics 02 Create some tasks and the first playbook

Basics04-TestingDeploythewebsiteagain:

Diditwork?

Wasthenginxservicerestarted?

Page 36: Ansible Basics - Adfinis SyGroup · 2020-04-28 · $ ansible test -i inventory.txt -u root -m command -a "ls -l /" Hands-on :: Basics 02 Create some tasks and the first playbook

Goodwork!You'vecompletedthispartoftheworkshop!

Page 38: Ansible Basics - Adfinis SyGroup · 2020-04-28 · $ ansible test -i inventory.txt -u root -m command -a "ls -l /" Hands-on :: Basics 02 Create some tasks and the first playbook