![](https://habrastorage.org/webt/bk/vw/ds/bkvwdsdsfvgvjf_ein3uddyocbm.png)
Esta é a transcrição do desempenho em DevOps-40 2020-03-18 :
A partir do segundo commit, qualquer cĂłdigo se torna herdado, porque as idĂ©ias iniciais começam a divergir da dura realidade. Isso nĂŁo Ă© bom nem ruim, Ă© um dado com o qual Ă© difĂcil discutir e Ă© necessĂĄrio se dar bem. Parte desse processo Ă© refatoração. Refatorando a infraestrutura como cĂłdigo. Deixe a histĂłria começar como refatorar Ansible em um ano e nĂŁo voar fora das bobinas.
A Origem Legada
Dia 1: Paciente Zero
![](https://habrastorage.org/webt/jm/mn/9q/jmmn9qcfnbprr8maq3k2zpp27ww.png)
Era uma vez um projeto condicional. Ele tinha uma equipe de desenvolvimento de desenvolvimento e engenheiros de operaçÔes. Eles resolveram o mesmo problema: como implantar o servidor e executar o aplicativo. O problema era que cada equipe resolveu esse problema do seu jeito. O projeto decidiu usar o Ansible para sincronizar o conhecimento entre as equipes de Dev e Ops.
Dia No. 89: O nascimento do legado
![](https://habrastorage.org/webt/p1/gg/sr/p1ggsrxayo0fachydvmwhqtloek.png)
, , legacy. ?
- , â .
- .
- Ansible / Python / Bash / Terraform! !
- Full Stack Overflow Developer stackoverflow, , .
, , , , , , , , .
- hosts: localhost
tasks:
- shell: echo -n Z >> a.txt && cat a.txt
register: output
delay: 1
retries: 5
until: not output.stdout.find("ZZZ")
â 109:
![](https://habrastorage.org/webt/52/-m/km/52-mkm0nvoywmsugy7gscnstjh0.png)
IaC / / , . , .
IaC
â 139: ?
![](https://habrastorage.org/webt/ih/ty/rh/ihtyrh6qo2oufdjxheiflkp1erg.png)
:
- ?
- ?
- ?
, . .. ( , 200 000 ), .
â 149:
![](https://habrastorage.org/webt/ml/58/cc/ml58ccbdsrcynuz6be0o76uw4tm.png)
e . . , . - , confluence, " ?" " ?" . : / . , , - .
![](https://habrastorage.org/webt/pn/98/jt/pn98jtnydc1wupw_jvifjjc9mpc.png)
, . , , : , , .
Ansible
Ansible , , .
â -997: SDS provision
![](https://habrastorage.org/webt/wb/sx/vi/wbsxviuyfwt5ngzmn92n6wpcs6a.png)
Ansible SDS (Software Defined Storage).
, , 60-90 , . e2e , .. , . . .
â -701: Ansible test kitchen
![](https://habrastorage.org/webt/kj/la/wd/kjlawd4ptlxrpsoa9bhechz57jc.png)
Ansible , test kitchen / kitchen-ci inspec. Ruby ( : YML ansible?) 40 10 . .
![](https://habrastorage.org/webt/jj/7i/ln/jj7ilnbd5gav6wmc9ls3qapduwq.png)
, - . 13 2 , 70 , 2 . XP (extreme programming) .. 70 .
â -601: Ansible molecule
![](https://habrastorage.org/webt/_n/23/w3/_n23w3y7acelkugzgtt0hoqxmdc.png)
testkitchen, docker . , 20-25 7 .
![](https://habrastorage.org/webt/u5/tb/n8/u5tbn8xgqjzbnvtlqjpdsa17mmc.png)
17 45 28 2 jenkins slave.
â 167: Ansible
![](https://habrastorage.org/webt/di/zi/cz/diziczzal8qnxt6azdlyhptniho.png)
, . , . , .
![](https://habrastorage.org/webt/rp/hs/tb/rphstbmcna7nbyocuotat9hbwv0.png)
, , , jira, google docs . , , . , - , , .
:
- Eat.
- Sleep.
- Code.
- IaC test.
- Repeat
.
![](https://habrastorage.org/webt/wf/h8/tg/wfh8tgcdwlkb60nb-kfsyqjrxgy.png)
, .
â 181: Green Build Master
![](https://habrastorage.org/webt/lw/ow/xi/lwowxis0izoohgthm9db7nheih4.png)
Green Build Master. , jenkins. , :
â 193: unit
![](https://habrastorage.org/webt/eq/v-/0b/eqv-0bs92a557nhsifvggktxmps.png)
â , . , .
â 211: unit integration
![](https://habrastorage.org/webt/2c/xt/8a/2cxt8avymduqf-k8seavlobvvqa.png)
unit , . .. , , .
![](https://habrastorage.org/webt/rh/_h/0w/rh_h0wompfe15rx5arn9afnna2w.png)
jenkins , / , .
Jenkins + Docker + Ansible = Tests
![](https://habrastorage.org/webt/e6/3d/0s/e63d0sm28xvpn-hlhze5bo4c8jo.png)
- Checkout repo and generate build stages.
- Run lint playbook stages in parallel.
- Run lint role stages in parallel.
- Run syntax check role stages in parallel.
- Run test role stages in parallel.
- Lint role.
- Check dependency on other roles.
- Check syntax.
- Create docker instance
- Run molecule/default/playbook.yml.
- Check idempotency.
- Run integration tests
- Finish
â 271: Bus Factor
![](https://habrastorage.org/webt/cj/tb/wg/cjtbwgo4xb1j7umm75vvpjph8wk.png)
- . e. code review . , , , .. .
![](https://habrastorage.org/webt/mr/9n/f3/mr9nf3at9k4h1ishpfypwehqlqo.png)
. , , . jenkins + bitbucket + jira.
, -, , :
- get_url:
url: "{{ actk_certs }}/{{ item.1 }}"
dest: "{{ actk_src_tmp }}/"
username: "{{ actk_mvn_user }}"
password: "{{ actk_mvn_pass }}"
with_subelements:
- "{{ actk_cert_list }}"
- "{{ actk_certs }}"
delegate_to: localhost
- copy:
src: "{{ actk_src_tmp }}/{{ item.1 }}"
dest: "{{ actk_dst_tmp }}"
with_subelements:
- "{{ actk_cert_list }}"
- "{{ actk_certs }}"
, .
get_url:
url: "{{ actk_certs }}/{{ actk_item }}"
dest: "{{ actk_src_tmp }}/{{ actk_item }}"
username: "{{ actk_mvn_user }}"
password: "{{ actk_mvn_pass }}"
loop_control:
loop_var: actk_item
with_items: "{{ actk_cert_list }}"
delegate_to: localhost
- copy:
src: "{{ actk_src_tmp }}/{{ actk_item }}"
dest: "{{ actk_dst_tmp }}"
loop_control:
loop_var: actk_item
with_items: "{{ actk_cert_list }}"
â 311:
![](https://habrastorage.org/webt/an/sq/6h/ansq6hsvm8jzb23p9fb6aowij1y.png)
, . " , ". docker, . testinfra ansible verifier - .
![](https://habrastorage.org/webt/hl/y5/ix/hly5ixtdz684mku874jgc2hb8su.png)
:
- docker.
- , .
- - .
- .
- .
![](https://habrastorage.org/webt/0t/re/rf/0trerfuwmb5rigsb7ueykjyrbvg.png)
Pipeline jenkins
- Generate build stages.
- Lint all in parallel.
- Run test role stages in parallel.
- Finish.
Lessons learned
Avoid global variables
Ansible , workaround private_role_vars, .
. role_a
role_b
# cat role_a/defaults/main.yml
---
msg: a
# cat role_a/tasks/main.yml
---
- debug:
msg: role_a={{ msg }}
# cat role_b/defaults/main.yml
---
msg: b
# cat role_b/tasks/main.yml
---
- set_fact:
msg: b
- debug:
msg: role_b={{ msg }}
- hosts: localhost
vars:
msg: hello
roles:
- role: role_a
- role: role_b
tasks:
- debug:
msg: play={{msg}}
![](https://habrastorage.org/webt/yo/6j/mn/yo6jmnxymfxjseigrda7hcjkysk.png)
, , . Ansible , - , .
BAD: .
# cat roles/some_role/tasks/main.yml
---
debug:
var: java_home
GOOD: defaults
.
# cat roles/some_role/defaults/main.yml
---
r__java_home:
"{{ java_home | default('/path') }}"
# cat roles/some_role/tasks/main.yml
---
debug:
var: r__java_home
Prefix role variables
BAD: .
# cat roles/some_role/defaults/main.yml
---
db_port: 5432
GOOD: , inventory .
# cat roles/some_role/defaults/main.yml
---
some_role__db_port: 5432
Use loop control variable
BAD: item
, / -
---
- hosts: localhost
tasks:
- debug:
msg: "{{ item }}"
loop:
- item1
- item2
GOOD: loop_var
.
---
- hosts: localhost
tasks:
- debug:
msg: "{{ item_name }}"
loop:
- item1
- item2
loop_control:
loop_var: item_name
, , ,
GOOD: .
- name: "Verify that required string variables are defined"
assert:
that: ahs_var is defined and ahs_var | length > 0 and ahs_var != None
fail_msg: "{{ ahs_var }} needs to be set for the role to work "
success_msg: "Required variables {{ ahs_var }} is defined"
loop_control:
loop_var: ahs_var
with_items:
- ahs_item1
- ahs_item2
- ahs_item3
Avoid hashes dictionaries, use flat structure
hash/dictionary , , hash/dictionary, .
BAD: hash/dictionary.
---
user:
name: admin
group: admin
GOOD: .
---
user_name: admin
user_group: "{{ user_name }}"
Create idempotent playbooks & roles
, .. configuration drift -. molecule, .
Avoid using command shell modules
shell , , Ansible.
Test your roles via molecule
Molecule , .
Molecule Multiple instances
molecule.yml
platforms
.
---
driver:
name: docker
platforms:
- name: postgresql-instance
hostname: postgresql-instance
image: registry.example.com/postgres10:latest
pre_build_image: true
override_command: false
network_mode: host
- name: app-instance
hostname: app-instance
pre_build_image: true
image: registry.example.com/docker_centos_ansible_tests
network_mode: host
, converge.yml
:
---
- name: Converge all
hosts: all
vars:
ansible_user: root
roles:
- role: some_role
- name: Converge db
hosts: db-instance
roles:
- role: some_db_role
- name: Converge app
hosts: app-instance
roles:
- role: some_app_role
Ansible verifier
molecule ansible , , 3 . testinfra/inspec, , :
---
- name: Verify
hosts: all
tasks:
- name: copy config
copy:
src: expected_standalone.conf
dest: /root/wildfly/bin/standalone.conf
mode: "0644"
owner: root
group: root
register: config_copy_result
- name: Certify that standalone.conf changed
assert:
that: not config_copy_result.changed
, smoke test:
---
- name: Verify
hosts: solr
tasks:
- command: /blah/solr/bin/solr start -s /solr_home -p 8983 -force
- uri:
url: http://127.0.0.1:8983/solr
method: GET
status_code: 200
register: uri_result
until: uri_result is not failed
retries: 12
delay: 10
- name: Post documents to solr
command: /blah/solr/bin/post -c master /exampledocs/books.csv
Put complex logic into modules & plugins
Ansible , , , shell , . , , .
Summarize Tips & Tricks
- Avoid global variables.
- Prefix role variables.
- Use loop control variable.
- Check input variables.
- Avoid hashes dictionaries, use flat structure.
- Create idempotent playbooks & roles.
- Avoid using command shell modules.
- Test your roles via molecule.
- Put complex logic into modules & plugins.
![](https://habrastorage.org/webt/ih/ty/rh/ihtyrh6qo2oufdjxheiflkp1erg.png)
, IaC. , .
Links
UPD1 2020.05.01 20:30 â callback_whitelist = profile_tasks
. ansible. mitogen
UPD2 2020.05.03 16:34 â English version