ãã®è¨äºã¯ããAnsible Advent Calendar 2022ãã®17æ¥ç®ã®è¨äºã§ãã
çæ§ããããéããã§ãããããã¬ããããããã¯ãã«ã«ãµãã¼ãã®åã¨ç³ãã¾ããæ®æ®µã¯ Ansible 㨠RHEL(主㫠Services )ã®ãµãã¼ãããã¦ãã¾ãã
ä»åã¯ãå©ç¨ãããæ©è½ã Ansible ã¢ã¸ã¥ã¼ã«ã«è¦å½ãããªãã¨ãã£ãæã«ãããã工夫ããä¸ä¾ã¨ãã¦ãansible.builtin.uri ã¢ã¸ã¥ã¼ã«ãä¾ã«ããªãã触ãã¦ã¿ããã¨æãã¾ãã
Ansible ã¯æ¬è¨äºã®ä½ææç¹ï¼2022å¹´12æ17æ¥ï¼ã§å®ã« 3300+ ãã®ã¢ã¸ã¥ã¼ã«ãæã¡ã¾ãããå ¨ã¦ã®è£½å ã®å ¨ã¦ã®æ©è½ãç¶²ç¾ ãã¦ããããã§ã¯ãªããããå¿ è¦ã ã¨æãããæ©è½ãããã°ãã®é½åº¦ãRFE ãä¸ãã¦å®è£ ã®ãªã¯ã¨ã¹ããåºãäºã«ãªãã¾ãã
Ansible Collections ã®ãªãã¸ããªã¯ãã¡ãã«ãªãã¾ãã
ä¾ãã°ãcisco.nxos ã® collection ã§è¿½å ãã¦ã»ããæ©è½çãããã°ã以ä¸ã®ãªãã¸ããªã® issue ãã RFE ãä½æããäºã«ãªãã¾ãã
å°ãRFEã¯å¿ ãæ¿èªããã¦å®è£ ããããã®ã§ã¯ããã¾ãããã¾ããå®è£ ãããã¨ãã¦ããã®ææçã¯ä¸æ確ãªã®ã§ããã®éã¯ããã®ãªã¯ã¨ã¹ãããæ©è½ã« ã¤ãã¦ã¯ãä½ãããã®ä»£æ¿æ段ã§å·¥å¤«ãã¦ãããã¨ã«ãªãã¾ã(ãã¡ãããèªãå®è£ ã㦠pull request ãéã£ã¦ãã¾ãã®ãã¢ãªã ã¨æãã¾ãï¼)ã
ãã®ä»£æ¿æ段ã«ã¤ãã¦ã¯ããåãåãããããã ããéã«ã¯ã§ããç¯å²ã§ãç´¹ä»ããã¦ããã ãã¦ããã¾ãã
ä»åããã®ä¸ä¾ã ansible.builtin.uri ã¢ã¸ã¥ã¼ã«ã«ãªãã¾ãã REST API ãç¨ãã¦ç®¡çããããã¼ãããµã¼ãã¹ã§ãç¾è¡ã® Ansible ã¢ã¸ã¥ã¼ã«ãå©ç¨ãããæ©è½ãæä¾ãã¦ããªãå ´åã¯ãã®ã¢ã¸ã¥ã¼ã«ãæ´»ç¨ããäºãèããããæ段ã®ä¸ã¤ã¨ãªãã¾ãã
ããããã¯ãå®éã®å©ç¨ä¾ã®ç´¹ä»ã¨ãªãã¾ããã¾ãä»å㯠controller.group ã¢ã¸ã¥ã¼ã«ãä¾ã«ãã¦ã¿ã¾ããã æ¬è¨äºã®ä½ææç¹ã§ã¯ãcontroller.group ã¢ã¸ã¥ã¼ã«ã§ã¤ã³ãã³ããªã®ã°ã«ã¼ããããã¹ãã®é¢é£ä»ã ã解é¤ããããã«ã¯ãé¢é£ã¥ãã¦ããããå ¨ã¦ã®ãã¹ããè¨è¿°ããå¿ è¦ãããã¾ãã console.redhat.com
ä¾ãã°ä»¥ä¸ã®æ§ãªãã¬ã¤ããã¯ã«ãªãã¾ããããã§ã¯ãAnsible Automation Controller(â»ä»¥éã³ã³ããã¼ã©ï¼ã«åå¨ããã¤ã³ãã³ããªãtest inventory ãã«åå¨ããã¤ã³ãã³ã㪠ã°ã«ã¼ããtest groupãã«ãã¹ããtest1ããtest2ããåå¨ããç¶æ ãããtest2 ã®ã¿ã®é¢é£ä»ãã解é¤ããã¿ã¹ã¯ã«ãªãã¾ãã
â»ã³ã³ããã¼ã©ã«å¿ è¦ãªæ¥ç¶æ å ±ã¯ controller_info.ymlãã¡ã¤ã«ã«å¤æ°ã¨ãã¦å ¨ã¦å®ç¾©ããã¤ã³ãã¼ããã¦ãã¾ãã
--- - hosts: localhost gather_facts: no tasks: - name: Include vars ansible.builtin.include_vars: file: controller_info.yml - name: Disassociate "test2" from "test group" of "test inventory" ansible.controller.group: controller_host: "{{ CONTROLLER_URL }}" controller_username: "{{ USERNAME }}" controller_password: "{{ PASSWORD }}" controller_oauthtoken: "{{ TOKEN }}" inventory: "inventory test" name: "test group" hosts: - test1 state: present validate_certs: no preserve_existing_hosts: no
ä¸è¨ã®ä¾ã§ã¯ãtest group ã«åå¨ãã¦ãã¦ã»ãããã¹ãã test1 ã®ã¿ã®ãããç¹ã«ä¸ä¾¿ã¯ããã¾ãããããããå¤æ°ããå ´åã¯ãã®å ¨ã¦ãè¨è¼ããå¿ è¦ãããã¾ãã ä¾ãã°ã¯ã©ã¦ãç°å¢çããã¤ãããã¯ã¤ã³ãã³ããªã使ã£ã¦ç®¡çãã¦ããå ´åã¯ãé½åº¦ãªã¹ããç¨æããã®ãæéã«ãªãäºãèãããã¾ãã ããããªããæ¬è¨äºä½ææç¹ã§ãã°ã«ã¼ãã«åå¨ãã¦ãããã¹ãã®ãªã¹ããåå¾ããæ©è½ã controller.group ã¢ã¸ã¥ã¼ã«ã«ã¯ããã¾ããã
ããã§ãansible.builtin.uri module ã使ã£ã¦é¢é£ä»ãã®è§£é¤ãè¡ãåã®ãã¹ãã®ãªã¹ããä¸åº¦åå¾ããããããåé¤ãããã¹ãã®ã¿ãé¸æããã°æ¸ãã¿ã¹ã¯ãç¨æãã¾ããã â»community.general ã³ã¬ã¯ã·ã§ã³ãå¿ è¦ã¨ãªãã¾ã
---
- hosts: localhost
gather_facts: no
tasks:
- name: Include vars
ansible.builtin.include_vars:
file: controller_info.yml
no_log: true
- name: Gathering a host list # ã¿ã¼ã²ããã¨ãªãã°ã«ã¼ãã®ãã¹ãã®æ
å ±ãåå¾ãã
ansible.builtin.uri:
url: "{{ CONTROLLER_HOST + '/api/v2/groups/' + GROUP_ID + '/all_hosts/' }}"
method: GET
url_username: "{{ CONTROLLER_USERNAME }}"
url_password: "{{ CONTROLLER_PASSWORD }}"
force_basic_auth: yes
headers:
Content-Type: "application/json"
validate_certs: false
register: result
- name: Define a list of all hosts # ãã¹ãã®ãªã¹ãã®ã¿ãåãåºã
ansible.builtin.set_fact:
host_list: "{{ result | community.general.json_query('json.results[*].name') | list }}"
- name: Make a host list that should exist only. # é¢é£ä»ãã解é¤ããããã¹ãã®ã¿ãé¤å¤ãããªã¹ããä½æãã
ansible.builtin.set_fact:
host_list: "{{ host_list | reject('search', REMOVED_HOSTNAME) }}"
- name: Dissociate ec2 host from the "{{ GROUP_NAME }}" # æå¾ã«ãé¢é£ä»ãã解é¤ããããã¹ããå«ã¾ããªããªã¹ãã渡ã
ansible.controller.group:
controller_host: "{{ CONTROLLER_HOST }}"
controller_username: "{{ CONTROLLER_USERNAME }}"
controller_password: "{{ CONTROLLER_PASSWORD }}"
controller_oauthtoken: "{{ CONTROLLER_OUTHTOKEN }}"
inventory: "{{ INVENTORY_NAME }}"
name: "{{ GROUP_NAME }}"
hosts: "{{ host_list }}"
state: present
validate_certs: no
preserve_existing_hosts: no
ãã£ã¨é©åãªæ¸ãæ¹ãããããããã¾ããããä¸è¨ã®ã¿ã¹ã¯ããµã³ãã«ã«ãªãã¾ãã
ã¾ã¨ã
ä»åã¯ãå©ç¨ãããæ©è½ã Ansible ã¢ã¸ã¥ã¼ã«ã«ç¡ãå ´åã«å·¥å¤«ããæ¹æ³ã®ä¸ä¾ãç´¹ä»ããã¦ããã ãã¾ããã å°ã è¤éãªãã¬ã¤ããã¯ãæ¸ãå¿ è¦ãå ´åã«ãã£ã¦ã¯åºã¦ãã¦ãã¾ãããããã¾ãããããé©åãªã¢ã¸ã¥ã¼ã«ãç¡ããã...ã§ããªãã¨ãããªããã°ï¼ã ã¨ããå ´åã«ãä»åãç´¹ä»ãã ansible.builtin.uri ã®ãããªãæ±ç¨æ§ã®é«ãã¢ã¸ã¥ã¼ã«ãæ´»ç¨ããã°æå¤ã¨ä½ã¨ããªãã±ã¼ã¹ãå¤ãããããã¾ããã
å°ãã§ããåèã«ãªãæ å ±ã«ãªãã¦ããã°å¹¸ãã§ãã