Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Ansible: nuovi paradigmi per l'orchestration

OpsCon
October 15, 2015

Ansible: nuovi paradigmi per l'orchestration

Il tool emergente di quest'anno nello scenario dei configuration management è senza dubbio Ansible: semplicità di apprendimento e mantenimento della soluzione sono le chiavi del suo successo, rendondolo la scelta ideale sia nei piccoli ambienti on-premise sia in quelli più complessi e dinamici del Cloud. Al contrario di altri strumenti, Ansible non ci costringe ad imparare un nuovo linguaggio come capita con Ruby in altri strumenti quali Puppet e Chef. Capiremo come convertire i nostri vecchi script bash a playbook Ansible idempotenti e vedremo come sia possibile strutturare i nostri playbook per fare un deployment di una classica applicazione 3 Tier su stack tecnologico PHP - Paolo Tonin #RoadToOpsCon #OpsConPisa

OpsCon

October 15, 2015
Tweet

More Decks by OpsCon

Other Decks in Technology

Transcript

  1. What this IS Some reason why you might consider Ansible

    Use case that works for mine usecase, MAYBE for you
  2. Why

  3. YAML vs RubyDSL user{"$user": managehome=>true, ensure => present, } file{"/home/$user":

    ensure=>directory, mode=>700, require=>User["$user"], } file{"/home/$user/.ssh": ensure=>directory, require=>File["/home/$user"], }
  4. Ansible is… (2) Pro Needs only SSH, agentless! NO extra

    programming language Idempotent Low learning curve
  5. Installation Pro On Mac OSX is a piece of cake

    Debian/Ubuntu $ brew update $ brew install ansible $ sudo apt-add-repository -y ppa:ansible/ansible $ sudo apt-get update $ sudo apt-get -y install ansible "Windows is not official supported as controller machine"
  6. Installation •Servers should be accessible via SSH using keypair authentication

    •It's reccomended to have a user with sudo permission to run the tasks in the server How to configure SSH access for running Ansible bit.ly/ansible-ssh
  7. Ad-hoc commands $ ansible web1 -a “free -m” $ ansible

    webservers -a “php -v” $ ansible all -a “sudo yum -y update”
  8. Ad-hoc commands 173.194.113.19 | success >> { “changed”: false, “ping”:

    “pong” } web1.mydomain.com | success >> { “changed”: false, “ping”: “pong” } $ ansible -all -m ping mongo_mm.local | success >> { “changed”: false, “ping”: “pong” } mongo1.local | success >> { “changed”: false, “ping”: “pong” }
  9. Ad-hoc commands 173.194.113.19 | success | rc=0 >> PHP 5.6.13

    (cli) (built: Sep 3 2015 14:08:58) Copyright (c) 1997-2015 The PHP Group Zend Engine v2.6.0, Copyright (c) 1998-2015 Zend Technologies with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2015, by Zend Technologies web1.mydomain.com | success | rc=0 >> PHP 5.6.13 (cli) (built: Sep 3 2015 14:08:58) Copyright (c) 1997-2015 The PHP Group Zend Engine v2.6.0, Copyright (c) 1998-2015 Zend Technologies with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2015, by Zend Technologies $ ansible webservers -a “php -v”
  10. Modules All standard modules are part of core, no abandoned

    modules All core modules are written in Python You can write custom modules in any language (eg. helper code in Ruby)
  11. Modules $ ansible all -s -m shell -a 'apt-get install

    nginx' $ ansible all -s -m shell -a 'service mysqld restart'
  12. Modules •Run arbitrary commands •Copy files to and from servers

    •Install packages •Manage daemons •Manage user and groups •Gather facts
  13. Tasks - name: install memcached yum: name=memcached state present -

    name: create database user mysql_user: name=bob.priv password=yaiShie8 priv=*.*:app_db state=present - name: Update apt apt: update_cache=yes
  14. Tasks - name: install memcached yum: name=memcached state present -

    name: create database user mysql_user: name=bob.priv password=My_Passwd priv=*.*:app_db state=present - name: Update apt apt: update_cache=yes
  15. Playbooks Plays are ordered set of tasks Written in YAML,

    declarative, no coding required Executed in the order it is written
  16. #!/bin/bash if ! rpm -qa | grep -qw ntp; then

    yum install ntp fi if ps aux | grep -v grep | grep “[n]ntpd” > /dev/null then echo “ntpd is running” > /dev/null else /sbin/service ntpd restart > /dev/null echo “Started ntpd” fi chkconfig ntpd on Ansible basics
  17. A Simple Playbook --- - hosts: all sudo: yes tasks:

    - yum: name=ntp state=installed - service: name=ntpd state=started enabled=yes # playbook.yml Defines hosts or Groups
  18. A Simple Playbook --- - hosts: all sudo: yes tasks:

    - yum: name=ntp state=installed - service: name=ntpd state=started enabled=yes # playbook.yml
  19. A Simple Playbook --- - hosts: all sudo: yes tasks:

    - yum: name=ntp state=installed - service: name=ntpd state=started enabled=yes # playbook.yml
  20. A Simple Playbook --- - hosts: all sudo: yes tasks:

    - name: Update apt-cache - apt: update_cache=yes - name: Install Nginx - apt: pkg=nginx state=latest # playbook.yml
  21. Templates Autogenerating configurations Jinja2 Python template engine (similar to Twig

    for PHP) It can be anything (like config files, system settings etc..)
  22. Templates <Virtualhost *:80> ServerName {{ domain }} ServerAlias www.{{ domain

    }} DocumentRoot {{ doc_root }} <Directory {{ doc_root }}> AllowOverride AuthConfig Require all granted </Directory> </Virtualhost> # templates/vhost.conf.j2
  23. -- - hosts: web1 sudo:yes vars: domain: readyfortraffic.com doc_root: /var/www/readyfortraffic.com/

    tasks: - name: Add a new virtualhost template: src=templates/vhost.conf.j2 dest=/etc/httpd/conf.d/{{ domain }}.conf notify: restart httpd - name: Add a welcome page template: src=templates/index.html dest={{ doc_root }}/index.html backup=yes handlers: - name: restart httpd service: name=httpd state=restarted
  24. Conditions & Loops - name: Install PHP packages apt: name={{

    item }} state=latest with_items: - php5-fpm - php5-cli - php5-mysql - php5-pdo - php5-mcrypt - php5-curl - php5-memcache when: ansible_distribution == 'Ubuntu'
  25. Conditions & Loops - name: Install PHP packages apt: name={{

    item }} state=latest with_items: - php5-fpm - php5-cli - php5-mysql - php5-pdo - php5-mcrypt - php5-curl - php5-memcache when: ansible_distribution == 'Ubuntu'
  26. - hosts: all gather_facts: yes remote_user: updater sudo: yes tasks:

    - name: Update Shellshock (Debian) apt: name=bash state=latest update_cache=yes when: ansible_os_family == "Debian" - name: Update Shellshock (RedHat) yum: name=bash state=latest update_cache=yes when: ansible_os_family == "RedHat"
  27. - hosts: all gather_facts: yes remote_user: updater sudo: yes tasks:

    - name: Update OpenSSL (Debian) apt: name={{ item }} state=latest update_cache=yes with_items: - openssl when: ansible_os_family == "Debian" - name: Update OpenSSL (RedHat) yum: name={{ item }} state=latest update_cache=yes with_items: - openssl when: ansible_os_family == "RedHat" post_tasks: - name: Reboot servers command: reboot
  28. Ansible basics Cloud Providers Support for most major Cloud providers;

    AWS, Rackspace, Azure, DigitalOcean etc.. Provision, configure networking, storage, manage security etc...
  29. Ansible for AWS Install python module on execution host $

    yum install python-boto awscli Add localhost to inventory [local] localhost Pattern used in playbooks for provisioning - hosts: localhost connection: local gather_facts: false $ pip install python-boto
  30. Ansible for AWS --- - hosts: locahost connection: local tasks:

    - name: create ec2 instance with volume that already exists action: module: ec2 zone: eu-central-1a image: ami-a1b2c3d4 instance_type: t2.medium state: present region: eu-central vpc_subnet_id: subnet-abcd1234 group: sg-ssss9999 volumes: - device_name: /dev/sda1 device_type: gp2 volume_size: 20 delete_on_termination: true