Le sugiero que lea la transcripción del informe de Ivan Ponomarev, "Los playbooks de Ansible son un código: verificamos, probamos, nos integramos continuamente".
La refactorización de código puede ser divertida, especialmente si es su código de infraestructura. Además, los roles de Ansible por alguna razón tienden a aumentar rápidamente la complejidad. Y esto agrega un giro a su tarea. Ivan le dirá cómo superar la complejidad del código Ansible a través de las pruebas. En contenedores Docker.
A medida que crece la base del código, Ansible viene con problemas familiares: la complejidad de mantener el código, los errores y el miedo al cambio. Los problemas familiares tienen una solución familiar: pruebas automatizadas y CI. En el informe, Ivan mostrará cómo usar las herramientas para resolver los problemas de "fragilidad" del código Ansible, realizar análisis estáticos, probar scripts Ansible y configurar sistemas CI para publicar roles en Ansible Galaxy.
. , . , , - .
![](https://habrastorage.org/webt/nf/ta/07/nfta07piewosflcsbxpy5kor--u.png)
. . . . — «», - , . , , — DigitalOcean. : Terraform + Ansible. Terraform, Ansible .
![](https://habrastorage.org/webt/s-/qw/d7/s-qwd7u14wwm_i79vtj3ifdr3uw.png)
Ansible , best practices. - — , , , , . .
![](https://habrastorage.org/webt/eg/ar/gh/egarghk4xofk4ikuz5j1jeakse8.png)
, . best practices, . , -, - .
![](https://habrastorage.org/webt/nj/uh/ag/njuhagvg09hsrbwnqqk5jmgc23y.png)
, . :
- .
- , , .
- . playbook, , .
![](https://habrastorage.org/webt/i-/pb/-i/i-pb-iobvlx80mqokwtgj6ytuu0.png)
CI. ? , unit-tests , Java Python. configuration is code. ?
![](https://habrastorage.org/webt/bp/m5/hy/bpm5hydmqirbdjn2uvxrlhxmhmi.png)
, , , , .
— well-formedness: . , . YAMLLint, Ansible-lint Syntax check playbook. .
![](https://habrastorage.org/webt/rg/cj/hp/rgcjhpmrkjjubwqyqv9xvoszxi0.png)
YAMLLint.
- YAML. YAML , . , .
- .
- UNIX-style.
- , YAML, , .
, Ansible, , . , Windows, Windows-style , . YAMLLint .
![](https://habrastorage.org/webt/g-/av/n5/g-avn5ij32qy-p-ethg2ii0momw.png)
YAMLLint. : , - , -. YAMLLint YAML- , Ansible playbooks , - , .
![](https://habrastorage.org/webt/tt/0h/fy/tt0hfyelhpl2ce_yz-gv06d6zcs.png)
Ansible-lint — , good practices.
:
- command module, — shell module? . Ansible-lint .
- command module, , Ansible. Ansible-lint , Ansible.
- idempotence () command shell. idempotence - . , Ansible, best practices .
- , , Python - , , .
![](https://habrastorage.org/webt/mg/fm/vz/mgfmvzpu54jsgq3yughva-ie4eg.png)
. «». - , . , CI-. .
![](https://habrastorage.org/webt/u_/cv/xz/u_cvxz-8vy7zjmtahzf8garcof0.png)
.
![](https://habrastorage.org/webt/pi/uu/bd/piuubdpass_jwhus5pbwkwppqqc.png)
— Syntax check, Ansible. . «», , — CI, . Syntax check Syntax check, , . - , .
![](https://habrastorage.org/webt/e-/-r/8s/e--r8s3rjxdbcjdubnnrxdv3kak.png)
CI-. Jenkins, . pipeline. , . , Ansible-.
![](https://habrastorage.org/webt/8u/xb/vu/8uxbvuxkjrr6dnkrddxsv4nqy7m.png)
, , . -, , . ?
![](https://habrastorage.org/webt/qw/rr/mg/qwrrmgn-vgntjqmu-xsm3fszvpq.png)
. Jeff Geerling — , Ansible-. , «Ansible for DevOps». , Travis , .
![](https://habrastorage.org/webt/my/md/z_/mymdz_tcpxpr0oqq2dju3q6x8-8.png)
, docker-. .
![](https://habrastorage.org/webt/jv/f0/ag/jvf0agrgtaoon9n2frjkp79pdki.png)
Molecule. , - . . Ansible-.
![](https://habrastorage.org/webt/ko/-u/hl/ko-uhlyjoskb4ecbx1tnjhhyq24.png)
OpenSource-, .
![](https://habrastorage.org/webt/ov/du/qs/ovduqsqkvbgjkinm_d_txkvfnju.png)
Python environment. ansible, molecule , Docker, — docker-py.
![](https://habrastorage.org/webt/wf/ed/6g/wfed6gpup-uhfxzypzjyljzrmje.png)
, . Ansible , . , . instances (, ), . , .
, playbook. Playbook — , , instances.
.
![](https://habrastorage.org/webt/sq/i9/3v/sqi93vsxqfvtrydvzim_add-j_i.png)
-, . molecule, . , readme-, , Ansible-. Molecule Default. , Default , .
![](https://habrastorage.org/webt/gq/-a/at/gq-aat0xi_za7ivwowhvwyzrz-w.png)
molecule , . Default-. , . , Molecule, . .
![](https://habrastorage.org/webt/d_/lu/6s/d_lu6sbyrw1jzgmhmwmnyzhm0pu.png)
, : molecule test
. , Molecule . , . . , .
![](https://habrastorage.org/webt/72/8d/im/728dimppa5gdm8wduhgxkdf50kq.png)
--debug
, , docker-py
. , docker-py
. --debug
: .
![](https://habrastorage.org/webt/pn/yi/xm/pnyixm6epqfwgxsocthjpo7mueq.png)
Test matrix. , Molecule . , . , syntax, converge, idempotence, instances, .
![](https://habrastorage.org/webt/wb/3w/yn/wb3wynbjsnbxjez2gx175oteaam.png)
, , — , , Molecule , instances, ? — molecule.yml. Platforms instances, . , . Ansible converge , Ansible .
docker, image. , image . , systemd. - , systemd, , , docker , . , , systemd.
![](https://habrastorage.org/webt/dq/s-/r4/dqs-r4l6l85jvy9lisxd2wfaj64.png)
docker .
-, instances , , Ansible.
-, . .
Vagrant, .
Molecule, . docker — , .
![](https://habrastorage.org/webt/zl/up/va/zlupvavg_cyxfje1hhkmayrzhle.png)
: , . , .
requirements.yml, best practices Ansible. : . , dependency, .
![](https://habrastorage.org/webt/wr/ka/hb/wrkahbv7gzh_0rsbqke7goeiyvq.png)
, : YAMLLint, Ansible-lint, Syntax check.
![](https://habrastorage.org/webt/yk/wi/q7/ykwiq7vk_vt8wdp52zrktdtk1og.png)
converge instances playbook.yml. , . , - . converge playbook.yml. , - , .
![](https://habrastorage.org/webt/0b/gm/ad/0bgmadx7ytt05b623n_ol78joq0.png)
- : converge , , , , — destroy=never
( « instances») docker, interactive- , .
![](https://habrastorage.org/webt/d4/lf/ok/d4lfokwbrnbw0din9z9zztsw-ou.png)
converge. , , instances , . Molecule . , diff Ansible, Jeff Geerling : , . , , . , .
![](https://habrastorage.org/webt/9t/i7/dp/9ti7dpbx3vyhq0we3_zlw1qvp8i.png)
--diff
. , , , , failed idempotence.
![](https://habrastorage.org/webt/6n/xb/zn/6nxbzn_vxumz-n2g6ovdgcff3lq.png)
Molecule — . , , . , , . Molecule .
:
- Testinfra (Python, default).
- Serverspec (Ruby).
- Goss (written in Go, tests in YAML).
, - , Serverspec, Molecule.
, Testinfra, Python, Molecule Ansible, .
![](https://habrastorage.org/webt/wf/do/98/wfdo98snolnlm4h1g1gxyhkojvg.png)
Testinfra Molecule. .
![](https://habrastorage.org/webt/1o/eg/rs/1oegrsh2wq8ywmy6vvecgdjocx4.png)
, , ? , , - , ? shell, - , .
. , host, host «run» return code stdout stderr .
![](https://habrastorage.org/webt/zh/qs/u0/zhqsu0kpfsgqfbauu8hn7otzt5q.png)
assert, , , rc=0. - , assert , : , .
![](https://habrastorage.org/webt/jv/p9/vw/jvp9vwpal0og_tc9bqcakc7af8u.png)
molecule test, — Ansible- instances. instances, destroy=never
molecule verify
. instances.
![](https://habrastorage.org/webt/yc/ds/gl/ycdsglouflle8lxkplabyh7c3a0.png)
, . 2000 JUnit, Unit-, : «keep the bar green to keep your code clean». everything is code, .
![](https://habrastorage.org/webt/q_/fa/3v/q_fa3vjdcg7fcvxbpwfct_vafxw.png)
? , . curl — - , curl.
![](https://habrastorage.org/webt/dc/by/-g/dcby-g5pxpfyolbid1wbfezlhhs.png)
Testinfra . , . Host.process , host. - , - , , : , root , - .
![](https://habrastorage.org/webt/9p/vb/bt/9pvbbtal3_h6lhus3xpyjvr6o64.png)
. , .
![](https://habrastorage.org/webt/ah/2f/nt/ah2fntrmiqiybu-8v864v7bmkqm.png)
. , - , , - , . -, exists , , contains , - , , OK.
![](https://habrastorage.org/webt/0i/cd/2w/0icd2wxc0aypghabejearlmzxss.png)
- TDD, , , Ansible, Ansible.
Ansible- , Java Python, .
, , - — Testinfra, — Ansible assert. , — Ansible-. Jython — Python JVM. , jython version, , - .
![](https://habrastorage.org/webt/9g/ml/uq/9gmluqfpql9ot8rmqmjavikpkzw.png)
. , , Molecule, Molecule , . ? Ansible-lint. Ansible-lint , Molecule .
assert Ansible.
![](https://habrastorage.org/webt/cw/2_/oy/cw2_oy1vknpgl_x11srbuegvihy.png)
, . , , , , , , .
. - production - , , Ansible playbooks .
![](https://habrastorage.org/webt/kn/de/yd/kndeydeuaimjnu2bmgs2ekqadh4.png)
, -. , , , Testinfra, . , Ansible- .
![](https://habrastorage.org/webt/ng/yr/tr/ngyrtrhqjvhroywdhjzgj2ielwg.png)
. . — Galaxy. GitHub, OpenSource, GitHub , , GitHub- CI . , Master, . - , - Master, .
![](https://habrastorage.org/webt/nn/5j/re/nn5jrelzhhbbnd6xlwpgkgrhe7o.png)
? Molecule CI-. Jenkins, «». Jenkins Multibranch, checkout MyRole, , Ansible , : MyRole, . : symlink , .
![](https://habrastorage.org/webt/hl/uj/ao/hlujaokyhvxxozxviyxxvs25gnm.png)
Molecule .
![](https://habrastorage.org/webt/fj/e2/bx/fje2bx3wnisexoe7oos43rn5peu.png)
, Jenkins. - - , , . , . , , , molecule test.
![](https://habrastorage.org/webt/__/fw/0q/__fw0qet_ehwql4ousfupgkc3xc.png)
, , OpenSource, , — Travis. . services docker, , Ansible Molecule docker-py. , — , , , .
— molecule test, , , GitHub-.
![](https://habrastorage.org/webt/ec/of/xj/ecofxj1thtspbigbxnxydwf8eri.png)
, webhook, Ansible Galaxy , : build passing build failing.
Galaxy . , GitHub, Ansible Galaxy. , Travis webhook. CI , , , — . Travis.
![](https://habrastorage.org/webt/dl/hv/ar/dlhvarqg_cksv7oepmhakepmt_i.png)
, ? — - , - , copy-paste-modify .
![](https://habrastorage.org/webt/mp/0v/zi/mp0vzik3lii8bjig_0mfxolcoqq.png)
. , , Galaxy, . , Molecule, , .
, , linting. — YAMLLint, Ansible-lint Syntax Check. , .
![](https://habrastorage.org/webt/v1/qu/ss/v1qussep3_c0mdbhxdnm66eoiua.png)
: - ? , Ansible Galaxy Molecule, , Molecule . , , , , . , , YAML well format, .
![](https://habrastorage.org/webt/bg/p3/t_/bgp3t_on2vb9p2k0poevu5txkxk.png)
- ? ? , Molecule — . , , Molecule .
, , , , .
![](https://habrastorage.org/webt/yd/4p/v_/yd4pv_xcw3a7po3qyhzpyk-x1bw.png)
, , .
— Heisenbug. , .
![](https://habrastorage.org/webt/a1/rx/g6/a1rxg6i44u5ybwotvbhuo7xhtxg.png)
- . , ?
- . , , - , , , URL, URL.
- , .
- . , , , , Ansible .
- . , , , instances - , , failsafe. , , instances?
: , framework, pytest. , , . , .
![](https://habrastorage.org/webt/f7/wf/ir/f7wfirmtx2m0avb6pbkyhhhmfzy.png)
, ? , pytest, , — , , — . asserts : , . . : Python, values- , .
![](https://habrastorage.org/webt/av/e8/ic/ave8icjotwx98amrm-hemktlj3m.png)
, , ? . , , , ‘port$’. «var_values», , -, Ansible playbooks, var_values , «port», , , , , .
var_values? Python . YAML-. , YAML Ansible, , .
![](https://habrastorage.org/webt/0c/ge/8g/0cge8g9b5rcd3y7zjtcurvhkek4.png)
, , , , , .
? . port_var_values, , , .
![](https://habrastorage.org/webt/nd/k_/53/ndk_53txd6_wytpacr-8bfacv80.png)
, : «keep the bar green to keep the configuration clean». , .
: , . , . , . , . , . , , .
![](https://habrastorage.org/webt/hk/ch/uh/hkchuh90w1l651tffrcu2e2hc68.png)
— , «» . -, - , .
: «password», «pass», «pwd». -, , placeholder, -, - , Vault. , .
![](https://habrastorage.org/webt/r_/ko/vx/r_kovxpxhggjar5zgdfkns8jp0s.png)
. pull request. . : k=’myskq_root_password’, v=’12345’. , password «12345», , GitHub . , pull request .
.
![](https://habrastorage.org/webt/6f/vo/gm/6fvogmb9p-lj1lzce90bisk3mrs.png)
— Ansible.
- -, : YAMLLint + Ansible-lint + Syntax check — . - Ansible-, - , , , , . , . Ansible-lint , , best practices .
- Molecule.
- . playbook failed fast. , , - , .
- .
![](https://habrastorage.org/webt/ns/lr/9r/nslr9ruw6lznmu6apuua3dti97w.png)
- - — Molecule. :
pip install molecule
, molecule init
, molecule test
. - ? converge idempotence.
- converge idempotence? .
![](https://habrastorage.org/webt/tj/wn/8k/tjwn8klvrwdzl9r8fddpwnwagre.png)
, : , , . — ? , Git, , pipeline, quality gates, .
![](https://habrastorage.org/webt/ib/bi/bz/ibbibzu1dccagtq8w_fslbqpo14.png)
, , — GitHub + Travis + Galaxy. OpenSource. , OpenSource. Jenkins Multibranch .
![](https://habrastorage.org/webt/kl/ay/5n/klay5nrakvoxev7akq9cefxbdzu.png)
, , , .
: ! . : , , , ? , Ansible? Ansible , Python, Testinfra, Molecule. , Python, , .
: ? , Java. pull request — : . , — - , . , - , , , . . playbooks . , . - , ? . , - , . - shell, shell - . , .
: Ansible Windows? Ansible playbook Windows. ? , ?
: , Ansible - Windows Testinfra. Windows, Ansible cygwin . Linux- , . , . .
: , . — . , ? . changed_when, ?
: , -, - , , ?
: , , , . , .
: , asserts, . . . command shell, , . , , . — , — , . , . Ansible . , , , . .
: . , Ansible gathering facts study, , …
: .
: , , JUnit , — , , gathering tasks, , , ?
: . , . -, -. -, ? , -. , , , , , - . - . , : , , ? .
: , , GitHub , . , version. , .
: . . Molecule , .
: . , ? , - , - , , , .
: . , service is running. , «»?
: , — , . , , …
: , , , -, - . , «200» , - . . selenium- . , Ansible : « ». , , «200» , , , pipeline.
: production Ansible Galaxy ? .
: , . , nginx, PostgreSQL. . Oracle Java Ansible Galaxy. . . , , . , Ansible build pipeline. Molecule, Travis . , , Jeff Geerling.
: playbook Ansible Galaxy?
: , , . . , , , . , 150000 , , 150 . 150000 , , , - production.
: Jinja. , Jinja , hosts. ?
: , Molecule , Molecule , . inventory. Molecule , , . - … Molecule? Molecule - , playbook.yml. playbook.yml, , , playbook. - Jinja, — . , . Molecule .