ã¿ãªãããããã¼ã¯ãªã¹ãã¹ãRed Hatã®ããã¨ãã§ãã
Ansibleã¦ã¼ã¶ã°ã«ã¼ãã®Advent Calender 2022æçµæ¥ã®è¨äºã¨ãã¦ã2022å¹´ã®Ansibleã«ã©ã®ãããªå¤åããããããªã®ãã¨ãã£ããããã®å人çãªäºæ³ãæ¸ãæ®ãã¦ãããã¨æãã¾ãã
ãã®è¨äºã¯ãAnsibleã¦ã¼ã¶ã³ãã¥ããã£ã®Advent Calendarã§ããããã¢ããã¹ããªã¼ã ã®æ å ±ãå ã«ãã¦æ¸ãã¦ãã¾ããRed Hatã®ãã¯ãã«ã«ãµãã¼ãã«ã¯åãåãããªãã§ã!
2021å¹´ã®Ansibleã®ãµããããã«ã¤ãã¦ã¯ãã³ãã©ãã©ããã
2022å¹´ã«ãã£ã¦ããããªå¤å
ç¾å¨ã®Ansible Core 2.xã¯ãããç¨åº¦å®å®æã«å ¥ã£ã¦ãããããæ©è½é¢ã§åçãªå¤æ´ãå ãããããã¨ã¯ãã°ãããªããã®ã¨èãã¦ãã¾ããããããAnsible Coreã稼åããPlaybookã®å®è¡ç°å¢ã¨ãã¦ã¯ã2022å¹´ã«å¤§ããªå¤åããã£ã¦ãããã§ãã
ç¾å¨ãå¤ãã®Ansible Playbookã®å®è¡ç°å¢ã¯ãã³ã³ããã¼ã©ã¨ãªããã¼ãã®ã·ã¹ãã ã¯ã¤ãããã¦ã¼ã¶ã®ãã¼ã ãã£ã¬ã¯ããªãããã¦Pythonã®virtualenvãå©ç¨ããä»®æ³çãªå®è¡ç°å¢ãå©ç¨ãã¦æ§ç¯ããã¦ãã¾ãã
Ansibleã®ã¢ã¸ã¥ã¼ã«ç¾¤ã¯ãWindowsåãã®ã¢ã¸ã¥ã¼ã«ãé¤ãã¦ãPythonã§æ¸ãããå°ããªããã°ã©ã ã§æ§æããã¦ãã¾ããã¢ã¸ã¥ã¼ã«ãæ¸ãããã§å¿ è¦ã¨ãªãããã¡ã¤ã«ã®ãã¼ããã·ã§ã³ãã§ãã¯ãªã©ãªåºæ¬çãªæ©è½ã¯ãAnsibleã®ã³ã¢ã«ã©ã¤ãã©ãªã¨ãã¦ãã³ãã«ããã¦ãããããç¹ã«æèãããã¨ãªãã¢ã¸ã¥ã¼ã«ããå©ç¨ã§ãã¾ãã
ããããä¾ãã°EC2ã¤ã³ã¹ã¿ã³ã¹ã®ä½æã¨ãã£ããªãã¬ã¼ã·ã§ã³ã«å©ç¨ããbotoãªã©ã®ã©ã¤ãã©ãªã«ã¤ãã¦ã¯ãå©ç¨è ãã¢ã¸ã¥ã¼ã«æ¯ã«åå¥ã«ã³ã³ããã¼ã«ãã¼ãã®å®è¡ç°å¢ã«æºåããå¿ è¦ãããã¾ããAnsibleã®ã³ã¢æ©è½ã«å«ã¾ããªããã¢ã¸ã¥ã¼ã«ãåä½ããããã«ã®ã¿å¿ è¦ãªã©ã¤ãã©ãªç¾¤ã®ã¤ã³ã¹ãã¼ã«ããã¼ã¸ã§ã³ãæèãã¦ãå®è¡ç°å¢ãç¶æ管çããå¿ è¦ãããã¾ããããã®ç®¡çã常ã«æèãã¦ããªãã¨ãããã³ã³ããã¼ã«ãã¼ãã®å®è¡ç°å¢ã§åä½ããPlaybookããå¥ã®ã³ã³ããã¼ã«ãã¼ãã§ã¯ã©ã¤ãã©ãªä¸è¶³ããã¼ã¸ã§ã³ãã¹ãããã§åä½ããªã...ã¨ãã£ãåé¡ãçºçãããã¨ã«ãªãã¾ãã
ãã®ç¶æ管çã«é¢ããã³ã¹ãã¯ãPlaybookã«ãã£ã¦èªååããããªãã¬ã¼ã·ã§ã³ãå¤æ§åããã°ããã»ã©é¡èã«å¤§ãããªã£ã¦ãã¾ããã¾ããPlaybookãRoleåãã¦åå©ç¨ãé²ãã¨ãããããæ³å®å¤ã®ç°å¢ã§èµ·åãããå¯è½æ§ãã§ã¦ãã¾ãã
ãã®ãããªãå®è¡ç°å¢ã®å·®ç°ã解決ããããã«ãããã¾ã§ããã¤ãã®ã¢ããã¼ããåå¨ãã¦ãã¾ããã
- å®è¡ç°å¢ãæ§ç¯ããããã®æé æ¸ã«ããæåæ§ç¯ãã
- 1.ã®æ§ç¯èªä½ãèªååããããã®ãã¼ãã¹ãã©ããçãªPlaybookã®ä½æã¨å®è¡ãã
- å®è¡ç°å¢ãPythonã®virtualenvã§ä½æå¾ã«rpmãªã©ã§ããã±ã¼ã¸åããã³ã³ããã¼ã«ãã¼ãã«ã¤ã³ã¹ãã¼ã«ãã¦å©ç¨ãã
æ°´å¹³å±éããããã®ã¯ãçããªã3.ã§ãããããã±ã¼ã¸ã®é©ç¨å¾ã«ã³ã³ããã¼ã«ãã¼ãã®ç®¡çè 権éãæã¤å©ç¨è ã«ãã£ã¦æ¹å¤ããã¦ãã¾ãå¯è½æ§ã¯æ®ãã¾ãã
ããã§ã3.ã®ããã±ã¼ã¸åã®ã¢ããã¼ããããä¸æ©é²ãã¦ã³ã³ããåãã¦ãã¾ãã°è¯ãã®ã§ã¯ãªãã...ã¨ããã¢ã¤ãã¢ãæ°å¹´åããè°è«ããã¦ãã¾ããã
以éã§ã¯ã2022å¹´ã«åãã¦åºãæ®åããããªäºæããã¦ããããã®ã³ã³ãããã¼ã¹ã®Playbookå®è¡ç°å¢ã«ã¤ãã¦ãç´¹ä»ãã¾ãã
ã³ã³ãããã¼ã¹ã®å®è¡ç°å¢
ã³ã³ãããã¼ã¹ã®å®è¡ç°å¢ã¯ããã®åã®éããAnsible CoreãCollectionsãããããå¿ è¦ã¨ããPythonã¢ã¸ã¥ã¼ã«ãªã©ã®ã©ã¤ãã©ãªç¾¤ãã³ã³ããã¨ãã¦ããã±ã¼ã¸åãããã®ã§ãã
ã³ã³ããåãããå®è¡ç°å¢ã§ã¯ãåç´ã«podmanãdockerã§ansible-playbookã³ãã³ããå®è¡ããã ãã§ãªããPlaybookã®å®è¡ãã°ãæ®ããããSSHæ¥ç¶ã«å©ç¨ããéµãé ç½®ãããã³ããã¼ã«ãã¼ãã®ãã£ã¬ã¯ããªãééãã¦ã³ããã¦å©ç¨ã§ããããã«ãããªã©ãããã¾ã§ã®å®è¡ç°å¢ã§ã¯æèããå¿ è¦ã®ãªãã£ãç¹ãèæ ®ããªããã°ãªãã¾ããã
Ansibleããã¸ã§ã¯ãã§ã¯ããã®ãããªã³ã³ããç°å¢åºæã®è¨å®ãéçºè ãå©ç¨è ãããã»ã©æèããã«ãæ¬æ¥ãããããã¨ã«éä¸ã§ãããããã³ã³ãããã¼ã¹ã®å®è¡ç°å¢ãã使ãããããã¦ãä½ããããã®ãã¼ã«ãæä¾ãã¦ãã¾ãã
ããããã®ãã¼ã«ã®ç´°ã使ãæ¹ã¯ãæ¥å¹´ã®ã©ããã§ãç´¹ä»ããã¨ãã¦ãããã§ã¯ã½ã¼ã¹ã³ã¼ããªãã¸ããªãããã¥ã¡ã³ã®ãªã³ã¯ãç°¡åãªåä½ãµã³ãã«ããç´¹ä»ãããã¨æãã¾ãã
使ã
æ®å¿µãªãããansible-playbookã³ãã³ãã«ã¯ãã³ã³ãããã¼ã¹ã®å®è¡ç°å¢ãå©ç¨ãã¦Playbookãå®è¡ããæ©è½ã¯ããã¾ããã
ãã®å½¹å²ã®æ ãã®ãAnsible Navigatorã§ããAnsible Navigatorã¯ãã³ã³ã½ã¼ã«ãã¼ã¹ã®Playbookã©ã³ãã£ã¼ã§ãã ããã¯ã¨ã³ãã¨ãã¦Ansible Runnerãå©ç¨ãã¦ãããå¾æ¥ã®ã³ã³ããã¼ã©ä¸ã«ã¤ã³ã¹ãã¼ã«ãããAnsible Coreããã³ã³ãããã¼ã¹ã®å®è¡ç°å¢ãå©ç¨ãã¦Playbookãå®è¡ã§ãã¾ãã
- ãã£ã¬ã¯ããªæ§é
âââ ansible-navigator.yml ...(1) âââ inventory â âââ hosts ...(2) âââ logs âââ playbook âââ test.yml ...(3)
- (1) è¨å®ãã¡ã¤ã« - ansible-navigator.yml åèæ å ±
--- ansible-navigator: execution-environment: container-engine: docker enabled: True image: quay.io/ansible/ansible-runner:stable-2.12-devel pull-policy: missing logging: level: debug append: True file: ./logs/ansible-navigator.log mode: stdout playbook-artifact: enable: True replay: ./logs/artifact/replay.json save-as: ./logs/artifact/replay.json
- (2) ã¤ã³ãã³ããªãã¡ã¤ã« - inventory/hosts
[linux] 192.168.125.110 192.168.125.111 192.168.125.112 [linux:vars] ansible_user=ansible
- (3) Playbook - playbook/test.yml
--- - hosts: all gather_facts: false tasks: - ping:
ããã§ã¯ããã¹ãç¨ã®Playbookãå®è¡ãã¦ã¿ã¾ããããä»åã®å®è¡ä¾ã§ã¯ãansible-navigator runã³ãã³ããå®è¡ããã¨ãansible-navigator.ymlãã¡ã¤ã«ã®environmentl.imageãã©ã¡ã¼ã¿ã«æå®ããã³ã³ããã¤ã¡ã¼ã¸ããã¦ã³ãã¼ããããã®ã³ã³ããå ã§Playbookãå®è¡ãã¾ãããã®ããã«ãã¦ãAnsible Navigatorã¨ã³ã³ãããã¼ã¹ã®å®è¡ç°å¢ãå©ç¨ããã°ãéçºç°å¢ããã¹ãç°å¢ãããã¦æ¬çªç°å¢ã§å®å ¨ã«çµ±ä¸ãããå®æ³ç°å¢ãå©ç¨ãããã¨ãã§ãã¾ãã
Playbookã®å®è¡ãã°ã¯ã(1)ã®ansible-navigator.logging.fileã«ãããã£ã¦ãlogs/ansible-navigator.logã«è¨é²ããã¾ããã¾ããSSHæ¥ç¶ã«å¿ è¦ã¨ãªãã³ã³ããã¼ã«ãã¼ãä¸ã®ç§å¯éµããssh-agentãã£ã¦ã¹ãã¢ãã¦ããç§å¯éµã®ãã¹ãã¬ã¼ãºããansible-navigatorã«ãã£ã¦ééçã«åç §ããå½¢ã§ã³ã³ããå®è¡ç°å¢ãèµ·åããã¾ãã
$ pip install --user ansible-navigator $ eval `ssh-agent` $ ssh-add Enter passphrase for /home/hsaito/.ssh/id_rsa: ******** Identity added: /home/hsaito/.ssh/id_ecdsa (/home/hsaito/.ssh/id_ecdsa) $ ansible-navigator run playbook/test.yml -i inventory/hosts ----------------------------------------------------------------------------------- Execution environment image and pull policy overview ----------------------------------------------------------------------------------- Execution environment image name: quay.io/ansible/ansible-runner:stable-2.12-devel Execution environment image tag: stable-2.12-devel Execution environment pull policy: missing Execution environment pull needed: True ----------------------------------------------------------------------------------- Updating the execution environment ----------------------------------------------------------------------------------- stable-2.12-devel: Pulling from ansible/ansible-runner a1d0c7532777: Pull complete 0051e33d76d8: Pull complete 3fb241a85b00: Pull complete 014e39b4ae38: Pull complete 442efd345444: Pull complete 79831b585400: Pull complete 27b3a0cd32c9: Pull complete a0432b3d84b6: Pull complete 93e0336932fc: Pull complete 44e3598950f0: Pull complete fc120b105d20: Pull complete 25df1b0a7e8d: Pull complete b1cb03d0f630: Pull complete 324f5147ca8f: Pull complete Digest: sha256:2c44521150e5f260a85ee99fee4f3a5e41216510c9f408d6e2a64df9eb9064ab Status: Downloaded newer image for quay.io/ansible/ansible-runner:stable-2.12-devel quay.io/ansible/ansible-runner:stable-2.12-devel PLAY [all] ********************************************************************* TASK [ping] ******************************************************************** ok: [192.168.125.110] ok: [192.168.125.112] ok: [192.168.125.111] PLAY RECAP ********************************************************************* 192.168.125.110 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 192.168.125.111 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 192.168.125.112 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
ä¸æ¹ã§ãå¾æ¥ã®ã³ã³ããã¼ã©ã«ç´æ¥ã¤ã³ã¹ãã¼ã«ãããPlaybookå®è¡ç°å¢ãå©ç¨ãç¶ãããå ´åãããã§ããããansible-navigatorã³ãã³ãã«--ee falseãªãã·ã§ã³ãæå®ãããã¨ã§ãã³ã³ãããã¼ã¹ã®å®è¡ç°å¢ã§ã¯ãªããå¾æ¥ã®ã³ã³ããã¼ã©ä¸ã«ã¤ã³ã¹ãã¼ã«ãããansible-playbookã³ãã³ããå©ç¨ãããã¨ãã§ãã¾ãã
$ ansible-navigator run --ee false playbook/test.yml -i inventory/hosts PLAY [all] ********************************************************************* TASK [ping] ******************************************************************** ok: [192.168.125.111] ok: [192.168.125.112] ok: [192.168.125.110] PLAY RECAP ********************************************************************* 192.168.125.110 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 192.168.125.111 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 192.168.125.112 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
ãã®ããã«ãansible-navigatorãå©ç¨ããã°ãPlaybookå®è¡æã«ãå¾æ¥ã®ã³ã³ããã¼ã«ãã¼ãä¸ã«ã¤ã³ã¹ãã¼ã«ãããå®è¡ç°å¢ã¨ãã³ã³ãããã¼ã¹ã®å®è¡ç°å¢ãå¿ è¦ã«å¿ãã¦é¸æãããã¨ãã§ãã¾ãã
ä½ã
ã³ã³ãããã¼ã¹ã®å®è¡ç°å¢ã§å©ç¨ããã³ã³ããã¤ã¡ã¼ã¸ã¯ãåºæ¬çã«ã¯Quay.ioããæä¾ãããansible-runnnerã®ã¤ã¡ã¼ã¸ãå©ç¨ãã¾ãã
ãããã«ã¹ã¿ãã¤ãºãã¦ç¬èªã®CollectionsãPythonã¢ã¸ã¥ã¼ã«ãã¤ã³ã¹ãã¼ã«ããç¶æ ã§å©ç¨ãããã¨ããã¦ã¼ã¹ã±ã¼ã¹ãå½ç¶çºçãã¦ããã§ããããAnsible Builderãå©ç¨ããã°ãå©ç¨è ãã«ã¹ã¿ãã¤ãºããã³ã³ããã¤ã¡ã¼ã¸ãä½æãããã¨ãã§ãã¾ãã
å ¬å¼ããã¥ã¡ã³ãã¨å ã«ãã¦ã以ä¸ã®ãããªãã£ã¬ã¯ããªãæ§æãã¦ã«ã¹ã¿ã ã³ã³ããã¤ã¡ã¼ããä½æããä¾ããç´¹ä»ãã¾ãã
- ãã£ã¬ã¯ããªæ§é
. âââ ansible.cfg ...(1) âââ bindep.txt ...(2) âââ custom_ee.yml ...(3) âââ requirements.txt ...(4) âââ requirements.yml ...(5)
- (1) Ansible Coreè¨å®ãã¡ã¤ã« - ansible.cfg
[defaults] host_key_checking=false
- (2) ã³ã³ããã¤ã¡ã¼ã¸ä½ææã«ã¤ã³ã¹ãã¼ã«ããOSããã±ã¼ã¸ç¾¤ - bindep.txt
krb5-devel [platform:rpm]
- (3) Ansible Builderã®ã³ã³ãããã«ãè¨å®ãã¡ã¤ã« - custom_ee.yml
version: 1 build_arg_defaults: ANSIBLE_GALAXY_CLI_COLLECTION_OPTS: '-v' EE_BASE_IMAGE: 'quay.io/ansible/ansible-runner:stable-2.11-devel' ansible_config: 'ansible.cfg' dependencies: galaxy: requirements.yml python: requirements.txt system: bindep.txt additional_build_steps: prepend: | RUN whoami RUN cat /etc/os-release append: - RUN echo This is a post-install command! - RUN ls -la /etc
- (4) ã³ã³ããã¤ã¡ã¼ã¸ä½ææã«ã¤ã³ã¹ãã¼ã«ããPythonã¢ã¸ã¥ã¼ã«ç¾¤ - requirements.txt
awxkit
- (5) ã³ã³ããã¤ã¡ã¼ã¸ä½ææã«ã¤ã³ã¹ãã¼ã«ããCollections/Roles群 - requirements.yml åèæ å ±
collections: - ansible.posix - awx.awx - community.general
ããã§ã¯ãAnsible Builderãå©ç¨ãã¦ã«ã¹ã¿ãã¤ãºãããã³ã³ããå®è¡ç°å¢ã®ã¤ã¡ã¼ã¸ãä½æãã¦ã¿ã¾ãã以ä¸ã®ä¾ã§ã¯ã-fãªãã·ã§ã³ã«(3)ã®ãã«ãè¨å®ãã¡ã¤ã«ã-tãªãã·ã§ã³ã§ãä½æããã³ã³ããã¤ã¡ã¼ã¸ã«ä»ä¸ããã¿ã°åãæå®ãã¦ãã«ããã¦ãã¾ãã
$ pip install --user ansible-builder $ ansible-builder build -f custom_ee.yml -t custom_ee:2.11 Running command: docker build -f context/Dockerfile -t custom_ee:2.11 context Complete! The build context can be found at: /home/hsaito/work/testing/ansible-builder/custom_ee/context $ docker images REPOSITORY TAG IMAGE ID CREATED SIZE custom_ee 2.11 3c5ad277f944 19 seconds ago 780MB
ä½æããã¤ã¡ã¼ã¸ã¯ãAnsible Navigatorããå©ç¨ã§ãã¾ããå ç¨ã使ããã§å©ç¨ããAnsible Navigatorã®ãã£ã¬ã¯ããªã§ãå®éã«ã«ã¹ã¿ã ã¤ã¡ã¼ã¸ãå©ç¨ãã¦Playbookãå®è¡ãã¦ã¿ã¾ãããã(3)ã§ã¯ãã¼ã¹ã¤ã¡ã¼ã¸(build_arg_defaults.EE_BASE_IMAGE)ã¨ãã¦ãAnsible Core 2.11ãæå®ãã¦ãã¾ãã
ä»åã¯ã--eeiãªãã·ã§ã³ã§ãã«ã¹ã¿ã ã¤ã¡ã¼ã¸ã®ã¿ã°ãæå®ãã¦ãã¾ããã¾ããAnsible Coreã®ãã¼ã¸ã§ã³ã2.11ã§ãããã¨ã確èªããããã«-vvãªãã·ã§ã³ããããã¦æå®ãã¦ãã¾ãã
$ ansible-navigator run --eei custom_ee:2.11 playbook/test.yml -i inventory/hosts -vv ansible-playbook [core 2.11.7.post0] config file = None configured module search path = ['/home/runner/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules'] ansible python module location = /usr/local/lib/python3.8/site-packages/ansible ansible collection location = /home/runner/.ansible/collections:/usr/share/ansible/collections executable location = /usr/local/bin/ansible-playbook python version = 3.8.8 (default, Aug 25 2021, 16:13:02) [GCC 8.5.0 20210514 (Red Hat 8.5.0-3)] jinja version = 2.10.3 libyaml = True No config file found; using defaults Skipping callback 'awx_display', as we already have a stdout callback. Skipping callback 'default', as we already have a stdout callback. Skipping callback 'minimal', as we already have a stdout callback. Skipping callback 'oneline', as we already have a stdout callback. PLAYBOOK: test.yml ************************************************************* 1 plays in /home/hsaito/work/testing/ansible-navigator/playbook/test.yml PLAY [all] ********************************************************************* META: ran handlers TASK [ping] ******************************************************************** task path: /home/hsaito/work/testing/ansible-navigator/playbook/test.yml:6 ok: [192.168.125.110] => {"ansible_facts": {"discovered_interpreter_python": "/usr/libexec/platform-python"}, "changed": false, "ping": "pong"} ok: [192.168.125.112] => {"ansible_facts": {"discovered_interpreter_python": "/usr/libexec/platform-python"}, "changed": false, "ping": "pong"} ok: [192.168.125.111] => {"ansible_facts": {"discovered_interpreter_python": "/usr/libexec/platform-python"}, "changed": false, "ping": "pong"} META: ran handlers META: ran handlers PLAY RECAP ********************************************************************* 192.168.125.110 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 192.168.125.111 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 192.168.125.112 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
ã¾ã¨ã
Ansible Navigatorã¯ãAnsibleããã¸ã§ã¯ãã®ä¸ã§ãã2021å¹´ã«ç«ã¡ä¸ããããæãè¥ãããã¸ã§ã¯ãã§ããã¾ã ã¾ã ãããã¯ã·ã§ã³ç°å¢ã§ã®éç¨ã«ã¯èããªãé¨åãããã¾ããã2022å¹´ãå®å®æ§ã®åä¸ãæ©è½æ¡å¼µãé²ãããã¦ãããã¨ã§ãããã
ä»åãç´¹ä»ããã®ã¯CLIã§ã®å©ç¨ä¾ã§ãããããªãããªããã¹ããã¼ã¹ã®ã¦ã¼ã¶ã¤ã³ã¿ã¼ãã§ã¤ã¹ãæä¾ãã¦ãã¾ãã2022å¹´ã¯ãAnsible Navigatorãæä¾ãããã¤ããªãããªPlaybookå®è¡ç°å¢ã®å©ç¨ãé²ããã®ã¨äºæ³ãã¦ãã¾ãããã®è¨äºããã¿ãªããã®2022å¹´ã®æ´»åã®ãå½¹ã«ç«ã¦ã°å¹¸ãã§ãã
ããã§ã¯ã¿ãªããã2022å¹´ãHappy Automation! :)