Talk at CfgMgmtCamp on February 3 about why you should use Ansible and how to use it
.......AnsibleWhy and how I use it!Ton KerstenAT ComputingGent, Belgium....
View Slide
. .Introduction. . . . . .Why. . . . . . . . . . . . . . .How.Recap. .Resources.Questions?Agenda...1 Introduction...2 Why...3 How...4 Recap...5 Resources...6 Questions?tk-atc-ans-v1.1Ton Kersten © 2014 – AT Computing 2 / 28
. .Introduction. . . . . .Why. . . . . . . . . . . . . . .How.Recap. .Resources.Questions?$ who am iUNIX/Linux consultant and Trainer @ ATComputingUNIX Nerd (started in 1986 with SunOS 3)Linux Geek (started in 1992 with 0.96α)Scripting nerdFree and Open Source Software enthusiastProgrammingPlain text aficionadoBig fan of things that just work· · ·tk-atc-ans-v1.1Ton Kersten © 2014 – AT Computing 3 / 28
. .Introduction. . . . . .Why. . . . . . . . . . . . . . .How.Recap. .Resources.Questions?Long agoShell scriptsSSH loopsParallel SSHCluster SSHScreen synchronized windowstmux synchronized panes· · ·Things got out of controltk-atc-ans-v1.1Ton Kersten © 2014 – AT Computing 4 / 28
. .Introduction. . . . . .Why. . . . . . . . . . . . . . .How.Recap. .Resources.Questions?NextCF EnginePuppetChefSalt StackJujuCapistranoFabric· · ·tk-atc-ans-v1.1Ton Kersten © 2014 – AT Computing 5 / 28
. .Introduction. . . . . .Why. . . . . . . . . . . . . . .How.Recap. .Resources.Questions?What I want.Simple command........root@dns1 # ansible -playbook playbooks/vtun/main.ymlPLAY [tunservers] ***************************************TASK: [install package vtun] ****************************TASK: [deploy vtun config] ******************************TASK: [ensure vtund is running] *************************NOTIFIED: [restart vtund] *******************************PLAY RECAP **********************************************tun1 : ok=1 changed =4 unreachable =0 failed =0tk-atc-ans-v1.1Ton Kersten © 2014 – AT Computing 6 / 28
. .Introduction. . . . . .Why. . . . . . . . . . . . . . .How.Recap. .Resources.Questions?Why AnsibleNo master serverNo more daemonsNo more agentsNo databasesNo separate PKIUses standard SSH functionalityVery, very powerfulConfiguration, deployment, ad-hoc, continuous deliverySimple configuration filesIdempotenttk-atc-ans-v1.1Ton Kersten © 2014 – AT Computing 7 / 28
. .Introduction. . . . . .Why. . . . . . . . . . . . . . .How.Recap. .Resources.Questions?EasyFrom nothing to production in a jiffyPython 2.6 + Paramiko, PyYAML, Jinja2 on masterPython 2.4 + simplejson on nodesCan run in Python virtualenvCan run from git checkoutUses SSH for transport and loginNo root needed, can use sudotk-atc-ans-v1.1Ton Kersten © 2014 – AT Computing 8 / 28
. .Introduction. . . . . .Why. . . . . . . . . . . . . . .How.Recap. .Resources.Questions?Simple componentsCommandsansibleansible-playbookansible-pullansible-docansible-galaxyModules (172 at this moment)Files / templatingUsersPackages (yum, apt, zypper, . . . )Services· · · (See: ansible-doc)Or, write your owntk-atc-ans-v1.1Ton Kersten © 2014 – AT Computing 9 / 28
. .Introduction. . . . . .Why. . . . . . . . . . . . . . .How.Recap. .Resources.Questions?Easy installOn all operating systemsCreate a Python virtualenv# pip install ansibleOn CentOS / RHEL / Scientific LinuxEnable the EPEL repository# yum install ansibleFrom github (Bleeding edge)Install and configure git$ git clone http :// github.com/ansible/ansible.git$ cd ansible$ sudo make installtk-atc-ans-v1.1Ton Kersten © 2014 – AT Computing 10 / 28
. .Introduction. . . . . .Why. . . . . . . . . . . . . . .How.Recap. .Resources.Questions?How it worksModule(s)ManagementnodeNodeNodeNodePlaybooksorrolesHostsno daemonscommunicationover SSHtk-atc-ans-v1.1Ton Kersten © 2014 – AT Computing 11 / 28
. .Introduction. . . . . .Why. . . . . . . . . . . . . . .How.Recap. .Resources.Questions?My example networkManagementnodeand DNSTunnel serverWeb serverWeb serverdns1.example.net172.16.12.1/24web1.example.net172.16.12.2/24web2.example.net172.16.12.3/24tun1.example.net172.16.12.4/24tk-atc-ans-v1.1Ton Kersten © 2014 – AT Computing 12 / 28
. .Introduction. . . . . .Why. . . . . . . . . . . . . . .How.Recap. .Resources.Questions?Inventory file.# cat /etc/ansible/hosts........dns1web1web2tun1[dnsservers]dns1[webservers]web1web2[tunservers]tun1tk-atc-ans-v1.1Ton Kersten © 2014 – AT Computing 13 / 28
. .Introduction. . . . . .Why. . . . . . . . . . . . . . .How.Recap. .Resources.Questions?Site playbook.# cat /etc/ansible/site.yml........- hosts: alluser: ansiblesudo: truesudo_user: rootroles:- common- sudo- include: playbooks/vtun/main.ymltk-atc-ans-v1.1Ton Kersten © 2014 – AT Computing 14 / 28
. .Introduction. . . . . .Why. . . . . . . . . . . . . . .How.Recap. .Resources.Questions?Running AnsibleGeneral ansible command form:ansible -m -a .# ansible all -m ping -o........web2 | success >> {"changed": false , "ping": "pong"}tun1 | success >> {"changed": false , "ping": "pong"}web1 | success >> {"changed": false , "ping": "pong"}dns1 | success >> {"changed": false , "ping": "pong"}tk-atc-ans-v1.1Ton Kersten © 2014 – AT Computing 15 / 28
. .Introduction. . . . . .Why. . . . . . . . . . . . . . .How.Recap. .Resources.Questions?Running a single commandThe command module is default.# ansible webservers -a ’ls -l /etc/passwd’........web2 | success | rc=0 >>-rw -r--r-- 1 root root 2302 Nov 25 13:20 /etc/passwdweb1 | success | rc=0 >>-rw -r--r-- 1 root root 1906 Oct 26 19:31 /etc/passwdtk-atc-ans-v1.1Ton Kersten © 2014 – AT Computing 16 / 28
. .Introduction. . . . . .Why. . . . . . . . . . . . . . .How.Recap. .Resources.Questions?Installing a package.# ansible tunservers -m yum -a name=vtun........tun1 | success >> {"changed": false ,"msg": "","rc": 0,"results": ["vtun -3.0.2 -1. el6.rf.x86_64 providingvtun is already installed"]}tk-atc-ans-v1.1Ton Kersten © 2014 – AT Computing 17 / 28
. .Introduction. . . . . .Why. . . . . . . . . . . . . . .How.Recap. .Resources.Questions?PlaybooksWritten in YAMLRecipes of desired state, for which hostsCan use variablesCan contain handlersWhen a state changes, take configured actionCan be re-usedtk-atc-ans-v1.1Ton Kersten © 2014 – AT Computing 18 / 28
. .Introduction. . . . . .Why. . . . . . . . . . . . . . .How.Recap. .Resources.Questions?Simple playbook.# cat /etc/ansible/playbooks/vtun/main.yml........- hosts: tunserverstasks:- name: install package vtunyum: pkg=vtun state=present- name: deploy vtun configtemplate: src=vtund.conf.j2dest =/ etc/vtund.confowner=root group=root mode =0400notify:- restart vtund- name: ensure vtund is runningservice: name=vtund state=started enabled=yeshandlers:- name: restart vtundservice: name=vtund state=restartedtk-atc-ans-v1.1Ton Kersten © 2014 – AT Computing 19 / 28
. .Introduction. . . . . .Why. . . . . . . . . . . . . . .How.Recap. .Resources.Questions?Playbook run.# ansible-playbook playbooks/vtun/main.yml........PLAY [tunservers] ***************************************TASK: [install package vtun] ****************************ok: [tun1]TASK: [deploy vtun config] ******************************ok: [tun1]TASK: [ensure vtund is running] *************************ok: [tun1]NOTIFIED: [restart vtund] *******************************changed: [tun1]PLAY RECAP **********************************************tun1 : ok=1 changed =4 unreachable =0 failed =0tk-atc-ans-v1.1Ton Kersten © 2014 – AT Computing 20 / 28
. .Introduction. . . . . .Why. . . . . . . . . . . . . . .How.Recap. .Resources.Questions?TemplatesAnsible uses the Jinja2 templating engineVariable substitutionLoopsCommentsConditionalsFiltersAnsible facts are availablePuppet Facter facts are available (if installed)Chefs Ohai facts are available (if installed)tk-atc-ans-v1.1Ton Kersten © 2014 – AT Computing 21 / 28
. .Introduction. . . . . .Why. . . . . . . . . . . . . . .How.Recap. .Resources.Questions?Templates.# ansible-playbook playbooks/vtun/main.yml........# Ansible information :# Filedate : {{ ansible_managed }}# Hostname : {{ ansible_hostname }}tunnel {passwd {{ secretpassword }};type tun; # IP tunnelproto tcp; # UDP protocoldevice tun1; # Use this deviceup {# Connection is Upifconfig "%% {{ srvaddr }} pointopoint {{ clntaddr }}";};}# (c) 2011 -{{ ansible_date_time.year }} by {{ name }}tk-atc-ans-v1.1Ton Kersten © 2014 – AT Computing 22 / 28
. .Introduction. . . . . .Why. . . . . . . . . . . . . . .How.Recap. .Resources.Questions?RolesPlaybooks grow large and unreadableStandard way of writing thingsCan easily be shared with others (Through Galaxy).Ansible role directory structuur........thisrole....................................................Top of the rolefiles........................................................Role fileshandlers.................................................Role handlersmain.yml......................................Role handlers starttasks........................................................Role tasksmain.yml......................................Role starting pointtemplates...............................................Role templatesvars .................................................... Role variablesmain.yml.....................................Role variables starttk-atc-ans-v1.1Ton Kersten © 2014 – AT Computing 23 / 28
. .Introduction. . . . . .Why. . . . . . . . . . . . . . .How.Recap. .Resources.Questions?Roles in playbooks.Using roles in playbooks........- hosts: allroles:- common- users- sudo- hosts: webserversroles:- nginx- hosts: tunserversroles:- vtuntk-atc-ans-v1.1Ton Kersten © 2014 – AT Computing 24 / 28
. .Introduction. . . . . .Why. . . . . . . . . . . . . . .How.Recap. .Resources.Questions?RecapEntire Ansible setup is in a git repoCron runs my ansible_run script every hourConfigure authorized_keys to allow management hoststo connectLogs playbook runs to /var/log/ansible.logConfigure logrotate for /var/log/ansible.logUse Ansible callbacks to give feedbackUse sudo for root commandsUse roles as much as possibleMake roles genericDefine variables for site configurationtk-atc-ans-v1.1Ton Kersten © 2014 – AT Computing 25 / 28
. .Introduction. . . . . .Why. . . . . . . . . . . . . . .How.Recap. .Resources.Questions?ResourcesWebsite: http://www.ansible.comDocumentation: http://docs.ansible.comIRC on Freenode: #ansibleTwitter: ansibleReddit: http://www.reddit.com/r/ansibleGoogle Group: https://groups.google.comWeekly newsletter: http://devopsu.comCheckout and study the source from github· · ·tk-atc-ans-v1.1Ton Kersten © 2014 – AT Computing 26 / 28
. .Introduction. . . . . .Why. . . . . . . . . . . . . . .How.Recap. .Resources.Questions?Please!!!!Contribute to Ansible codeContribute to Ansible documentationUse roles from GalaxyShare roles on GalaxySpread the Ansible word. . .tk-atc-ans-v1.1Ton Kersten © 2014 – AT Computing 27 / 28
. .Introduction. . . . . .Why. . . . . . . . . . . . . . .How.Recap. .Resources.Questions?Questions?Questions??.Contact Ton Kersten:........[email protected]http://www.atcomputing.nlhttps://github.com/tonkhttps://speakerdeck.com/tonk@TonKersten on TwitterTKersten on IRC.Created with:........LATEX BeamerVimVim SnippetsEvince....tk-atc-ans-v1.1Ton Kersten © 2014 – AT Computing 28 / 28