5.4.3.2. k8s Ansible 모듈 로컬 테스트
경우에 따라 개발자가 매번 Operator를 실행하고 다시 빌드하는 대신 로컬 시스템에서 Ansible 코드를 실행하는 것이 좋습니다.
프로세스
community.kubernetes
컬렉션을 설치합니다.$ ansible-galaxy collection install community.kubernetes
새 Ansible 기반 Operator 프로젝트를 초기화합니다.
$ operator-sdk new --type ansible \ --kind Test1 \ --api-version test1.example.com/v1alpha1 test1-operator
출력 예
Create test1-operator/tmp/init/galaxy-init.sh Create test1-operator/tmp/build/Dockerfile Create test1-operator/tmp/build/test-framework/Dockerfile Create test1-operator/tmp/build/go-test.sh Rendering Ansible Galaxy role [test1-operator/roles/test1]... Cleaning up test1-operator/tmp/init Create test1-operator/watches.yaml Create test1-operator/deploy/rbac.yaml Create test1-operator/deploy/crd.yaml Create test1-operator/deploy/cr.yaml Create test1-operator/deploy/operator.yaml Run git init ... Initialized empty Git repository in /home/user/go/src/github.com/user/opsdk/test1-operator/.git/ Run git init done
$ cd test1-operator
원하는 Ansible 논리로
roles/test1/tasks/main.yml
파일을 수정합니다. 이 예제에서는 변수의 스위치로 네임스페이스를 생성하고 삭제합니다.- name: set test namespace to "{{ state }}" community.kubernetes.k8s: api_version: v1 kind: Namespace state: "{{ state }}" name: test ignore_errors: true 1
- 1
ignore_errors: true
를 설정하면 존재하지 않는 프로젝트를 삭제하는 데 실패하지 않습니다.
roles/test1/defaults/main.yml
파일을 수정하여state
를 기본적으로present
로 설정합니다.state: present
test1
역할을 포함하는 최상위 디렉터리에 Ansible플레이북 playbook.yml
을 생성합니다.- hosts: localhost roles: - test1
플레이북을 실행합니다.
$ ansible-playbook playbook.yml
출력 예
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all' PLAY [localhost] *************************************************************************** PROCEDURE [Gathering Facts] ********************************************************************* ok: [localhost] Task [test1 : set test namespace to present] changed: [localhost] PLAY RECAP ********************************************************************************* localhost : ok=2 changed=1 unreachable=0 failed=0
네임스페이스가 생성되었는지 확인합니다.
$ oc get namespace
출력 예
NAME STATUS AGE default Active 28d kube-public Active 28d kube-system Active 28d test Active 3s
state
를absent
로 설정하여 플레이북을 재실행합니다.$ ansible-playbook playbook.yml --extra-vars state=absent
출력 예
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all' PLAY [localhost] *************************************************************************** PROCEDURE [Gathering Facts] ********************************************************************* ok: [localhost] Task [test1 : set test namespace to absent] changed: [localhost] PLAY RECAP ********************************************************************************* localhost : ok=2 changed=1 unreachable=0 failed=0
네임스페이스가 삭제되었는지 확인합니다.
$ oc get namespace
출력 예
NAME STATUS AGE default Active 28d kube-public Active 28d kube-system Active 28d