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

Provisionando Servidores com Ansible

Renato Suero
September 19, 2015

Provisionando Servidores com Ansible

Apresentação sobre Ansible no Dia da Liberdade de Software 2015 - Softwware Freedom Day

Renato Suero

September 19, 2015
Tweet

More Decks by Renato Suero

Other Decks in Programming

Transcript

  1. Ansible É uma ferramenta open source escrita em Python, para

    gerenciamento de configurações que serve para automatizar tarefas de provisionamento de máquinas. Ele trabalha com Playbooks (livro de receitas) escritos em YAML , e tem uma curva de aprendizagem mínima. Tem outras opções? Chef, Puppet, SaltStack, etc...
  2. Acesso SSH mkdir ~/.ssh chmod 700 ~/.ssh touch ~/.ssh/authorized_keys chmod

    600 ~/.ssh/authorized_keys echo $(curl -L https://github.com/renatosuero.keys) > ~/.ssh/authorized_keys
  3. Instalação PIP $ sudo easy_install pip $ sudo pip install

    ansible APT(Debian/Ubuntu) $ sudo apt-get install software-properties-common $ sudo apt-add-repository ppa:ansible/ansible $ sudo apt-get update $ sudo apt-get install ansible YUM(Centos/RedHat) # install the epel-release RPM if needed on CentOS, RHEL, or Scientific Linux $ sudo yum install ansible
  4. Hosts O arquivo pode conter o IP ou nome do

    servidor server01 192.168.1.123 Podemos separar servidores em grupos [web] server01 server02 [database] db01 Por padrão o arquivo está em /etc/ansible/hosts Mas gosto do arquivo na pasta do projeto =)
  5. Arquivo server.yml --- - hosts: all tasks: - name :

    "Install Curl" shell: sudo apt-get curl $ ansible-playbook server.yml Playbook - Livro de Receitas ansible-playbook server.yml --syntax-check
  6. Módulos Shell -> Era um módulo... Arquivo server.yml --- -

    hosts: all tasks: - name : "Install Curl" apt: name=curl state=latest $ ansible-playbook server.yml
  7. Módulos - Opções name = Nome do pacote state =

    Opção para instalação ou remoção present = instalar pacote latest = instalar a última versão absent = remover update_cache = Atualizar pacotes(apt-get update/ yum update) cache_valid_time = tempo para guardar o cache
  8. Módulos - Loop - name: "Add some apps" apt: name={{

    item }} state=latest with_items: - curl - wget - name: "Add some apps" apt: name={{ item.name }} state={{item.state }} with_items: - {name: 'curl', state: 'absent' } - {name: 'wget', state: 'present }
  9. Módulos - Tags Uma forma de agrupar tarefas no ansible

    tasks: - name: "Install Curl" apt: name=curl state=present tags: - common - name: "Install Git" apt: name=git state=latest tags: - development
  10. E se tiver diferentes distribuições, como faz ? - name:

    "Install Curl" apt: name=curl state=present when: ansible_os_family == "Debian" - name: "Install Curl" yum: name=curl state=present when: ansible_os_family == "CentOS"
  11. Includes server.yml - hosts: all tasks: - include: tasks/commom.yml tasks/commom.yml

    - name: "Install Curl" apt: name=curl state=present tags: - common
  12. Variáveis e Templates/arquivos? - hosts: all vars: timezone: America/Sao_Paulo tasks:

    - name: Criando arquivo teste copy: src=files/teste.txt dest=/home/renato/ - name: Configurando o Timezone template: src=templates/timezone.js dest=/etc/timezone - name: Reconfigurando o Timezone command: dpkg-reconfigure -f noninteractive tzdata
  13. Register -> Salvando vidas e evitando retrabalho - name: Configurando

    o Timezone template: src=templates/timezone.j2 dest=/etc/timezone register: timezone_alterado - name: Reconfigurando o Timezone command: dpkg-reconfigure -f noninteractive tzdata when: timezone_alterado.changed
  14. E se quiser interagir com meu playbook? vars_prompt: - name:

    "file" prompt: "Digite o nome do arquivo" tasks: - name: Criando arquivo teste copy: src=files/teste.txt dest=/home/renato/{{ file }}.txt
  15. Diga que não é privado vars_prompt: - name: "file" prompt:

    "Digite o nome do arquivo" private: no tasks: - name: Criando arquivo teste copy: src=files/teste.txt dest=/home/renato/{{ file }}.txt
  16. Handlers server.yml handlers: - include: handlers/fail2ban.yml tasks: - include: tasks/security.yml

    tasks/security.yml - name: Install fail2ban apt: name=fail2ban state=latest - name: Copy file fail2ban copy: src=files/fail2ban/jail.local dest=/etc/fail2ban/ notify: - restart fail2ban handlers/fail2ban.yml - name: restart fail2ban service: name=fail2ban state=restarted NÃO É O MELHOR EXEMPLO MAS ….
  17. Start e Steps É possível definir a partir de qual

    task deve começar usando o comando: ansible-playbook server.yml --start-at-task="Install some apps" Também podemos acompanhar as tarefas e decidir quais queremos executar usando o comando: ansible-playbook server.yml --step y -> Executa a tarefa n -> Pula a tarefa c -> Não perguntará mais
  18. Ansible Galaxy - Instalando múltiplos roles requirements.txt adriagalin.timezone Podemos instalar

    vários pacotes de uma vez Principalmente, mantemos controle das dependências do projeto =)
  19. Ansible Galaxy - Usando role instalado server.yml - hosts: all

    vars: - ag_timezone: Etc/UTC roles: - { role: adriagalin.timezone } tasks: - include: tasks/commom.yml