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

Keep Simple your infrastructure with Ansible

devjlopez
September 24, 2016

Keep Simple your infrastructure with Ansible

Getting started with Ansible

devjlopez

September 24, 2016
Tweet

More Decks by devjlopez

Other Decks in Programming

Transcript

  1. Javier López López < Software Architect PHP > Aurea Inc.

    <Scrum Master Certified> PHP, LEADING, GO, ARCHITECTURE, TESTING, SCRUM Javier López López | PHPConfMX 2016
  2. Ansible Javier López López | PHPConMX 2016 What is Ansible?

    Ansible is a radically simple IT automation engine that automates cloud provisioning, configuration management, application deployment, intra-service orchestration, and many other IT needs. ORCHESTRATION ENGINE
  3. How it works? Javier López López | PHPConMX 2016 Vagrant

    Test Servers Production Servers SSH SSH SSH Ansible
  4. Installation Javier López López | PHPConMX 2016 1. Install Homebrew

    2. Install Python 2.7.x 3. Install Ansible $ sudo pip install ansible #or $brew install ansible OSX $ sudo apt-add-repository -y ppa:ansible/ansible $ sudo apt-get update $ sudo apt-get install -y ansible LINUX Download and install Cygwin install separately PyYAML and Jinja2 Extra instructions: http://www.jeffgeerling.com/blog/ running-ansible-within-windows WINDOWS
  5. Javier López López | PHPConMX 2016 Ansible Concepts Inventory file

    List of server to connect using Ansible [develop] localhost ansible_connection=local [testing] other1.example.com ansible_connection=ssh ansible_user=javier other2.example.com ansible_connection=ssh ansible_user=root [production] 123.321.123.111 ansible_user=root ansible_ssh_pass=itWorks
  6. Ansible Concepts Javier López López | PHPConMX 2016 Modules Ansible

    ships with a number of modules (called the ‘module library’) that can be executed directly on remote hosts or through Playbooks. Users can also write their own modules. These modules can control system resources, like services, packages, or files (anything really), or handle executing system commands. http://docs.ansible.com/ansible/list_of_all_modules.html ‣ MySQL ‣ Redis ‣ Mongo ‣ Copy ‣ Replace ‣ RabbitMQ ‣ Azure ‣ CloudFormation Amazon ‣ Composer ‣ Amazon EC2 ‣ Git ‣ New Relic ‣ Amazon S3 ‣ Etc……..
  7. Ansible Concepts Javier López López | PHPConMX 2016 Playbook Playbooks

    are Ansible’s configuration, deployment, and orchestration language. They can describe a policy you want your remote systems to enforce, or a set of steps in a general IT process. http://docs.ansible.com/ansible/playbooks.html
  8. Getting Started Javier López López | PHPConMX 2016 $ ansible

    hosts -i inventory_file —m module -a arguments $ ansible all -i “localhost,” —m ping
  9. Javier López López | PHPConMX 2016 Playbook --- hosts: all

    remote_user: root tasks: - name: install apache2 apt: name=apache2 update_cache=yes state=latest notify: - restart apache handlers: - name: restart apache service: name=apache state=restarted playbook.yml Hosts User Description Command Task List Handlers List Description Command Notification Handler Name
  10. Javier López López | PHPConMX 2016 Running Ansible playbook $

    ansible-playbook playbook.yml -i inventory
  11. IDEMPOTENCE Javier López López | PHPConMX 2016 In computer science,

    the term idempotent is used more comprehensively to describe an operation that will produce the same results if executed once or multiple times https://en.wikipedia.org/wiki/Idempotence#Computer_science_meaning
  12. Javier López López | PHPConMX 2016 PHP Stack ✦Linux ✦Nginx

    ✦Configuration ✦PHP ✦Configuration ✦Modules ✦Composer
  13. Ansible Javier López López | PHPConMX 2016 ‣ Roles ‣

    Tasks ‣ Handlers ‣ Loops ‣ Variables ‣ Conditionals ‣ Templates ‣ Etc… http://docs.ansible.com/ansible/playbooks.html
  14. Ansible Roles Javier López López | PHPConMX 2016 Roles in

    Ansible build on the idea of include files and combine them to form clean, reusable abstractions – they allow you to focus more on the big picture and only dive down into the details when needed http://docs.ansible.com/ansible/playbooks_roles.html
  15. Ansible Roles Javier López López | PHPConMX 2016 ├── defaults

    │ └── main.yml ├── handlers │ └── main.yml ├── meta │ └── main.yml ├── tasks │ └── main.yml ├── templates │ └── apache2_php.ini.j2 └── vars └── main.yml Variables PHP Role Dependencies Handlers Templates Tasks Variables
  16. Loops Javier López López | PHPConMX 2016 http://docs.ansible.com/ansible/playbooks_loops.html - name:

    add several users user: name={{ item }} state=present groups=wheel with_items: - alice - bob - charlie Task Name Module Array Items REPLACE user: name=alice state=present groups=wheel user: name=bob state=present groups=wheel user: name=charlie state=present groups=wheel
  17. Nested Loops Javier López López | PHPConMX 2016 http://docs.ansible.com/ansible/playbooks_loops.html -

    name: give users access with different passwords mysql_user: name={{ item[0] }} priv=*.*:ALL append_privs=yes password={{ item[1] }} with_nested: - [ 'alice', 'bob' ] - [ 'clientdb', ‘employeedb', ‘more_data’] mysql_user: name=alice priv=*.*:ALL append_privs=yes password=bob mysql_user: name=clientdb priv=*.*:ALL append_privs=yes password=employeedb
  18. More Loops Javier López López | PHPConMX 2016 http://docs.ansible.com/ansible/playbooks_loops.html ‣

    Standard Loops ‣ Nested Loops ‣ Loops over hashes ‣ Loops over Files ‣ Loops over Fileglobs ‣ Loops over Parallel Sets of Data ‣ Loops over subelements ‣ Random choices ‣ Etc…..
  19. Variables Javier López López | PHPConMX 2016 - hosts: all

    vars: apache_service: apache2 - name: install apache2 apt: name={{ apache_service }} update_cache=yes state=latest http://docs.ansible.com/ansible/playbooks_variables.html Definition Usage
  20. Conditionals Javier López López | PHPConMX 2016 tasks: - name:

    "shut down Debian flavored systems" command: /sbin/shutdown -t now when: ansible_os_family == "Debian" Task Name Module Conditional http://docs.ansible.com/ansible/playbooks_conditionals.html
  21. Templates Javier López López | PHPConMX 2016 tasks: - name:

    “Copy files" template: src=/mytemplates/foo.j2 dest=/etc/file.conf owner=bin group=wheel mode=0644 Jinja2 Template Template processed http://docs.ansible.com/ansible/template_module.html
  22. Ansible Galaxy Javier López López | PHPConMX 2016 $ ansible-galaxy

    install flyapen.jenkins https://galaxy.ansible.com/ ➡Explore Username Rolename
  23. Thanks Javier López López | PHPConMX 2016 Javier López López

    Software Architect & Scrum Master @devjlopez http://www.devjlopez.com/ [email protected]