Los playbooks de Ansible son un código: verificamos, probamos, integramos continuamente. Ivan Ponomarev

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.



. , . , , - .



. . . . — «», - , . , , — DigitalOcean. : Terraform + Ansible. Terraform, Ansible .



Ansible , best practices. - — , , , , . .



, . best practices, . , -, - .



, . :


  1. .
    • , .
    • . : , ? ?
  2. , , .
  3. . 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 .


All Articles