데이터 엔지니어링 정복/Ansible & AWX
[Ansible&AWX] when, with_items, loop, debug 모듈을 사용한 간단한 예제
nooh._.jl
2025. 2. 6. 16:21
728x90
반응형
환경
centos7 서버
awx docker-compose로 실행중
목표: 서버에 유저를 생성한 뒤 생성된 유저목록을 출력하고, 생성된 유저들을 지우는 workflow template을 만들기
현재 awx-gitlab 프로젝트 연동된 상태
깃랩 프로젝트 클론한 뒤 새로운 브랜치로 가서 아래 파일들을 만들어준다.
* create_users.yaml
---
- name: Create Users Playbook
hosts: localhost
vars:
users_list:
- alice
- bob
- charlie
tasks:
- name: Create users
user:
name: "{{ item }}"
state: present
with_items: "{{ users_list }}"
register: user_creation_results
- name: Print the result of user creation
debug:
msg: "User {{ item.item }} creation status: {{ item.changed }}"
with_items: "{{ user_creation_results.results }}"
- name: Set fact for successful Users
set_fact:
successful_users: "{{ successful_users | default([]) + [item.item] }}"
with_items: "{{ user_creation_results.results }}"
- name: Display successful Users
debug:
msg: "Successfully created users: {{ successful_users }}"
when: successful_users | length > 0
- name: Save successful users to a JSON file
copy:
content: "{{ successful_users | to_json }}"
dest: successful_users.json
* remove_users.yaml
---
- name: Remove Users Playbook
hosts: localhost
gather_facts: no
become: true # Ensures access to /root/successful_users.json
tasks:
- name: Check if successful_users.json exists
stat:
path: /root/successful_users.json
register: json_file_status
- name: Load successful users from JSON file
slurp:
src: /root/successful_users.json
register: json_file_content
when: json_file_status.stat.exists
- name: Parse JSON content
set_fact:
successful_users: "{{ json_file_content['content'] | b64decode | from_json }}"
when: json_file_status.stat.exists
- name: Ensure users are defined before proceeding
fail:
msg: "No users found to remove!"
when: successful_users is not defined or successful_users | length == 0
- name: Remove users created previously
ansible.builtin.user:
name: "{{ item }}"
state: absent
loop: "{{ successful_users }}"
register: user_removal_results
ignore_errors: true
- name: Set fact for successfully removed users
set_fact:
removed_users: "{{ user_removal_results.results | selectattr('changed', 'equalto', true) | map(attribute='item') | list }}"
- name: Display removed users
debug:
msg: "Successfully removed users: {{ removed_users }}"
when: removed_users | length > 0
- name: Remove the JSON file after processing
ansible.builtin.file:
path: /root/successful_users.json
state: absent
when: removed_users | length > 0
그리고 아래와 같이 gitlab 원격레포에 푸시한다.
$ git status On branch 0205_playbook_example Untracked files: (use "git add <file>..." to include in what will be committed) 00_SIMPLE_COMMAND/examples/ nothing added to commit but untracked files present (use "git add" to track) $ git add 00_SIMPLE_COMMAND/examples/ $ git commit -m "0206 studying of making playbook" $ git push origin 0205_playbook_example |
원격레포에서 머지 리퀘스트 후 머지해주고, 로컬 메인에도 풀 해준다.
그런 뒤 AWX - Projects에서 해당 깃랩프로젝트 sync해준다.
그리고 templates에서 각 플레이북 하나씩 job template을 아래처럼 만들어준다.
일단 아래처럼 작성해준다.
만약 디버깅하고 싶다면 Verbosity를 1로 둬서 디버깅모드로 실행할 수 있다.
2개 playbook을 만들면 아래와 같다.
이제 각각 실행해서 정상적으로 동작하는지 확인한다.
이제 workflow template을 만들어준다.
start버튼을 눌러서 처음으로 시작할 job template을 선택한다.
그리고 + 버튼을 눌러서 성공시에 다음으로 올 잡템플릿을 정해준다.
마지막으로 save를 눌러주고, 실행시켜본다.
성공하면 아래와 같다.
728x90
반응형