간단한 Ansible 플레이북 생성 및 Python 인터프리터 구성
이제 Python 인터프리터를 확인하는 방법을 이해했으므로 간단한 플레이북을 만들고 Python 인터프리터를 명시적으로 구성하는 방법을 알아보겠습니다.
기본 플레이북 생성
다양한 Python 인터프리터 구성을 테스트하는 데 사용할 간단한 Ansible 플레이북을 만들어 보겠습니다.
- WebIDE 에서
test_playbook.yml이라는 새 파일을 만듭니다.
- 파일에 다음 내용을 추가합니다.
---
- name: Test Python Interpreter
hosts: local
gather_facts: yes
tasks:
- name: Display Python version
debug:
msg: "Python interpreter is {{ ansible_python_interpreter | default('/usr/bin/python3') }} with version {{ ansible_python_version }}"
- name: Create a test file
file:
path: "~/python_info.txt"
state: touch
- name: Write Python info to file
lineinfile:
path: "~/python_info.txt"
line: "Ansible used Python interpreter: {{ ansible_python_interpreter | default('/usr/bin/python3') }} with version {{ ansible_python_version }}"
이 플레이북은 다음을 수행합니다.
- 사용 중인 Python 인터프리터 경로 및 버전을 표시합니다.
- 홈 디렉토리에 텍스트 파일을 만듭니다.
- 해당 파일에 Python 인터프리터 정보를 씁니다.
기본 설정으로 플레이북 실행
기본 설정으로 이 플레이북을 실행해 보겠습니다.
ansible-playbook -i inventory.ini test_playbook.yml
다음과 유사한 출력을 볼 수 있습니다.
PLAY [Test Python Interpreter] *****************************
TASK [Gathering Facts] *************************************
ok: [localhost]
TASK [Display Python version] ******************************
ok: [localhost] => {
"msg": "Python interpreter is /usr/bin/python3 with version 3.10.10"
}
TASK [Create a test file] **********************************
changed: [localhost]
TASK [Write Python info to file] ***************************
changed: [localhost]
PLAY RECAP ************************************************
localhost : ok=4 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
이는 Ansible 이 시스템 Python 인터프리터를 사용하고 있음을 확인하며, 이는 일반적으로 로컬 작업에 적합합니다.
Python 인터프리터 명시적으로 설정
이제 다양한 방법으로 Python 인터프리터를 명시적으로 설정하는 방법을 알아보겠습니다.
방법 1: 인벤토리 파일에서 설정
- Python 인터프리터 변수를 추가하여
inventory.ini 파일을 업데이트합니다.
[local]
localhost ansible_connection=local ansible_python_interpreter=/usr/bin/python3
- 플레이북을 다시 실행합니다.
ansible-playbook -i inventory.ini test_playbook.yml
출력은 이전과 유사해야 하며, 지정된 Python 인터프리터가 사용되고 있음을 확인합니다.
방법 2: 플레이북에서 설정
- 플레이 수준에서 Python 인터프리터 변수를 포함하도록
test_playbook.yml을 업데이트합니다.
---
- name: Test Python Interpreter
hosts: local
gather_facts: yes
vars:
ansible_python_interpreter: /usr/bin/python3
tasks:
- name: Display Python version
debug:
msg: "Python interpreter is {{ ansible_python_interpreter }} with version {{ ansible_python_version }}"
- name: Create a test file
file:
path: "~/python_info_2.txt"
state: touch
- name: Write Python info to file
lineinfile:
path: "~/python_info_2.txt"
line: "Ansible used Python interpreter: {{ ansible_python_interpreter }} with version {{ ansible_python_version }}"
- 업데이트된 플레이북을 실행합니다.
ansible-playbook -i inventory.ini test_playbook.yml
출력이 Python 인터프리터가 올바르게 설정되었음을 확인하는지 확인합니다.
방법 3: 명령줄을 통해 설정
플레이북을 실행할 때 Python 인터프리터를 설정할 수도 있습니다.
ansible-playbook -i inventory.ini test_playbook.yml -e "ansible_python_interpreter=/usr/bin/python3"
이 접근 방식은 단일 실행에 대한 설정을 일시적으로 재정의하는 데 유용합니다.
결과 확인
어떤 Python 인터프리터가 사용되었는지 확인하기 위해 생성한 파일의 내용을 확인해 보겠습니다.
cat ~/python_info.txt
cat ~/python_info_2.txt
두 파일 모두 Python 3 인터프리터를 사용하고 있음을 표시해야 합니다.