我建议您熟悉Ivan Ponomarev对报告的解读,“ Ansible剧本是一种代码:我们进行检查,测试和持续集成。”
代码重构可能很有趣,尤其是在您的基础结构代码中。此外,Ansible角色出于某种原因往往会迅速增加复杂性。这增加了您的任务的难度。伊万将告诉您如何通过测试克服Ansible代码的复杂性。在Docker容器中。
随着代码库的增长,Ansible会遇到一些常见的问题:维护代码的复杂性,错误以及对更改的恐惧。熟悉的问题有一个熟悉的解决方案:自动化测试和CI。在报告中,Ivan将展示如何使用工具来解决Ansible代码的“易碎性”问题,执行静态分析,测试Ansible脚本以及配置CI系统以在Ansible Galaxy中发布角色。
. , . , , - .
. . . . — «», - , . , , — DigitalOcean. : Terraform + Ansible. Terraform, Ansible .
Ansible , best practices. - — , , , , . .
, . best practices, . , -, - .
, . :
- .
- , , .
- . playbook, , .
CI. ? , unit-tests , Java Python. configuration is code. ?
, , , , .
— well-formedness: . , . YAMLLint, Ansible-lint Syntax check playbook. .
YAMLLint.
- YAML. YAML , . , .
- .
- UNIX-style.
- , YAML, , .
, Ansible, , . , Windows, Windows-style , . YAMLLint .
YAMLLint. : , - , -. YAMLLint YAML- , Ansible playbooks , - , .
Ansible-lint — , good practices.
:
- command module, — shell module? . Ansible-lint .
- command module, , Ansible. Ansible-lint , Ansible.
- idempotence () command shell. idempotence - . , Ansible, best practices .
- , , Python - , , .
. «». - , . , CI-. .
.
— Syntax check, Ansible. . «», , — CI, . Syntax check Syntax check, , . - , .
CI-. Jenkins, . pipeline. , . , Ansible-.
, , . -, , . ?
. Jeff Geerling — , Ansible-. , «Ansible for DevOps». , Travis , .
, docker-. .
Molecule. , - . . Ansible-.
OpenSource-, .
Python environment. ansible, molecule , Docker, — docker-py.
, . Ansible , . , . instances (, ), . , .
, playbook. Playbook — , , instances.
.
-, . molecule, . , readme-, , Ansible-. Molecule Default. , Default , .
molecule , . Default-. , . , Molecule, . .
, : molecule test
. , Molecule . , . . , .
--debug
, , docker-py
. , docker-py
. --debug
: .
Test matrix. , Molecule . , . , syntax, converge, idempotence, instances, .
, , — , , Molecule , instances, ? — molecule.yml. Platforms instances, . , . Ansible converge , Ansible .
docker, image. , image . , systemd. - , systemd, , , docker , . , , systemd.
docker .
-, instances , , Ansible.
-, . .
Vagrant, .
Molecule, . docker — , .
: , . , .
requirements.yml, best practices Ansible. : . , dependency, .
, : YAMLLint, Ansible-lint, Syntax check.
converge instances playbook.yml. , . , - . converge playbook.yml. , - , .
- : converge , , , , — destroy=never
( « instances») docker, interactive- , .
converge. , , instances , . Molecule . , diff Ansible, Jeff Geerling : , . , , . , .
--diff
. , , , , failed idempotence.
Molecule — . , , . , , . Molecule .
:
- Testinfra (Python, default).
- Serverspec (Ruby).
- Goss (written in Go, tests in YAML).
, - , Serverspec, Molecule.
, Testinfra, Python, Molecule Ansible, .
Testinfra Molecule. .
, , ? , , - , ? shell, - , .
. , host, host «run» return code stdout stderr .
assert, , , rc=0. - , assert , : , .
molecule test, — Ansible- instances. instances, destroy=never
molecule verify
. instances.
, . 2000 JUnit, Unit-, : «keep the bar green to keep your code clean». everything is code, .
? , . curl — - , curl.
Testinfra . , . Host.process , host. - , - , , : , root , - .
. , .
. , - , , - , . -, exists , , contains , - , , OK.
- TDD, , , Ansible, Ansible.
Ansible- , Java Python, .
, , - — Testinfra, — Ansible assert. , — Ansible-. Jython — Python JVM. , jython version, , - .
. , , Molecule, Molecule , . ? Ansible-lint. Ansible-lint , Molecule .
assert Ansible.
, . , , , , , , .
. - production - , , Ansible playbooks .
, -. , , , Testinfra, . , Ansible- .
. . — Galaxy. GitHub, OpenSource, GitHub , , GitHub- CI . , Master, . - , - Master, .
? Molecule CI-. Jenkins, «». Jenkins Multibranch, checkout MyRole, , Ansible , : MyRole, . : symlink , .
Molecule .
, Jenkins. - - , , . , . , , , molecule test.
, , OpenSource, , — Travis. . services docker, , Ansible Molecule docker-py. , — , , , .
— molecule test, , , GitHub-.
, webhook, Ansible Galaxy , : build passing build failing.
Galaxy . , GitHub, Ansible Galaxy. , Travis webhook. CI , , , — . Travis.
, ? — - , - , copy-paste-modify .
. , , Galaxy, . , Molecule, , .
, , linting. — YAMLLint, Ansible-lint Syntax Check. , .
: - ? , Ansible Galaxy Molecule, , Molecule . , , , , . , , YAML well format, .
- ? ? , Molecule — . , , Molecule .
, , , , .
, , .
— Heisenbug. , .
- . , ?
- . , , - , , , URL, URL.
- , .
- . , , , , Ansible .
- . , , , instances - , , failsafe. , , instances?
: , framework, pytest. , , . , .
, ? , pytest, , — , , — . asserts : , . . : Python, values- , .
, , ? . , , , ‘port$’. «var_values», , -, Ansible playbooks, var_values , «port», , , , , .
var_values? Python . YAML-. , YAML Ansible, , .
, , , , , .
? . port_var_values, , , .
, : «keep the bar green to keep the configuration clean». , .
: , . , . , . , . , . , , .
— , «» . -, - , .
: «password», «pass», «pwd». -, , placeholder, -, - , Vault. , .
. pull request. . : k=’myskq_root_password’, v=’12345’. , password «12345», , GitHub . , pull request .
.
— Ansible.
- -, : YAMLLint + Ansible-lint + Syntax check — . - Ansible-, - , , , , . , . Ansible-lint , , best practices .
- Molecule.
- . playbook failed fast. , , - , .
- .
- - — Molecule. :
pip install molecule
, molecule init
, molecule test
. - ? converge idempotence.
- converge idempotence? .
, : , , . — ? , Git, , pipeline, quality gates, .
, , — GitHub + Travis + Galaxy. OpenSource. , OpenSource. Jenkins Multibranch .
, , , .
: ! . : , , , ? , 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 .