Ansibleããã©ã¯ãã£ã¹ã«å ¥ã£ãåŸãAnsibleã®ã³ãŒãã®éãç¹ã«ããŒã«ãéåžžã«æ¥éã«å¢å ãå§ããŸããã ãããã³ã°ãããã³ãããããã·ãããŒã¿ããŒã¹ãç£èŠããã°ã®åéãªã©ã®åœ¹å²ãªã©-ãããã¯å€æ°ãããŸãã ç¹å®ã®ãããžã§ã¯ãã«åºæã®åœ¹å²ããããŸãããå€ãã¯å ±éã®åé¡ã解決ããåããœãªã¥ãŒã·ã§ã³ã2åäœæããªãããã«ãããžã§ã¯ãããŒã éã§å ±æããããšèããŠããŸãã
å€ãã®ã³ãŒãã«å ããŠãå€ããããããµããåé¡ãçŸããŸãïŒå€åãžã®ææã 人ã ã¯ãããšã€ãªã¢ã³ãã®åœ¹å²ã«å€æŽãå ããããªãã®ã§ããããå°ç¡ãã«ããããšãæããŠã代ããã«ç¬èªã®ã³ããŒãäœæããŸãã å°å ¥ãããåé¡ãé·ãããŠæ€åºãããæãããããããã³ãŒããçŸåšéçºã®çŠç¹ã«ãªãå Žåãã³ãŒãã®ãªãã¡ã¯ã¿ãªã³ã°ã¯å®è¡ãããŸããã çµè«ïŒæªãã³ãŒãã¯éªã ããŸåŒã®ããã«æé·ããŸãã
ããªãã¿ã®åé¡ã«ã¯ãèªåãã¹ããšCIãšããããªãã¿ã®è§£æ±ºçããããŸãã ããããAnsibleã¯ããŸãã«ãå ·äœçãªãã¯ãããžãŒã§ãã æåã§ããŒã«ãä»®æ³ãã·ã³ã«ããŒã«ããäœããèœã¡ãŠããªããããšã確èªããããšãé€ããŠãAnsibleã³ãŒãããã§ãã¯ããããã«äœããããããšã¯å¯èœã§ããïŒ Moleculeãããžã§ã¯ãã®ååšãç¥ããŸã§ãããã¯äžå¯èœã ãšæããŸããã
Moleculeã䜿çšãããšãAnsible-codeã®ãè匱æ§ãã«é¢ãããã¹ãŠã®åé¡ãå®å šã«æé€ãããŸãã ãã¹ãé§åéçºã®æ¯æè ã§ããã°ãMoleculeã䜿çšããŠãããã¹ããæžãã-ãã¹ãã«å€±æãã-ã³ãŒããæžãã-ãã¹ãã«åæ Œããããšãããµã€ã¯ã«ã§Ansibleãéçºã§ããŸãã ããã§ãªãå Žåã¯ãæ¢åã®ã³ãŒãã«åã«ãMoleculeããã蟌ããã ãã§ãåäžã®ãã¹ããäœæããªããŠããã³ãŒãã£ã³ã°æšæºãžã®æºæ ãããªã¬ãŒãã¹ãçæ§ã«é¢ãã圹å²ããã§ãã¯ããã·ã¹ãã ãæ¢ã«ååŸããŠããŸãã
Moleculeã«ã€ããŠç¥ã£ããšãã«æåã«åºã質åã¯ããã¹ãããã»ã¹ã§ã©ãã§åœ¹å²ãæãããŸããïŒ çãã¯å¯èœã§ãã æãåçŽãªã®ã¯ãMoleculeãã·ã³ã§Dockerã䜿çšå¯èœãªå ŽåãããŒã«ã¯Dockerã³ã³ãããŒã«ãããã€ããããã¹ãå®è¡äžã«ç Žæ£ãããããšã§ãã ãã ããVagrantãAzureãEC2ãGCEãªã©ãMoleculeã®ä»ã®ãã©ã€ããŒã䜿çšã§ããŸãã
ä»-é çªã«ãã¹ãŠã«ã€ããŠã
èšçœ®
Ansibleãšåæ§ã«ãMoleculeã¯Pythonã§èšè¿°ãããããŒã ã«ãã£ãŠé 眮ãããŸã
pip install molecule
埮åŠãªéãããããŸãïŒ2018幎3æã®æç¹ã§ãæå·åãšpyopensslããã±ãŒãžãpipã§æåã§æŽæ°ããå¿ èŠããããŸãããããããªããšãMoleculeã®èµ·åãšããã©ãŒãã³ã¹ã«åé¡ããããŸããã Dockerãã©ã€ããŒïŒæšå¥šïŒã䜿çšããå Žåã¯ãDockerãšdocker-py pipããã±ãŒãžãã€ã³ã¹ããŒã«ããå¿ èŠããããŸãã
éçºçšãã·ã³ãšããŠãWindowsãšCygwinã䜿çšããŸããAnsibleã¯ãã®ãã·ã³ã§æ£åžžã«åäœããŸãã æ®å¿µãªãããCygwinãWindows DockerãMoleculeãšåéã«ãªããªãã£ããããLinuxã§ã®ã¿Moleculeã䜿çšããŠããŸãã
ãããžã§ã¯ãã®åæå
ããŒã«ãoldroleãšåŒã°ããå ŽåãMoleculeãããŒã«ã«è¿œå ããã«ã¯ãã³ãã³ããå®è¡ããå¿ èŠããããŸã
molecule init scenario -r oldrole
å®å šã«æ°ãã圹å²ãäœæããã«ã¯ãå®äºããå¿ èŠããããŸã
molecule init role -r newrole
ãããã®ã³ãã³ãã¯ãã©ã«ããŒãšãã¡ã€ã«ãäœæããŸããæãèå³æ·±ãã®ã¯ååãã©ã«ããŒã§ãã å®éãç§ã¯ãã®ã³ãã³ããäžåºŠã ã䜿çšããããã§ãã 次ã«ãåã ã®èšå®ãšãã¹ãã転éããããã«ããã¡ã€ã«æ§é ãä»ã®åœ¹å²ã«æåã§ã³ããŒããŸããã
è¡ãã
åæååŸããããžã§ã¯ãã®ã«ãŒãã§ã³ãã³ããå®è¡ããŸã
molecule test
ãã¡ãããæåã®ãªãªãŒã¹ã§ã¯ãããŸããããMoleculeãããªãã®åœ¹å²ã§äœãããããšããŠããããã©ãã§åé¡ãçºçããããããã«ããããŸãã ã³ã³ãœãŒã«ãžã®åºåã«ã¯ãèšç»ãããå®è¡ã¹ã¯ãªãããå«ãŸããŸãã
--> Test matrix âââ default âââ lint âââ destroy âââ dependency âââ syntax âââ create âââ prepare âââ converge âââ idempotence âââ side_effect âââ verify âââ destroy
ã芧ã®ãšãããMoleculeã¯ãéçã³ãŒãåæãããã®å®è¡çµæã«åºã¥ãã€ã³ãã©ã¹ãã©ã¯ãã£ãã¹ãã®å®è¡ãŸã§ãããŒã«ãå æ¬çã«ãã§ãã¯ããããã®æ¢è£œã®ãã³ãã¬ãŒãã¹ã¯ãªãããæäŸããŠããŸãã ãã¡ãããèšå®ã®å©ããåããŠããã®ã¹ã¯ãªãããå€æŽããããç¹å®ã®æé ã®ã¿ãå®è¡ãããã§ããŸãã
å®è¡çµæãã倱æãããã³ã³ãœãŒã«ã«ååãªæ å ±ããªãå Žåã¯ãå®è¡ããŠã¿ãŠãã ãã
molecule --debug test
-ããã«ãããçµè«ããã詳现ã«ãªããŸãã
ãã¹ãããããã°ããéçšã§ããã¹ãç°å¢ã«ãã°ã€ã³ããŠäœãééã£ãŠããã®ãã確èªãããå ŽåããããŸãã ãããè¡ãã«ã¯ãã³ãã³ããå®è¡ãããšäŸ¿å©ã§ã
molecule test --destroy=never
-ãããã£ãŠããã¹ãç°å¢ã¯åé€ãããŸãããDockerã䜿çšããŠããå Žåãéåžžã©ãããã°ã€ã³ã§ããŸãã
docker exec -it instance /bin/bash
éç解æ
ãAy-yy-yyãYAMLãã¡ã€ã«ã®å é ã«3ã€ã®ãã€ãã¹èšå·ãä»ããã®ãå¿ããŸããïŒ ãããŠãããã§ã¯ç©ºã®è¡ã«ã¹ããŒã¹ããããŸãïŒãæåã®ã¹ãããã§ãã®ãããªã¡ãã»ãŒãžã®ã»ãšãã©ããããŸãã 幞ããªããšã«ããããã®å€ãã¯èŠåã§ãããã¹ãã£ã³å šäœãåæ¢ããŸããã ãããããã£ãšéèŠãªæãããããããŸãã ããšãã°ããã®æ®µéã§ãã·ã¹ãã ã¯ãã³ãã³ãã¢ãžã¥ãŒã«ãåé¿ã§ããå Žåã«ã·ã§ã«ã¢ãžã¥ãŒã«ã誀ã£ãŠäœ¿çšããŠããããšã瀺ããããç¹å¥ãªget_urlã¢ãžã¥ãŒã«ã䜿çšã§ããå Žåã«wgetã³ãã³ãã䜿çšãããããå ŽåããããŸãã ãŸãããã¹ããããå ŽåïŒããã³Pythonã§ãã¹ããäœæããå¿ èŠãããå ŽåïŒãã·ã¹ãã ã¯Pythonã³ãŒãã«å¯ŸããŠflake8éçã¢ãã©ã€ã¶ãŒãå®è¡ããŸãã
圹å²ã®éå§
éçåæã®æ®µéã§éèŠãªã³ã¡ã³ããèŠã€ãããªãã£ãå Žåãã·ã¹ãã ã¯ããŒããäœæããAnsibleèªäœã䜿çšããŠããŒã«ãèµ·åããŸãã 圌女ããããè¡ãæ¹æ³ã¯ã
molecule/default/molecule.yml
ãã¡ã€ã«ã§å®çŸ©ãããŠããŸãã ç§ã®å Žåã次ã®ããã«ãªããŸãã
--- dependency: name: galaxy options: role-file: requirements.yml driver: name: docker lint: name: yamllint platforms: - name: instance image: solita/ubuntu-systemd:latest command: /sbin/init privileged: True volumes: - "/sys/fs/cgroup:/sys/fs/cgroup:rw" provisioner: name: ansible lint: name: ansible-lint scenario: name: default verifier: name: testinfra lint: name: flake8
ãã¹ãæžã¿ã®åœ¹å²ãä»ã®åœ¹å²ãäºåã«ã€ã³ã¹ããŒã«ããªããšã€ã³ã¹ããŒã«ã§ããªãå Žåãååã·ã¹ãã ã«å¿ èŠãªäŸåé¢ä¿ãããŠã³ããŒãããããã«äŸé Œããå¿ èŠããããŸãã ããã¯ãäŸåé¢ä¿ãš
requirements.yml
ãã¡ã€ã«ã®èšå®ãéããŠè¡ãããŸãïŒãã®ãã¡ã€ã«ã®è©³çŽ°ã«ã€ããŠã¯ãGalaxyãã«ããåç §ããŠãã ãã ïŒã
driver
ã»ã¯ã·ã§ã³ã§ã䜿çšãããã©ã€ããŒãéžæããŸãïŒå¯èœãªãªãã·ã§ã³ã«ã€ããŠã¯ã ãã¡ããåç §ããŠãã ãã ïŒã
platforms
ã»ã¯ã·ã§ã³ã§ãããŒã«ãããŒã«ãããã©ãããã©ãŒã ãéžæããŸãã dockerã®å Žåãåºæ¬çãªdockerã³ã³ãããŒãæå®ããŸããããã§ã¯ãäžèšã®äŸã«ç¹ã«æ³šæãæã£ãŠãã ããã 圹å²ãsystemdã«åºã¥ããŠãµãŒãã¹ãäœæããå Žåãdockerã³ã³ãããŒã§ããããå®è¡ã§ããããã«ããã«ã¯ãäŸã«ç€ºãããã«ç¹å¥ãªsolita / ubuntu-systemdã€ã¡ãŒãžã䜿çšããå ·äœçã«æ§æããå¿ èŠããããŸãã ç°ãªããã©ãããã©ãŒã ïŒããšãã°ãç°ãªãLinuxãã£ã¹ããªãã¥ãŒã·ã§ã³ïŒã§åœ¹å²ããã¹ãããå Žåã¯ãããã§ããã€ãã®å€ãæå®ã§ããŸãã
æ§æãã¡ã€ã«ã®ä»ã®ã»ã¯ã·ã§ã³ã®ç®çã¯ã ããã¥ã¡ã³ãã«èšèŒãããŠããŸã ïŒããšãã°ãããã§ã¯ãå®è¡ãããªãã¯ã¹ãåå®çŸ©ããéçã¢ãã©ã€ã¶ãŒãæ§æã§ããŸãïŒã
ç·šéããå¿ èŠããããšæããã次ã®ãã¡ã€ã«ã¯
playbook.yml
ã§ãã ããã¯ãããŒãã§ããŒã«ãããŒã«ããããã«å®è¡ãããéåžžã®Ansibleãã¬ã€ããã¯ã§ãããããŒã«ã®1ã€ã§ã¯æ¬¡ã®ããã«ãªããŸãã
--- - name: Converge hosts: all roles: - role: ansiblebit.oracle-java - role: fluteansible tasks: - name: mkdir for score file: dest: /var/opt/flute/score state: directory mode: "0775" group: flute - name: copy flute config file copy: src: flute.xml dest: opt/flute/flute.xml mode: "0644" - name: start flute service service: name: flute state: started
ããã§åŠæ¹ãã
- ããŒã«ã®ã€ã³ã¹ããŒã«ã«å è¡ãããã¹ãŠã®ãã®ïŒãã®å Žåãansiblebit.oracle-javaããŒã«ãrequirements.ymlã§ãæå®ãããŠãããããèªåçã«ã€ã³ã¹ããŒã«ãããŸãïŒã
- å®éã«ãã¹ããããããŒã«ïŒãã®å Žåã
fluteansible
ïŒãã€ã³ã¹ããŒã«ããŸãã - ããã«ãã§ãã¯ããããã«å¿ èŠãªè¿œå ã®æé ïŒãã®å Žåãæ§æãã¡ã€ã«ãã³ããŒãããµãŒãã¹ãå®è¡ããŠãã€ã³ãã©ã¹ãã©ã¯ãã£ãã¹ãã§ãµãŒãã¹ãå®è¡ãããæ£ããå®è¡ãããŠããããšã確èªã§ããããã«ããŸãïŒã
ãåæããšãã¹ãçæ§ãã®æé
ã¹ãããåæååã§ã¯ãã¯ãªãŒã³ãªãã¹ãããŒãã§
playbook.yml
ãå®è¡ããã ãã§ãã
次ã«ã
--diff
ã¹ãããã§ãååã¯
--diff
ãªãã·ã§ã³ïŒè©³çŽ°ã«ã€ããŠã¯Ansibleã®ããã¥ã¡ã³ããåç §ïŒã䜿çšããŠåã
--diff
ã
--diff
ããå®è¡ããåèµ·åãããšãã«åã圹å²ãå®è¡ãããªãããã«ããŸãäœããå€ãã-çµå±ãã·ã¹ãã ã¯ãã§ã«æãŸããç¶æ ã«ãããŸãã
ååãšããŠãAnsible-codeã®ã¹ãçæ§ã¯èªåçã«æäŸãããŸãããå Žåã«ãã£ãŠã¯ïŒäž»ã«ã·ã§ã«ã³ãã³ãã®å ŽåïŒãã³ãã³ããç¹°ãè¿ããªãæ¡ä»¶ãåå¥ã«æå®ããå¿ èŠããããŸãã
ã¹ãããã確èªãã
ããã§æãèå³æ·±ãããšãèµ·ãã£ãŠããŸãã ããã©ã«ãã§ã¯ãMoleculeã¯testinfraã·ã¹ãã ã䜿çšããŠãããŒã«ãããŒã«ããåŸã«ãã¹ãããŒãã®çŸåšã®ç¶æ ããã§ãã¯ããŸãã ãã¹ãã¯Pythonã§èšè¿°ããã
molecule/default/tests/test_default.py
ã ãã¹ãããã·ãŒãžã£å㯠"
test_
"ã§å§ãŸãå¿ èŠããããŸãã
ã·ã¹ãã ã®ç¶æ ã«é¢ããŠç¢ºèªãããã»ãŒãã¹ãŠã®ããšããtestinfraã®æ°è¡ã®ã³ãŒãã§ç°¡åã«è¡ãããšãã§ããŸãã ããšãã°ãããã€ãã®ã³ãã³ããèµ·åã§ããããšã確èªãããå Žåããã®å®è¡çµæãšã³ã³ãœãŒã«ãžã®åºåã確èªããã«ã¯ã次ã®ããã«ããŸãã
def test_jython_installed(host): cmd = host.run('jython --version') assert cmd.rc == 0 assert cmd.stderr.find(u'Jython 2') > -1
ãµãŒãã¹ãå®è¡ãããŠããããšã®ç¢ºèªã¯æ¬¡ã®ãšããã§ãã
def test_service_is_running(host): assert host.service('flute').is_running
ãã¡ã€ã«ã®ååšãšãã®å 容ã¯ã次ã®ããã«ç¢ºèªã§ããŸãã
def test_log_files(host): err = host.file('/var/log/flute/std.err') assert err.exists assert err.contains('Flute started')
testinfraã®å¯èœæ§ã¯å°œããããšããããŸãã;çµã¿èŸŒã¿ã®æ©èœã«é¢ãã詳现ãªããã¥ã¡ã³ãã¯ãã¡ãã§ãã
CI / CDãã€ãã©ã€ã³
éçãã§ãã¯ãšèªåãã¹ããå®äºãããã次ã®èªç¶ãªã¹ãããã¯CI / CDãã€ãã©ã€ã³ãæ§ç¯ããããšã§ãã
AnsibleããŒã«ã®ãªãªãŒã¹ã¯ãGithubãªããžããªã®masterãã©ã³ããžã®ææ°ã³ãããã®ãªãªãŒã¹ãæ€èšããŠããAnsible Galaxyããããããããšã§ãã ãããã£ãŠãä¿è·ããããã¹ã¿ãŒãã©ã³ãã§ããŒãžã§ã³ç®¡çã«GitHubã䜿çšããå Žåããã¹ã¿ãŒãã©ã³ããžã®åããŒãžã¯ãªãªãŒã¹ã«ãªããŸãã Moleculeãã¹ããå®è¡ãããCIãµãŒããŒã§ãå䜵ã«å¿ èŠãªæ¡ä»¶ã§ã³ãŒãã®ã¬ãã¥ãŒãšæ€èšŒãè¡ããšãå®å®ããé ä¿¡ãã€ãã©ã€ã³ãæ§ç¯ãããŸãã
Jenkins Multibranch Pipelineã䜿çšããJenkinsfileã¯2ã€ã®ã¹ãããã ãã§æ§æãããŠããŸãã
node { stage ("Get Latest Code") { checkout scm } try { stage ("Molecule test") { /* Jenkins check out the role into a folder with arbitrary name, so we need to let Ansible know where to find 'fluteansible' role*/ sh 'mkdir -p molecule/default/roles' sh 'ln -sf `pwd` molecule/default/roles/fluteansible' sh 'molecule test' } } catch(all) { currentBuild.result = "FAILURE" throw err } }
å®éã«ã¯ã1ã€ã®æ©èœã«ã€ããŠã§ã¯ãªããŠãã話ãããšã¯äœããããŸããã Jenkins Multibranchã¯ããããžã§ã¯ãã®ååãšã¯ç°ãªãååã®ãã©ã«ããŒã«ãããžã§ã¯ããããŠã³ããŒãããŸãïŒ
fluteansible_PR-3-7YQKOAVNLO7P2S6Z4O6BLBAFYNYCTBDGTLQFWMXA35XGZZZMLQRA
ïŒãMoleculeãæ£åžžã«èŠã€ããããã«å¿ èŠã§ãåå/ããã©ã«ã/ããŒã«ãã©ã«ãå ã®ãããžã§ã¯ãã®ã«ãŒããžã®ã·ã³ããªãã¯ãªã³ã¯ã®åœ¢åŒã®ããŒã«ãããã
ãã®èšäºã§ã¯ã Moleculeã®å¥ã®Jenkinsfileã®äŸãèŠã€ããããšãã§ããŸãã ãã®äŸã§ã¯ãäœæè ãæ ãããããååã¹ã¯ãªããã®åã¹ããããã¹ããŒãžã«åå²ããæ£åžžã«å®äºãããšãã¿ã°ãGitãªããžããªã«æžã蟌ã¿ãŸãã
ãããã«
Ansibleã¯ããèªäœãåªããã·ã¹ãã ã§ãã ããããMoleculeãããã°ãã³ãŒããå°ç¡ãã«ããæããæããããšãªããMoleculeã§å¥è·¡ãèµ·ããããšãã§ããŸãã Moleculeã®äœ¿ãããããšãã®æ©èœã«ãããMoleculeã¯Ansibleã¹ã¯ãªãããéçºããããã®ãå¿ é ãããŒã«ã«ãªã£ãŠããŸãã
MoleculeãšJenkinsã䜿çšããŠéçºãããŠããããŒã«ã®äŸã¯ã ããã«ãããŸã ã