Probing Ansible Bonds with Molecule Tests“DevOps”,… I Github/Twitter/IRC: Fobhep,...
Transcript of Probing Ansible Bonds with Molecule Tests“DevOps”,… I Github/Twitter/IRC: Fobhep,...
![Page 1: Probing Ansible Bonds with Molecule Tests“DevOps”,… I Github/Twitter/IRC: Fobhep, hopfenmueller@atix.de #atix#cfgmgmtcamp2020 whoarewe I MatthiasDellweg I Physicist I SeniorSoftware](https://reader035.fdocuments.us/reader035/viewer/2022071008/5fc67c54bb3052547c281929/html5/thumbnails/1.jpg)
![Page 2: Probing Ansible Bonds with Molecule Tests“DevOps”,… I Github/Twitter/IRC: Fobhep, hopfenmueller@atix.de #atix#cfgmgmtcamp2020 whoarewe I MatthiasDellweg I Physicist I SeniorSoftware](https://reader035.fdocuments.us/reader035/viewer/2022071008/5fc67c54bb3052547c281929/html5/thumbnails/2.jpg)
Probing Ansible Bonds withMolecule Tests
Matthias Dellweg & Bernhard Hopfenmüller
4. Februar 2020
#atix #cfgmgmtcamp2020
![Page 3: Probing Ansible Bonds with Molecule Tests“DevOps”,… I Github/Twitter/IRC: Fobhep, hopfenmueller@atix.de #atix#cfgmgmtcamp2020 whoarewe I MatthiasDellweg I Physicist I SeniorSoftware](https://reader035.fdocuments.us/reader035/viewer/2022071008/5fc67c54bb3052547c281929/html5/thumbnails/3.jpg)
whoarewe
I Matthias Dellweg
I Physicist
I Senior SoftwareEngineer
I Foreman, Pulp, Ansible(FAM), …
I Github: mdellweg,[email protected],IRC: x9c4
I Bernhard Hopfenmüller
I Physicist
I Senior IT Consultant
I Ansible (FAM), Kafka,“DevOps”, …
I Github/Twitter/IRC:Fobhep,[email protected]
#atix #cfgmgmtcamp2020
![Page 4: Probing Ansible Bonds with Molecule Tests“DevOps”,… I Github/Twitter/IRC: Fobhep, hopfenmueller@atix.de #atix#cfgmgmtcamp2020 whoarewe I MatthiasDellweg I Physicist I SeniorSoftware](https://reader035.fdocuments.us/reader035/viewer/2022071008/5fc67c54bb3052547c281929/html5/thumbnails/4.jpg)
whoarewe
I Matthias Dellweg
I Physicist
I Senior SoftwareEngineer
I Foreman, Pulp, Ansible(FAM), …
I Github: mdellweg,[email protected],IRC: x9c4
I Bernhard Hopfenmüller
I Physicist
I Senior IT Consultant
I Ansible (FAM), Kafka,“DevOps”, …
I Github/Twitter/IRC:Fobhep,[email protected]
#atix #cfgmgmtcamp2020
![Page 5: Probing Ansible Bonds with Molecule Tests“DevOps”,… I Github/Twitter/IRC: Fobhep, hopfenmueller@atix.de #atix#cfgmgmtcamp2020 whoarewe I MatthiasDellweg I Physicist I SeniorSoftware](https://reader035.fdocuments.us/reader035/viewer/2022071008/5fc67c54bb3052547c281929/html5/thumbnails/5.jpg)
#atix #cfgmgmtcamp2020
![Page 6: Probing Ansible Bonds with Molecule Tests“DevOps”,… I Github/Twitter/IRC: Fobhep, hopfenmueller@atix.de #atix#cfgmgmtcamp2020 whoarewe I MatthiasDellweg I Physicist I SeniorSoftware](https://reader035.fdocuments.us/reader035/viewer/2022071008/5fc67c54bb3052547c281929/html5/thumbnails/6.jpg)
Test Everything
Chemical Substance
I Does my chemical plant work as specified?
I Can I optimize without breaking?
I Can I produce additional substances in the same plant?
Ansible role
I Does my system work as specified?
I Can I refactor without breaking?
I Can I develop further features with backwards compatibility?
#atix #cfgmgmtcamp2020
![Page 7: Probing Ansible Bonds with Molecule Tests“DevOps”,… I Github/Twitter/IRC: Fobhep, hopfenmueller@atix.de #atix#cfgmgmtcamp2020 whoarewe I MatthiasDellweg I Physicist I SeniorSoftware](https://reader035.fdocuments.us/reader035/viewer/2022071008/5fc67c54bb3052547c281929/html5/thumbnails/7.jpg)
Test Everything
Chemical Substance
I Does my chemical plant work as specified?
I Can I optimize without breaking?
I Can I produce additional substances in the same plant?
Ansible role
I Does my system work as specified?
I Can I refactor without breaking?
I Can I develop further features with backwards compatibility?
#atix #cfgmgmtcamp2020
![Page 8: Probing Ansible Bonds with Molecule Tests“DevOps”,… I Github/Twitter/IRC: Fobhep, hopfenmueller@atix.de #atix#cfgmgmtcamp2020 whoarewe I MatthiasDellweg I Physicist I SeniorSoftware](https://reader035.fdocuments.us/reader035/viewer/2022071008/5fc67c54bb3052547c281929/html5/thumbnails/8.jpg)
Test Everything
Chemical Substance
I Does my chemical plant work as specified?
I Can I optimize without breaking?
I Can I produce additional substances in the same plant?
Ansible role
I Does my system work as specified?
I Can I refactor without breaking?
I Can I develop further features with backwards compatibility?
#atix #cfgmgmtcamp2020
![Page 9: Probing Ansible Bonds with Molecule Tests“DevOps”,… I Github/Twitter/IRC: Fobhep, hopfenmueller@atix.de #atix#cfgmgmtcamp2020 whoarewe I MatthiasDellweg I Physicist I SeniorSoftware](https://reader035.fdocuments.us/reader035/viewer/2022071008/5fc67c54bb3052547c281929/html5/thumbnails/9.jpg)
Test Everything
Chemical Substance
I Does my chemical plant work as specified?
I Can I optimize without breaking?
I Can I produce additional substances in the same plant?
Ansible role
I Does my system work as specified?
I Can I refactor without breaking?
I Can I develop further features with backwards compatibility?
#atix #cfgmgmtcamp2020
![Page 10: Probing Ansible Bonds with Molecule Tests“DevOps”,… I Github/Twitter/IRC: Fobhep, hopfenmueller@atix.de #atix#cfgmgmtcamp2020 whoarewe I MatthiasDellweg I Physicist I SeniorSoftware](https://reader035.fdocuments.us/reader035/viewer/2022071008/5fc67c54bb3052547c281929/html5/thumbnails/10.jpg)
Building Blocks
Modules are already tested. Why should i test roles?
#atix #cfgmgmtcamp2020
![Page 11: Probing Ansible Bonds with Molecule Tests“DevOps”,… I Github/Twitter/IRC: Fobhep, hopfenmueller@atix.de #atix#cfgmgmtcamp2020 whoarewe I MatthiasDellweg I Physicist I SeniorSoftware](https://reader035.fdocuments.us/reader035/viewer/2022071008/5fc67c54bb3052547c281929/html5/thumbnails/11.jpg)
More than just a Bunch of Atoms
6=ordering matters
#atix #cfgmgmtcamp2020
![Page 12: Probing Ansible Bonds with Molecule Tests“DevOps”,… I Github/Twitter/IRC: Fobhep, hopfenmueller@atix.de #atix#cfgmgmtcamp2020 whoarewe I MatthiasDellweg I Physicist I SeniorSoftware](https://reader035.fdocuments.us/reader035/viewer/2022071008/5fc67c54bb3052547c281929/html5/thumbnails/12.jpg)
More than just a Bunch of Atoms
�mind the additional side effects!
#atix #cfgmgmtcamp2020
![Page 13: Probing Ansible Bonds with Molecule Tests“DevOps”,… I Github/Twitter/IRC: Fobhep, hopfenmueller@atix.de #atix#cfgmgmtcamp2020 whoarewe I MatthiasDellweg I Physicist I SeniorSoftware](https://reader035.fdocuments.us/reader035/viewer/2022071008/5fc67c54bb3052547c281929/html5/thumbnails/13.jpg)
A Role
nginx_sysinfodefaults
main.yamlfiles
stylsheet.csshandlers
main.yamltasks
main.yamltemplates
index.html.j2vars
main.yaml
#atix #cfgmgmtcamp2020
![Page 14: Probing Ansible Bonds with Molecule Tests“DevOps”,… I Github/Twitter/IRC: Fobhep, hopfenmueller@atix.de #atix#cfgmgmtcamp2020 whoarewe I MatthiasDellweg I Physicist I SeniorSoftware](https://reader035.fdocuments.us/reader035/viewer/2022071008/5fc67c54bb3052547c281929/html5/thumbnails/14.jpg)
Bootstrap
$mkvirtualenv --python=python3.6 molecule(molecule)$ pip install molecule(molecule)$ molecule init --scenario
#atix #cfgmgmtcamp2020
![Page 15: Probing Ansible Bonds with Molecule Tests“DevOps”,… I Github/Twitter/IRC: Fobhep, hopfenmueller@atix.de #atix#cfgmgmtcamp2020 whoarewe I MatthiasDellweg I Physicist I SeniorSoftware](https://reader035.fdocuments.us/reader035/viewer/2022071008/5fc67c54bb3052547c281929/html5/thumbnails/15.jpg)
A Role
nginx_sysinfo[...].yamllintmolecule
defaultDockerfile.j2INSTALL.rstmolecule.ymlplaybook.ymltests
test_default.py
#atix #cfgmgmtcamp2020
![Page 16: Probing Ansible Bonds with Molecule Tests“DevOps”,… I Github/Twitter/IRC: Fobhep, hopfenmueller@atix.de #atix#cfgmgmtcamp2020 whoarewe I MatthiasDellweg I Physicist I SeniorSoftware](https://reader035.fdocuments.us/reader035/viewer/2022071008/5fc67c54bb3052547c281929/html5/thumbnails/16.jpg)
Molecule Config File
# config file for tests# molecule/default/molecule.ymldriver:name: docker
platforms:- name: centos7image: centos:7
- name: debian10image: debian:10
#atix #cfgmgmtcamp2020
![Page 17: Probing Ansible Bonds with Molecule Tests“DevOps”,… I Github/Twitter/IRC: Fobhep, hopfenmueller@atix.de #atix#cfgmgmtcamp2020 whoarewe I MatthiasDellweg I Physicist I SeniorSoftware](https://reader035.fdocuments.us/reader035/viewer/2022071008/5fc67c54bb3052547c281929/html5/thumbnails/17.jpg)
Test Matrix
(molecule)$ molecule matrix [-s default] testdefault
lintdependencydestroycreateprepareconvergeidempotenceverifydestroy
#atix #cfgmgmtcamp2020
![Page 18: Probing Ansible Bonds with Molecule Tests“DevOps”,… I Github/Twitter/IRC: Fobhep, hopfenmueller@atix.de #atix#cfgmgmtcamp2020 whoarewe I MatthiasDellweg I Physicist I SeniorSoftware](https://reader035.fdocuments.us/reader035/viewer/2022071008/5fc67c54bb3052547c281929/html5/thumbnails/18.jpg)
Linting
---# molecule/default/molecule.ymldriver:name: docker
lint:name: yamllint
platforms:- name: centos7image: centos:7
- name: debian10image: debian:10
#atix #cfgmgmtcamp2020
![Page 19: Probing Ansible Bonds with Molecule Tests“DevOps”,… I Github/Twitter/IRC: Fobhep, hopfenmueller@atix.de #atix#cfgmgmtcamp2020 whoarewe I MatthiasDellweg I Physicist I SeniorSoftware](https://reader035.fdocuments.us/reader035/viewer/2022071008/5fc67c54bb3052547c281929/html5/thumbnails/19.jpg)
yaml 6= yaml
# some yaml file of the role# we want this to failfoo: barfoo2: barfoo3:- bar1- bar2foo4:- de- no
foo5: Falsefoo6: Yes
#atix #cfgmgmtcamp2020
![Page 20: Probing Ansible Bonds with Molecule Tests“DevOps”,… I Github/Twitter/IRC: Fobhep, hopfenmueller@atix.de #atix#cfgmgmtcamp2020 whoarewe I MatthiasDellweg I Physicist I SeniorSoftware](https://reader035.fdocuments.us/reader035/viewer/2022071008/5fc67c54bb3052547c281929/html5/thumbnails/20.jpg)
yaml 6= yaml
--- # this looks betterfoo: barfoo2: barfoo3:- bar1- bar2
foo4:- de- no
foo5: falsefoo6: true
#atix #cfgmgmtcamp2020
![Page 21: Probing Ansible Bonds with Molecule Tests“DevOps”,… I Github/Twitter/IRC: Fobhep, hopfenmueller@atix.de #atix#cfgmgmtcamp2020 whoarewe I MatthiasDellweg I Physicist I SeniorSoftware](https://reader035.fdocuments.us/reader035/viewer/2022071008/5fc67c54bb3052547c281929/html5/thumbnails/21.jpg)
Linting Taste
edited .yamllint in role dir (added my molecule)
rules:document-start: enableindentation: {spaces: 2, indent-sequences: consistent}truthy: enableignore: |.gitlab-ci.yml
#atix #cfgmgmtcamp2020
![Page 22: Probing Ansible Bonds with Molecule Tests“DevOps”,… I Github/Twitter/IRC: Fobhep, hopfenmueller@atix.de #atix#cfgmgmtcamp2020 whoarewe I MatthiasDellweg I Physicist I SeniorSoftware](https://reader035.fdocuments.us/reader035/viewer/2022071008/5fc67c54bb3052547c281929/html5/thumbnails/22.jpg)
Test Matrix
(molecule)$ molecule matrix [-s default] testdefault
lint # Xdependencydestroycreateprepareconvergeidempotenceverifydestroy
#atix #cfgmgmtcamp2020
![Page 23: Probing Ansible Bonds with Molecule Tests“DevOps”,… I Github/Twitter/IRC: Fobhep, hopfenmueller@atix.de #atix#cfgmgmtcamp2020 whoarewe I MatthiasDellweg I Physicist I SeniorSoftware](https://reader035.fdocuments.us/reader035/viewer/2022071008/5fc67c54bb3052547c281929/html5/thumbnails/23.jpg)
Does my Role have Dependencies?
---# molecule/default/molecule.ymldependency:name: galaxy
driver:name: docker
lint:name: yamllint
platforms:- name: centos7image: centos:7
- name: debian10image: debian:10
#atix #cfgmgmtcamp2020
![Page 24: Probing Ansible Bonds with Molecule Tests“DevOps”,… I Github/Twitter/IRC: Fobhep, hopfenmueller@atix.de #atix#cfgmgmtcamp2020 whoarewe I MatthiasDellweg I Physicist I SeniorSoftware](https://reader035.fdocuments.us/reader035/viewer/2022071008/5fc67c54bb3052547c281929/html5/thumbnails/24.jpg)
Requirements File
---# molecule/default/requirements.yml- src: geerlingguy.*
#atix #cfgmgmtcamp2020
![Page 25: Probing Ansible Bonds with Molecule Tests“DevOps”,… I Github/Twitter/IRC: Fobhep, hopfenmueller@atix.de #atix#cfgmgmtcamp2020 whoarewe I MatthiasDellweg I Physicist I SeniorSoftware](https://reader035.fdocuments.us/reader035/viewer/2022071008/5fc67c54bb3052547c281929/html5/thumbnails/25.jpg)
Get Dependencies
(molecule)$ molecule dependency--> Test matrix
defaultdependency
--> Scenario: 'default'--> Action: 'dependency'
- changing role geerlingguy.ntp from 1.6.4 to unspecified- downloading role 'ntp', owned by geerlingguy- downloading role from https://github.com/geerlingguy/[...]- extracting geerlingguy.ntp [...]- geerlingguy.ntp (1.6.4) was installed successfully
Dependency completed successfully.
#atix #cfgmgmtcamp2020
![Page 26: Probing Ansible Bonds with Molecule Tests“DevOps”,… I Github/Twitter/IRC: Fobhep, hopfenmueller@atix.de #atix#cfgmgmtcamp2020 whoarewe I MatthiasDellweg I Physicist I SeniorSoftware](https://reader035.fdocuments.us/reader035/viewer/2022071008/5fc67c54bb3052547c281929/html5/thumbnails/26.jpg)
Alternatives to Galaxy?
I gilt - A GIT layering tool.
---dependency:
name: gilt---# gilt config file- git: https://github.com/blueboxgroup/ursula.git
version: masterfiles:
- src: roles/loggingdst: roles/blueboxgroup.logging/
I shell - An Unix tool
---dependency:
name: shellcommand: curl | bash
#atix #cfgmgmtcamp2020
![Page 27: Probing Ansible Bonds with Molecule Tests“DevOps”,… I Github/Twitter/IRC: Fobhep, hopfenmueller@atix.de #atix#cfgmgmtcamp2020 whoarewe I MatthiasDellweg I Physicist I SeniorSoftware](https://reader035.fdocuments.us/reader035/viewer/2022071008/5fc67c54bb3052547c281929/html5/thumbnails/27.jpg)
Use Dependencies?
Patience please
#atix #cfgmgmtcamp2020
![Page 28: Probing Ansible Bonds with Molecule Tests“DevOps”,… I Github/Twitter/IRC: Fobhep, hopfenmueller@atix.de #atix#cfgmgmtcamp2020 whoarewe I MatthiasDellweg I Physicist I SeniorSoftware](https://reader035.fdocuments.us/reader035/viewer/2022071008/5fc67c54bb3052547c281929/html5/thumbnails/28.jpg)
Test Matrix
(molecule)$ matrix [-s default] testdefault
lint # Xdependency # Xdestroycreateprepareconvergeidempotenceverifydestroy
#atix #cfgmgmtcamp2020
![Page 29: Probing Ansible Bonds with Molecule Tests“DevOps”,… I Github/Twitter/IRC: Fobhep, hopfenmueller@atix.de #atix#cfgmgmtcamp2020 whoarewe I MatthiasDellweg I Physicist I SeniorSoftware](https://reader035.fdocuments.us/reader035/viewer/2022071008/5fc67c54bb3052547c281929/html5/thumbnails/29.jpg)
Create, Converge, Destroy, Prepare
The Ansible provisioner
---# molecule/default/molecule.yml[...]provisioner:
name: ansiblelint:
name: ansible-lintoptions:
vvv: trueplaybooks:
create: create.ymlconverge: playbook.ymldestroy: destroy.ymlprepare: prepare.yml
#atix #cfgmgmtcamp2020
![Page 30: Probing Ansible Bonds with Molecule Tests“DevOps”,… I Github/Twitter/IRC: Fobhep, hopfenmueller@atix.de #atix#cfgmgmtcamp2020 whoarewe I MatthiasDellweg I Physicist I SeniorSoftware](https://reader035.fdocuments.us/reader035/viewer/2022071008/5fc67c54bb3052547c281929/html5/thumbnails/30.jpg)
Create Matrix
Prepare infrastructure to run role
(molecule)$ molecule matrix [-s default] create--> Test matrix
defaultdependencycreateprepare
#atix #cfgmgmtcamp2020
![Page 31: Probing Ansible Bonds with Molecule Tests“DevOps”,… I Github/Twitter/IRC: Fobhep, hopfenmueller@atix.de #atix#cfgmgmtcamp2020 whoarewe I MatthiasDellweg I Physicist I SeniorSoftware](https://reader035.fdocuments.us/reader035/viewer/2022071008/5fc67c54bb3052547c281929/html5/thumbnails/31.jpg)
Steps for Creating
I Consider adjusting Dockerfile.j2
I Write prepare playbook
---- hosts: all#roles:# - geerlingguy.ntptasks:- name: Install curlbecome: truepackage:name: curl
I Done
Docker/Vagrant: create, destroy, prepare are bundled
#atix #cfgmgmtcamp2020
![Page 32: Probing Ansible Bonds with Molecule Tests“DevOps”,… I Github/Twitter/IRC: Fobhep, hopfenmueller@atix.de #atix#cfgmgmtcamp2020 whoarewe I MatthiasDellweg I Physicist I SeniorSoftware](https://reader035.fdocuments.us/reader035/viewer/2022071008/5fc67c54bb3052547c281929/html5/thumbnails/32.jpg)
Converge Matrix
Prepare infrastructure to run role
(molecule)$ molecule matrix [-s default] converge
defaultdependencycreateprepareconverge
#atix #cfgmgmtcamp2020
![Page 33: Probing Ansible Bonds with Molecule Tests“DevOps”,… I Github/Twitter/IRC: Fobhep, hopfenmueller@atix.de #atix#cfgmgmtcamp2020 whoarewe I MatthiasDellweg I Physicist I SeniorSoftware](https://reader035.fdocuments.us/reader035/viewer/2022071008/5fc67c54bb3052547c281929/html5/thumbnails/33.jpg)
Converge Playbook
Prepare infrastructure to run role
---- name: Convergehosts: allroles:- role: nginx_sysinfo
#atix #cfgmgmtcamp2020
![Page 34: Probing Ansible Bonds with Molecule Tests“DevOps”,… I Github/Twitter/IRC: Fobhep, hopfenmueller@atix.de #atix#cfgmgmtcamp2020 whoarewe I MatthiasDellweg I Physicist I SeniorSoftware](https://reader035.fdocuments.us/reader035/viewer/2022071008/5fc67c54bb3052547c281929/html5/thumbnails/34.jpg)
Converge Playbook
Since playbook names are defaulted
---# molecule/default/molecule.yml[...]provisioner:name: ansiblelint:name: ansible-lint
#playbooks:#create: create.yml#prepare: prepare.yml
#atix #cfgmgmtcamp2020
![Page 35: Probing Ansible Bonds with Molecule Tests“DevOps”,… I Github/Twitter/IRC: Fobhep, hopfenmueller@atix.de #atix#cfgmgmtcamp2020 whoarewe I MatthiasDellweg I Physicist I SeniorSoftware](https://reader035.fdocuments.us/reader035/viewer/2022071008/5fc67c54bb3052547c281929/html5/thumbnails/35.jpg)
Test Matrix
(molecule)$ molecule matrix [-s default] testdefault
lint # Xdependency # Xdestroy # Xcreate # Xprepare # Xconverge # Xidempotenceverifydestroy # X
#atix #cfgmgmtcamp2020
![Page 36: Probing Ansible Bonds with Molecule Tests“DevOps”,… I Github/Twitter/IRC: Fobhep, hopfenmueller@atix.de #atix#cfgmgmtcamp2020 whoarewe I MatthiasDellweg I Physicist I SeniorSoftware](https://reader035.fdocuments.us/reader035/viewer/2022071008/5fc67c54bb3052547c281929/html5/thumbnails/36.jpg)
Idempotence
I Rerun converge
I all tasks unchanged? X
Careful: failure is not always a sign of wrong
#atix #cfgmgmtcamp2020
![Page 37: Probing Ansible Bonds with Molecule Tests“DevOps”,… I Github/Twitter/IRC: Fobhep, hopfenmueller@atix.de #atix#cfgmgmtcamp2020 whoarewe I MatthiasDellweg I Physicist I SeniorSoftware](https://reader035.fdocuments.us/reader035/viewer/2022071008/5fc67c54bb3052547c281929/html5/thumbnails/37.jpg)
Remove step from test sequence?
---# molecule/default/molecule.yml[...]scenario:test_sequence:- lint- dependency- destroy- create- prepare- converge#- idempotence- verify- cleanup- destroy
#atix #cfgmgmtcamp2020
![Page 38: Probing Ansible Bonds with Molecule Tests“DevOps”,… I Github/Twitter/IRC: Fobhep, hopfenmueller@atix.de #atix#cfgmgmtcamp2020 whoarewe I MatthiasDellweg I Physicist I SeniorSoftware](https://reader035.fdocuments.us/reader035/viewer/2022071008/5fc67c54bb3052547c281929/html5/thumbnails/38.jpg)
Rescue Idempotence
sometimes changes are more equal than others
---- name: apt-get updateapt:update_cache: true
changed_when: false
#atix #cfgmgmtcamp2020
![Page 39: Probing Ansible Bonds with Molecule Tests“DevOps”,… I Github/Twitter/IRC: Fobhep, hopfenmueller@atix.de #atix#cfgmgmtcamp2020 whoarewe I MatthiasDellweg I Physicist I SeniorSoftware](https://reader035.fdocuments.us/reader035/viewer/2022071008/5fc67c54bb3052547c281929/html5/thumbnails/39.jpg)
Test Matrix
(molecule)$ molecule matrix [-s default] testdefault
lint # Xdependency # Xdestroy # Xcreate # Xprepare # Xconverge # Xidempotence # Xverifydestroy # X
#atix #cfgmgmtcamp2020
![Page 40: Probing Ansible Bonds with Molecule Tests“DevOps”,… I Github/Twitter/IRC: Fobhep, hopfenmueller@atix.de #atix#cfgmgmtcamp2020 whoarewe I MatthiasDellweg I Physicist I SeniorSoftware](https://reader035.fdocuments.us/reader035/viewer/2022071008/5fc67c54bb3052547c281929/html5/thumbnails/40.jpg)
Verify
Test the results of your role. Default python testinfra + flake8 linter
---#molecule/default/molecule.yml[...]verifier:name: testinfralint:name: flake8
#atix #cfgmgmtcamp2020
![Page 41: Probing Ansible Bonds with Molecule Tests“DevOps”,… I Github/Twitter/IRC: Fobhep, hopfenmueller@atix.de #atix#cfgmgmtcamp2020 whoarewe I MatthiasDellweg I Physicist I SeniorSoftware](https://reader035.fdocuments.us/reader035/viewer/2022071008/5fc67c54bb3052547c281929/html5/thumbnails/41.jpg)
Verify with Testinfra
#molecule/default/test/test_default.pyimport osfrom testinfra.utils import ansible_runner
testinfra_hosts = ansible_runner.AnsibleRunner(os.environ['MOLECULE_INVENTORY_FILE']
).get_hosts('all')
def test_nginx_is_installed(host):nginx = host.package("nginx")assert nginx.is_installed
#atix #cfgmgmtcamp2020
![Page 42: Probing Ansible Bonds with Molecule Tests“DevOps”,… I Github/Twitter/IRC: Fobhep, hopfenmueller@atix.de #atix#cfgmgmtcamp2020 whoarewe I MatthiasDellweg I Physicist I SeniorSoftware](https://reader035.fdocuments.us/reader035/viewer/2022071008/5fc67c54bb3052547c281929/html5/thumbnails/42.jpg)
Verify with Ansible
---#molecule/default/molecule.yml[...]verifier:name: ansiblelint:name: ansible-lint
Create verify.yml in default dir
#atix #cfgmgmtcamp2020
![Page 43: Probing Ansible Bonds with Molecule Tests“DevOps”,… I Github/Twitter/IRC: Fobhep, hopfenmueller@atix.de #atix#cfgmgmtcamp2020 whoarewe I MatthiasDellweg I Physicist I SeniorSoftware](https://reader035.fdocuments.us/reader035/viewer/2022071008/5fc67c54bb3052547c281929/html5/thumbnails/43.jpg)
Test Matrix
(molecule)$ molecule matrix [-s default] testdefault
lint # Xdependency # Xdestroy # Xcreate # Xprepare # Xconverge # Xidempotence # Xverify # Xdestroy # X
Run single steps of test sequence?
#atix #cfgmgmtcamp2020
![Page 44: Probing Ansible Bonds with Molecule Tests“DevOps”,… I Github/Twitter/IRC: Fobhep, hopfenmueller@atix.de #atix#cfgmgmtcamp2020 whoarewe I MatthiasDellweg I Physicist I SeniorSoftware](https://reader035.fdocuments.us/reader035/viewer/2022071008/5fc67c54bb3052547c281929/html5/thumbnails/44.jpg)
Run Molecule Steps
$ molecule [--scenario-name default] <sequence_step>$ molecule lint # lint$ molecule create # only create infra$ molecule list # list created infra$ molecule converge # run role$ molecule login # connect with instance to debug$ molecule verify # only run testinfra/ansible tests$ molecule destroy # destroy infra$ molecule test [--destroy=never]# run all of the above (keep infra)
#atix #cfgmgmtcamp2020
![Page 45: Probing Ansible Bonds with Molecule Tests“DevOps”,… I Github/Twitter/IRC: Fobhep, hopfenmueller@atix.de #atix#cfgmgmtcamp2020 whoarewe I MatthiasDellweg I Physicist I SeniorSoftware](https://reader035.fdocuments.us/reader035/viewer/2022071008/5fc67c54bb3052547c281929/html5/thumbnails/45.jpg)
Role results
#atix #cfgmgmtcamp2020
![Page 46: Probing Ansible Bonds with Molecule Tests“DevOps”,… I Github/Twitter/IRC: Fobhep, hopfenmueller@atix.de #atix#cfgmgmtcamp2020 whoarewe I MatthiasDellweg I Physicist I SeniorSoftware](https://reader035.fdocuments.us/reader035/viewer/2022071008/5fc67c54bb3052547c281929/html5/thumbnails/46.jpg)
Role results
I What about more info?
#atix #cfgmgmtcamp2020
![Page 47: Probing Ansible Bonds with Molecule Tests“DevOps”,… I Github/Twitter/IRC: Fobhep, hopfenmueller@atix.de #atix#cfgmgmtcamp2020 whoarewe I MatthiasDellweg I Physicist I SeniorSoftware](https://reader035.fdocuments.us/reader035/viewer/2022071008/5fc67c54bb3052547c281929/html5/thumbnails/47.jpg)
Role results
#atix #cfgmgmtcamp2020
![Page 48: Probing Ansible Bonds with Molecule Tests“DevOps”,… I Github/Twitter/IRC: Fobhep, hopfenmueller@atix.de #atix#cfgmgmtcamp2020 whoarewe I MatthiasDellweg I Physicist I SeniorSoftware](https://reader035.fdocuments.us/reader035/viewer/2022071008/5fc67c54bb3052547c281929/html5/thumbnails/48.jpg)
Roles are parameterizable
{% if full_info %}<table><tr> <th colspan='2'>OS Facts</th> </tr><tr><td>This system is running on</td><td>{{ ansible_distribution }}</td>
</tr>[...]
</table>{% endif %}
#atix #cfgmgmtcamp2020
![Page 49: Probing Ansible Bonds with Molecule Tests“DevOps”,… I Github/Twitter/IRC: Fobhep, hopfenmueller@atix.de #atix#cfgmgmtcamp2020 whoarewe I MatthiasDellweg I Physicist I SeniorSoftware](https://reader035.fdocuments.us/reader035/viewer/2022071008/5fc67c54bb3052547c281929/html5/thumbnails/49.jpg)
Scenarios test different Situations
Our role supports an option to include more (sensitive) information.
(molecule)$ molecule init scenario -s full
fullDockerfile.j2molecule.ymlplaybook.ymltests
test_default.py
#atix #cfgmgmtcamp2020
![Page 50: Probing Ansible Bonds with Molecule Tests“DevOps”,… I Github/Twitter/IRC: Fobhep, hopfenmueller@atix.de #atix#cfgmgmtcamp2020 whoarewe I MatthiasDellweg I Physicist I SeniorSoftware](https://reader035.fdocuments.us/reader035/viewer/2022071008/5fc67c54bb3052547c281929/html5/thumbnails/50.jpg)
Don’t repeat yourself
Usually only some aspects are different.
$ cd full$ ln -sf ../default/molecule.yml$ ln -sf ../default/Dockerfile.j2$ ln -sf ../default/prepare.yml$ cp ../default/playbook.yml .
#atix #cfgmgmtcamp2020
![Page 51: Probing Ansible Bonds with Molecule Tests“DevOps”,… I Github/Twitter/IRC: Fobhep, hopfenmueller@atix.de #atix#cfgmgmtcamp2020 whoarewe I MatthiasDellweg I Physicist I SeniorSoftware](https://reader035.fdocuments.us/reader035/viewer/2022071008/5fc67c54bb3052547c281929/html5/thumbnails/51.jpg)
Converge in Scenario “full”
# playbook.yml---- name: Convergehosts: allvars:full_info: true # test with non default option
roles:- role: nginx_sysinfo
#atix #cfgmgmtcamp2020
![Page 52: Probing Ansible Bonds with Molecule Tests“DevOps”,… I Github/Twitter/IRC: Fobhep, hopfenmueller@atix.de #atix#cfgmgmtcamp2020 whoarewe I MatthiasDellweg I Physicist I SeniorSoftware](https://reader035.fdocuments.us/reader035/viewer/2022071008/5fc67c54bb3052547c281929/html5/thumbnails/52.jpg)
Verify Scenario “full”
$ rm tests/*$ ln -sf ../default/tests/test_common.py tests/$ cp ../default/tests/test_content_default.py \tests/test_content_full.py
# tests/test_content_full.py[...]def test_nginx_serving_content(host):
assert host.addr("localhost").port(80).is_reachableresult = host.check_output("curl localhost:80")assert "IPv4 addresses" in resultassert "AppArmor" in resultassert "Environment variables" in result
#atix #cfgmgmtcamp2020
![Page 53: Probing Ansible Bonds with Molecule Tests“DevOps”,… I Github/Twitter/IRC: Fobhep, hopfenmueller@atix.de #atix#cfgmgmtcamp2020 whoarewe I MatthiasDellweg I Physicist I SeniorSoftware](https://reader035.fdocuments.us/reader035/viewer/2022071008/5fc67c54bb3052547c281929/html5/thumbnails/53.jpg)
Run Scenario Test
# Test non default scenario(molecule)$ molecule test -s full
# Test all scenarios in parallel(molecule)$ molecule test --all --parallel
#atix #cfgmgmtcamp2020
![Page 54: Probing Ansible Bonds with Molecule Tests“DevOps”,… I Github/Twitter/IRC: Fobhep, hopfenmueller@atix.de #atix#cfgmgmtcamp2020 whoarewe I MatthiasDellweg I Physicist I SeniorSoftware](https://reader035.fdocuments.us/reader035/viewer/2022071008/5fc67c54bb3052547c281929/html5/thumbnails/54.jpg)
Use different Driver
(molecule)$ pip install 'molecule[hetznercloud]'(molecule)$ molecule scenario init hetzner_default \--driver-name hetznercloud
hetzner_defaultcreate.ymldestroy.ymlmolecule.ymlplaybook.ymlprepare.ymltests
test_default.py
#atix #cfgmgmtcamp2020
![Page 55: Probing Ansible Bonds with Molecule Tests“DevOps”,… I Github/Twitter/IRC: Fobhep, hopfenmueller@atix.de #atix#cfgmgmtcamp2020 whoarewe I MatthiasDellweg I Physicist I SeniorSoftware](https://reader035.fdocuments.us/reader035/viewer/2022071008/5fc67c54bb3052547c281929/html5/thumbnails/55.jpg)
Use different Driver
# molecule.yml---[...]driver:name: hetznercloud
.platform_base: &platform_baseserver_type: cx11
platforms:- <<: *platform_basename: centos7image: centos-7
- <<: *platform_basename: debian10image: debian-10
#atix #cfgmgmtcamp2020
![Page 56: Probing Ansible Bonds with Molecule Tests“DevOps”,… I Github/Twitter/IRC: Fobhep, hopfenmueller@atix.de #atix#cfgmgmtcamp2020 whoarewe I MatthiasDellweg I Physicist I SeniorSoftware](https://reader035.fdocuments.us/reader035/viewer/2022071008/5fc67c54bb3052547c281929/html5/thumbnails/56.jpg)
There is more …
I even more driver: DigitalOcean, Podman, Vagrant, LXC, EC2,...
I Test-Driven-Development → make red green
I systemd in docker? → RTFM: examples
I use CI/CD → RTFM: examples
#atix #cfgmgmtcamp2020
![Page 57: Probing Ansible Bonds with Molecule Tests“DevOps”,… I Github/Twitter/IRC: Fobhep, hopfenmueller@atix.de #atix#cfgmgmtcamp2020 whoarewe I MatthiasDellweg I Physicist I SeniorSoftware](https://reader035.fdocuments.us/reader035/viewer/2022071008/5fc67c54bb3052547c281929/html5/thumbnails/57.jpg)
ATIX
#atix #cfgmgmtcamp2020
![Page 58: Probing Ansible Bonds with Molecule Tests“DevOps”,… I Github/Twitter/IRC: Fobhep, hopfenmueller@atix.de #atix#cfgmgmtcamp2020 whoarewe I MatthiasDellweg I Physicist I SeniorSoftware](https://reader035.fdocuments.us/reader035/viewer/2022071008/5fc67c54bb3052547c281929/html5/thumbnails/58.jpg)
Questions?
I Matthias Dellweg
I Physicist
I Senior SoftwareEngineer
I Foreman, Pulp, Ansible(FAM), …
I Github: mdellweg,[email protected],IRC: x9c4
I Bernhard Hopfenmüller
I Physicist
I Senior IT Consultant
I Ansible (FAM), Kafka,“DevOps”, …
I Github/Twitter/IRC:Fobhep,[email protected]
https://github.com/ATIX-AG/ansible_nginx_sysinfo
#atix #cfgmgmtcamp2020