Ansible, why and how I use it.
AnsibleWhy and how I use it!Ton KerstenAT ComputingAntwerp, Belgium
View Slide
Introduction Why How Recap Resources Questions?Agenda1 Introduction2 Why3 How4 Recap5 Resources6 Questions?tk-atc-ans-v1.3Ton Kersten © 2014 - AT Computing 2 / 29
Introduction Why How Recap Resources Questions?$ who am iUNIX/Linux consultant and Trainer @ AT ComputingUNIX 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.3Ton Kersten © 2014 - AT Computing 3 / 29
Introduction Why How Recap Resources Questions?Long agoShell scriptsSSH loopsParallel SSHCluster SSHScreen synchronized windowstmux synchronized panes· · ·Things got out of controltk-atc-ans-v1.3Ton Kersten © 2014 - AT Computing 4 / 29
Introduction Why How Recap Resources Questions?NextCF EnginePuppetChefSalt StackJujuCapistranoFabric· · ·tk-atc-ans-v1.3Ton Kersten © 2014 - AT Computing 5 / 29
Introduction Why How Recap Resources Questions?What I wantSimple command[email protected] # 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.3Ton Kersten © 2014 - AT Computing 6 / 29
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, continuousdeliverySimple configuration filesIdempotenttk-atc-ans-v1.3Ton Kersten © 2014 - AT Computing 7 / 29
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.3Ton Kersten © 2014 - AT Computing 8 / 29
Introduction Why How Recap Resources Questions?Simple components (Commands)Commandsansible ⇒ The main Ansible commandansible-playbook ⇒ Command to run playbooksansible-pull ⇒ The main Ansible pull commandansible-doc ⇒ Ansible documentation programansible-galaxy ⇒ Command to interact with Galaxyansible-vault ⇒ The Ansible password vaulttk-atc-ans-v1.3Ton Kersten © 2014 - AT Computing 9 / 29
Introduction Why How Recap Resources Questions?Simple components (Modules)A lot of modules (220+ at this moment)CommandsFiles / templatingUsersPackages (yum, apt, zypper, …)ServicesDatabases· · · (See: ansible-doc)Or, write your owntk-atc-ans-v1.3Ton Kersten © 2014 - AT Computing 10 / 29
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 ansibleOn Debian / UbuntuAvailable in standard repository# apt-get 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.3Ton Kersten © 2014 - AT Computing 11 / 29
Introduction Why How Recap Resources Questions?How it worksModule(s)ManagementnodeNodeNodeNodePlaybooksorrolesHostsno daemonscommunicationover SSHtk-atc-ans-v1.3Ton Kersten © 2014 - AT Computing 12 / 29
Introduction Why How Recap Resources Questions?My example networkManagementnodeand DNSTunnel serverWeb serverWeb serverdns1.example.net192.168.56.11/24web1.example.net192.168.56.12/24web2.example.net192.168.56.13/24tun1.example.net192.168.56.14/24tk-atc-ans-v1.3Ton Kersten © 2014 - AT Computing 13 / 29
Introduction Why How Recap Resources Questions?Inventory file# cat /etc/ansible/hostsdns1web1web2tun1[dnsservers]dns1[webservers]web1web2[tunservers]tun1tk-atc-ans-v1.3Ton Kersten © 2014 - AT Computing 14 / 29
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.3Ton Kersten © 2014 - AT Computing 15 / 29
Introduction Why How Recap Resources Questions?Running AnsibleGeneral ansible command form:ansible -m -a # ansible all -m ping -oweb2 | 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.3Ton Kersten © 2014 - AT Computing 16 / 29
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.3Ton Kersten © 2014 - AT Computing 17 / 29
Introduction Why How Recap Resources Questions?Installing a package# ansible tunservers -m yum -a name=vtuntun1 | success >> {"changed": false,"msg": "","rc": 0,"results": ["vtun-3.0.2-1.el6.rf.x86_64 providingvtun is already installed"]}tk-atc-ans-v1.3Ton Kersten © 2014 - AT Computing 18 / 29
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.3Ton Kersten © 2014 - AT Computing 19 / 29
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.3Ton Kersten © 2014 - AT Computing 20 / 29
Introduction Why How Recap Resources Questions?Playbook run# ansible-playbook playbooks/vtun/main.ymlPLAY [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.3Ton Kersten © 2014 - AT Computing 21 / 29
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.3Ton Kersten © 2014 - AT Computing 22 / 29
Introduction Why How Recap Resources Questions?Templates# cat playbooks/vtun/vtund.conf.j2# 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) 2012-{{ ansible_date_time.year }} by {{ name }}tk-atc-ans-v1.3Ton Kersten © 2014 - AT Computing 23 / 29
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 structuurthisrole.............................................................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.3Ton Kersten © 2014 - AT Computing 24 / 29
Introduction Why How Recap Resources Questions?Roles in playbooksUsing roles in playbooks- hosts: allroles:- common- users- sudo- hosts: webserversroles:- nginx- hosts: tunserversroles:- vtuntk-atc-ans-v1.3Ton Kersten © 2014 - AT Computing 25 / 29
Introduction Why How Recap Resources Questions?RecapEntire Ansible configuration is in a git repoUse sudo for root commandsConfigure authorized_keys for connectionsRun ansible script every hourLog playbook runs to /var/log/ansible.logUse Ansible callbacks to give feedbackUse roles as much as possibleMake roles genericDefine variables for site configurationtk-atc-ans-v1.3Ton Kersten © 2014 - AT Computing 26 / 29
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.3Ton Kersten © 2014 - AT Computing 27 / 29
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.3Ton Kersten © 2014 - AT Computing 28 / 29
Introduction Why How Recap Resources Questions?Questions?Questions??Contact me[email protected]http://www.atcomputing.nlhttps://github.com/tonkhttps://speakerdeck.com/tonk@TonKersten on TwitterTKersten on IRCCreated withLATEX BeamerVimVim SnippetsThe GimpEvincetk-atc-ans-v1.3Ton Kersten © 2014 - AT Computing 29 / 29