Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Introdução ao Ansible - Meetup São Paulo
Search
Ricardson
October 13, 2015
Technology
0
720
Introdução ao Ansible - Meetup São Paulo
Apresentação no primeiro Meetup de Ansible em São Paulo
Ricardson
October 13, 2015
Tweet
Share
Other Decks in Technology
See All in Technology
TypeScript開発にモジュラーモノリスを持ち込む
sansantech
PRO
3
840
実践! ソフトウェアエンジニアリングの価値の計測 ── Effort、Output、Outcome、Impact
nomuson
0
680
普通のエンジニアがLaravelコアチームメンバーになるまで
avosalmon
0
650
スタートアップで取り組んでいるAzureとMicrosoft 365のセキュリティ対策/How to Improve Azure and Microsoft 365 Security at Startup
yuj1osm
0
280
pg_bigmをRustで実装する(第50回PostgreSQLアンカンファレンス@オンライン 発表資料)
shinyakato_
0
140
日本版とグローバル版のモバイルアプリ統合の開発の裏側と今後の展望
miichan
1
470
[Oracle TechNight#85] Oracle Autonomous Databaseを使ったAI活用入門
oracle4engineer
PRO
1
200
[JAWS-UG新潟#20] re:Invent2024 -CloudOperationsアップデートについて-
shintaro_fukatsu
0
150
I could be Wrong!! - Learning from Agile Experts
kawaguti
PRO
8
1.6k
10年もののバグを退治した話
n_seki
0
140
型情報を用いたLintでコード品質を向上させる
sansantech
PRO
2
200
AIエージェントに脈アリかどうかを分析させてみた
sonoda_mj
2
130
Featured
See All Featured
What's in a price? How to price your products and services
michaelherold
244
12k
Docker and Python
trallard
43
3.2k
Building Better People: How to give real-time feedback that sticks.
wjessup
366
19k
The Language of Interfaces
destraynor
155
24k
A Philosophy of Restraint
colly
203
16k
GraphQLとの向き合い方2022年版
quramy
44
13k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
33
2.7k
Why Our Code Smells
bkeepers
PRO
335
57k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
230
52k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
29
2k
Intergalactic Javascript Robots from Outer Space
tanoku
270
27k
Bootstrapping a Software Product
garrettdimon
PRO
305
110k
Transcript
Introdução ao Ansible - Ansible Meetup São Paulo. © Ricardson
Williams 1
➜ ~ > whoami — SysAdmin por mais de 15
anos. — DevOps há 1 ano. — Instrutor Linux. /ricardson /ansible-Sao-Paulo © Ricardson Williams 2
Ansible foi criado por Michael Dehaan com a ideia principal
de ser simples e poderosa. © Ricardson Williams 3
© Ricardson Williams 4
➜ Podemos usar Ansible para..... — Gerenciamento Configuração — Provisionamento
— Deploy — Alternativa a Capistrano/Fabric — Quem usa??? © Ricardson Williams 5
➜ Simplicidade… — Sintax simples (YAML) — Segue uma ordenação
— Pode-se rodar a partir de uma desktop/servidor/laptop (Fácil de configurar) — Módulos — Utilizam JSON para se comunicar. — Podem ser desenvolvidos nas principais linguagens Ruby, Python, Shell, etc. — Playbook intuitivo. — Filosofia KISS1(é um princípio geral que valoriza a simplicidade) 1 KISS © Ricardson Williams 6
➜ Agentless — SSH para transporte: Seguro, Rápido e Simples.
— Não necessita “deploy” de agente. — Descentralização. © Ricardson Williams 7
➜ Seguro — Utiliza SSH. — Não necessita abertura de
portas. — Não necessita de codigo "extra" para administrar. — Não instala “daemons” vulneráveis nos Servidores/ Nodes © Ricardson Williams 8
➜ Idempotência… O comando/task(playbook) será aplicado apenas quando precisar ser
aplicado. © Ricardson Williams 9
Instalando Ansible — RHEL/CentOS/Fedora: ↪ $ sudo yum install ansible
— Python Pip: ↪ $ pip install ansible — Mac: ↪ $ brew install ansible — Deb/Ubuntu: ↪ sudo apt-add-repository ppa:ansible/ansible ↪ sudo apt-get update ↪ sudo apt-get install ansible © Ricardson Williams 10
➜ Inventário Simples INI (/etc/ansible/hosts) podemos utilizar o "-i" e
indicar outro arquivo de inventário2. [vagrant] 192.168.60.4 [production] www.myapp.com www2.myapp.com www3.myapp.com ansible_ssh_port=3522 [production:vars] http_port=80 ansible_ssh_user=ec2-user 2 Intro Inventory © Ricardson Williams 11
➜ Módulos Mais de 300 módulos3 embutidos. — apt/yum/portge/zypper (S.O)
=> Instalação de pacotes. — pip/npm/cpanm (Linguagens) => Instalação de Lib/pacotes. — command/shell => Execução de comandos shell/scripts. — copy => Copia de arquivos (origem/destino). — file => Criação de diretórios, link simbólico, troca de permissões. — service => Gerenciamento de Serviços (Liga/Desliga/Habilita). — docker/docker_image => Gerenciamento de imagens e containers docker. — git/subversion => Suporte as ferramentas SCM. 3 Modules by Category © Ricardson Williams 12
➜ Facts Informações sobre máquinas/nodes como nome/versão, ips, etc, podem
ser utilizadas em playbooks. $ ansible localhost -m setup localhost | success >> { "ansible_facts": { "ansible_all_ipv4_addresses": [ "10.0.2.15" ], "ansible_distribution": "CentOS", ”ansible_distribution_major_version": “7”, "ansible_distribution_release": "Core", "ansible_distribution_version": "7.1.1503", "ansible_domain": "localdomain", } } $ ansible localhost -m setup -a ‘filter=ansible_distribution’ localhost | success >> { "ansible_facts": { "ansible_distribution": "CentOS" }, "changed": false } © Ricardson Williams 13
➜ Ad-hoc4 — Esta funcionalidade o Michael "pegou emprestado" do
FUNC (Fedora Unified Network Controller). — O Ad-hoc nos permite executar comandos nas maquinas remotas sem a necessidade de se logar. $ ansible <host/group> -m MODULE-NAME -a MODULE-ARGS 4 AD-Hoc Intro © Ricardson Williams 14
//Ping da maquina local $ ansible localhost -m ping //Reinicia
todas as maquina de 10 em 10 $ ansible all -s -m command -a “/sbin/reboot” -f 10 //Instala pacote "ntp" $ ansible production -s -m yum -a “name=ntp state=present” //Inicia o serviço e ja habilitando o "start" $ ansible production -s -m service -a "name=ntpd state=started enabled=yes" //Remove pacote apache $ ansible production -s -m yum -a “name=httpd state=absent” //??? $ ansible app2 -s -m raw -a "pkg_add ftp://ftp.openbsd.org/pub/OpenBSD/5.7/packages//amd64/python-2.7.9p0.tgz" © Ricardson Williams 15
Ad-hoc… $ ansible vagrant -s -m shell -a "uname -a"
192.168.60.4 | success | rc=0 >> Linux app1 3.10.0-229.4.2.el7.x86_64 #1 SMP Wed May 13 10:06:09 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux 192.168.60.5 | success | rc=0 >> OpenBSD app2 5.7 GENERIC#825 amd64 $ ansible vagrant -s -m shell -a "ssh -V localhost" 192.168.60.4 | success | rc=0 >> OpenSSH_6.6.1p1, OpenSSL 1.0.1e-fips 11 Feb 2013 192.168.60.5 | success | rc=0 >> OpenSSH_6.8, LibreSSL 2.1 © Ricardson Williams 16
Playbook5 — Playbook utiliza o formato YAML simplificado a ideia
é não se tornar linguagem de programação e sim uma sintaxe simples de configuração. — Playbook é composto por um ou mais “plays” 5 Playbooks Intro © Ricardson Williams 17
Exemplo de playbook 1 --- - hosts: vagrant gather_facts: yes
sudo: true vars: packages_base: - vim - telnet - git - epel-release tasks: - name: Install Packages yum: name={{ item }} state=latest with_items: - “{{ packages_base }}” when: ansible_os_family == ‘RedHat’ ##Facts - name: Upgrade all packages yum: name=* state=latest when: ansible_os_family == ‘RedHat’ ##Facts tags: pkg_upgrade © Ricardson Williams 18
Organizando melhor o playbook (roles) Para melhor organização dos nossos
playbooks criamos o que chamamos de roles6 que seria basicamente a estrutura abaixo. Por padrão o Ansible sempre executa/ procura o arquivo mail.yml. 6 Playbooks Roles | Ansible Roles © Ricardson Williams 19
site.yml (Playbook que vai chamar a "role" init) roles/ `--
init (Nome da minha “role”) |-- task (playbook) | `-- main.yml `-- vars (variável) `-- main.yml © Ricardson Williams 20
Criando site.yml --- - hosts: vagrant sudo: true roles: -
init © Ricardson Williams 21
roles/init/tasks/main.yml --- - name: Install Packages yum: name={{ item }}
state=latest with_items: - “{{ packages_base }}” when: ansible_os_family == ‘RedHat’ - name: Upgrade all packages yum: name=* state=latest when: ansible_os_family == ‘RedHat’ tags: pkg_upgrade © Ricardson Williams 22
roles/init/vars/main.yml --- packages_base: - vim - telnet - git -
epel-release © Ricardson Williams 23
Executando o playbook $ ansible-playbook site.yml PLAY [vagrant] **************************************************************** GATHERING
FACTS *************************************************************** ok: [192.168.60.4] TASK: [init | Install Packages] ********************************** changed: [192.168.60.4] => (item=vim,git,net-tools,trace route,mc,links,epel-release) TASK: [init | Upgrade all packages] ****************************** changed: [192.168.60.4] PLAY RECAP ******************************************************************** 192.168.60.4 : ok=3 changed=2 unreachable=0 failed=0 © Ricardson Williams 24
Executando o playbook (Idempotência) $ ansible-playbook site.yml PLAY [vagrant] ****************************************************************
GATHERING FACTS *************************************************************** ok: [192.168.60.4] TASK: [init | Install Packages] ********************************** ok: [192.168.60.4] => (item=vim,git,net-tools,trace route,mc,links,epel-release) TASK: [init | Upgrade all packages] ****************************** ok: [192.168.60.4] PLAY RECAP ******************************************************************** 192.168.60.4 : ok=3 changed=0 unreachable=0 failed=0 © Ricardson Williams 25
Playbook - Tags $ ansible-playbook site.yml --tags pkg_upgrade PLAY [vagrant]
**************************************************************** GATHERING FACTS *************************************************************** ok: [192.168.60.4] TASK: [init | Upgrade all packages] ******************************************* changed: [192.168.60.4] PLAY RECAP ******************************************************************** 192.168.60.4 : ok=2 changed=1 unreachable=0 failed=0 © Ricardson Williams 26
Playbook - Tags (Idempotência) $ ansible-playbook site.yml --tags pkg_upgrade PLAY
[vagrant] **************************************************************** GATHERING FACTS *************************************************************** ok: [192.168.60.4] TASK: [init | Upgrade all packages] ******************************************* ok: [192.168.60.4] PLAY RECAP ******************************************************************** 192.168.60.4 : ok=2 changed=0 unreachable=0 failed=0 © Ricardson Williams 27
Idempotência??? Quando criamos uma task temos que garantir que seja
repetível e que seja executado apenas se necessário, vejamos abaixo um exemplo de task não idempotente. --- - name: Copy .zshrc template command: cp ~/.oh-my-zsh/templates/zshrc.zsh-template ~/.zshrc remote_user: ryan sudo: false TASK: [Copy .zshrc template] ************************************************** changed: [104.131.3.142] © Ricardson Williams 28
Idempotência Com a opção "creates" o comando só será executado
se o arquivo ".zshrc" não existir. --- - name: Copy .zshrc template command: creates="~/.zshrc" cp ~/.oh-my-zsh/templates/zshrc.zsh-template ~/.zshrc remote_user: ryan sudo: false TASK: [Copy .zshrc template] ************************************************** skipping: [104.131.3.142] © Ricardson Williams 29
© Ricardson Williams 30