
오늘 현재 저는 다음과 같은 역할을 맡고 있습니다. 이 역할은 제품의 기본 설치를 시뮬레이션합니다.
- name: Install Server.msi primary_appserver
ansible.windows.win_package:
path: C:\product.msi
log_path: C:\InstallProduct.log
arguments:
ADDLOCAL=DB,Agent
state: present
become: true
become_method: runas
vars:
ansible_become_user: "{{ ansible_user }}"
ansible_become_password: "{{ ansible_password }}"
when: "'primary_appservers' in group_names"
설치 마법사에서 추가 기능을 선택하는 "고급" 설치를 시뮬레이션하고 싶습니다.
설치 마법사에서는 하나 이상의 기능을 선택할 수 있습니다. 즉, ADDLOCAL
argumnet은 다음과 같습니다. ADDLOCAL=DB,Agent
- 기본 OR ADDLOCAL=DB,Agent,Feature_A
ORADDLOCAL=DB,Agent,Feature_A,Feature_B
Feature_C
예를 들어 설치하려면 추가 인수 목록이 필요하기 때문에 상황이 복잡해졌습니다 . RABBIT_LOCAL_PORT
, RABBIT_QUEUE_NAME
, RABBIT_TTL
...
vars
Ansible 또는 Jenkins에서 사용 extraVars
- 플레이북\역할의 값 덮어쓰기
플레이북\역할의 기존 값에 값을 추가할 수 있는 방법이 있습니까? 예를 들어 설치를 선택하거나 Feature_a
역할 Feature_b
의 ADDLOCAL 값이 로 변경됩니까 ADDLOCAL=DB,Agent,Feature_A,Feature_B
? 또는 두 번째 경우에 를 추가하면 역할의 값이 다음으로 변경되고 Feature_C
키 에 , , 인수?가 추가로 포함됩니다.ADDLOCAL
ADDLOCAL=DB,Agent,Feature_C
arguments
RABBIT_LOCAL_PORT
RABBIT_QUEUE_NAME
RABBIT_TTL
답변1
원하는 동작을 구현하는 데는 두 가지 옵션이 있습니다.
인수 변수를 목록으로 처리
인수를 생성하는 동안 인수를 구조(예제에서는 목록 맵)로 처리합니다. 사용 사례에 따라 기능/인수를 추가하거나 제거할 수 있습니다. 하지만 이 접근 방식은 약간의 복잡성을 추가합니다.
- name: set default arguments
set_fact:
arguments_map:
ADDLOCAL:
- feature1
- feature2
- name: set feature3
set_fact:
arguments_map: "{{ arguments_map | combine({'ADDLOCAL':['feature3']}, recursive=True, list_merge='append') }}"
- name: set feature4
set_fact:
arguments_map: "{{ arguments_map | combine({'ADDLOCAL':['feature4'], 'RABBIT_LOCAL_PORT':5672, 'RABBIT_QUEUE_NAME':'test'}, recursive=True, list_merge='append') }}"
- name: generate arguments string
set_fact:
arguments: "{% for argument in arguments_map | dict2items %}{{ argument['key'] }}={{ (argument['value'] | join(',')) if (argument['value'] | type_debug == 'list') else (argument['value']) }} {% endfor %}"
- debug:
var: arguments
그러면 다음 문자열이 생성됩니다.
ADDLOCAL=feature1,feature2,feature3,feature4 RABBIT_LOCAL_PORT=5672 RABBIT_QUEUE_NAME=test
가독성을 위해 사전 정의된 모든 세트를 var 파일로 이동할 수 있습니다.
점차적으로 인수 문자열에 연결
더 간단하지만 유연성이 떨어집니다.
- name: set default arguments
set_fact:
arguments: 'ADDLOCAL=DB,Agent'
- name: set feature1
set_fact:
arguments: "{{ arguments + ',feature1' }}"
- name: set feature2
set_fact:
arguments: "{{ arguments + ',feature2' }}"
- name: set additional arguments
set_fact:
arguments: "{{ arguments + ' RABBIT_LOCAL_PORT=5672 RABBIT_QUEUE_NAME=test' }}"
when: arguments is search('feature2')
- debug:
var: arguments
다음 문자열을 생성합니다.
ADDLOCAL=DB,Agent,feature1,feature2 RABBIT_LOCAL_PORT=5672 RABBIT_QUEUE_NAME=test