2.2. 네트워크 Ansible Playbook 실행
매일 특정 명령을 실행하려면 ansible 대신 ansible-playbook
을 사용하여 플레이북에 저장하고 실행할 수 있습니다. 플레이북은 명령줄에 플래그와 함께 제공한 많은 매개변수를 저장할 수 있으므로 명령줄에 입력하는 것은 적습니다. 플레이북과 인벤토리 파일이라는 두 개의 파일이 필요합니다.
사전 요구 사항
플레이북은 다음과 같습니다.
--- - name: Network Getting Started First Playbook connection: ansible.netcommon.network_cli gather_facts: false hosts: all tasks: - name: Get config for VyOS devices vyos.vyos.vyos_facts: gather_subset: all - name: Display the config debug: msg: "The hostname is {{ ansible_net_hostname }} and the OS is {{ ansible_net_version }}"
---
- name: Network Getting Started First Playbook
connection: ansible.netcommon.network_cli
gather_facts: false
hosts: all
tasks:
- name: Get config for VyOS devices
vyos.vyos.vyos_facts:
gather_subset: all
- name: Display the config
debug:
msg: "The hostname is {{ ansible_net_hostname }} and the OS is {{ ansible_net_version }}"
레이블 | 설명 |
---|---|
|
Ansible의 기본 팩트 수집( |
플레이북은 위의 명령줄에서 7개의 값 중 세 가지를 설정합니다.
-
그룹(
호스트: all
) -
연결 방법(
Connection: ansible.netcommon.network_cli
) 및 - 모듈(각 작업에 있음).
플레이북에 해당 값을 설정하면 명령줄에서 해당 값을 생략할 수 있습니다. 플레이북은 구성 출력을 표시하는 두 번째 작업도 추가합니다.
vyos.vyos_facts 또는
모듈을 통해 시스템에서 팩트를 수집할 때 수집된 데이터는 콘솔에 기록되는 대신 향후 작업에서 사용하기 위해 메모리에 보관됩니다.
ansible.builtin.setup
과 같은 컬렉션 관련 팩트
모듈이 플레이북에서 실행되면 콘솔에 쓰는 대신 향후 작업에서 사용할 수 있도록 출력이 메모리에 유지됩니다. 대부분의 다른 모듈에서는 모듈 또는 작업의 출력을 저장하고 재사용할 변수를 명시적으로 등록해야 합니다.
팩트에 대한 자세한 내용은 Ansiible Playbook 참조 가이드의 [Ansible facts]를 참조하십시오.
다음 디버그 작업을 사용하면 쉘에 결과를 볼 수 있습니다.
프로세스
다음 명령을 사용하여 플레이북을 실행합니다.
Copy to Clipboard Copied! Toggle word wrap Toggle overflow ansible-playbook -i vyos.example.net, -u ansible -k -e ansible_network_os=vyos.vyos.vyos first_playbook.yml
ansible-playbook -i vyos.example.net, -u ansible -k -e ansible_network_os=vyos.vyos.vyos first_playbook.yml
플레이북에는 두 개의 작업이 있는 하나의 플레이가 포함되어 있으며 다음과 같은 출력을 생성합니다.
Copy to Clipboard Copied! Toggle word wrap Toggle overflow ansible-playbook -i vyos.example.net, -u ansible -k -e ansible_network_os=vyos.vyos.vyos first_playbook.yml
$ ansible-playbook -i vyos.example.net, -u ansible -k -e ansible_network_os=vyos.vyos.vyos first_playbook.yml PLAY [Network Getting Started First Playbook] *************************************************************************************************************************** TASK [Get config for VyOS devices] *************************************************************************************************************************** ok: [vyos.example.net] TASK [Display the config] *************************************************************************************************************************** ok: [vyos.example.net] => { "msg": "The hostname is vyos and the OS is VyOS 1.1.8" }
- 이제 장치 구성을 검색할 수 있으므로 Ansible로 업데이트를 시도할 수 있습니다.
첫 번째 플레이북의 확장 버전인
first_playbook_ext.yml
을 여기에서 다운로드합니다.플레이북은 다음과 같습니다.
Copy to Clipboard Copied! Toggle word wrap Toggle overflow --- - name: Network Getting Started First Playbook Extended connection: ansible.netcommon.network_cli gather_facts: false hosts: all tasks: - name: Get config for VyOS devices vyos.vyos.vyos_facts: gather_subset: all - name: Display the config debug: msg: "The hostname is {{ ansible_net_hostname }} and the OS is {{ ansible_net_version }}" - name: Update the hostname vyos.vyos.vyos_config: backup: yes lines: - set system host-name vyos-changed - name: Get changed config for VyOS devices vyos.vyos.vyos_facts: gather_subset: all - name: Display the changed config debug: msg: "The new hostname is {{ ansible_net_hostname }} and the OS is {{ ansible_net_version }}"
--- - name: Network Getting Started First Playbook Extended connection: ansible.netcommon.network_cli gather_facts: false hosts: all tasks: - name: Get config for VyOS devices vyos.vyos.vyos_facts: gather_subset: all - name: Display the config debug: msg: "The hostname is {{ ansible_net_hostname }} and the OS is {{ ansible_net_version }}" - name: Update the hostname vyos.vyos.vyos_config: backup: yes lines: - set system host-name vyos-changed - name: Get changed config for VyOS devices vyos.vyos.vyos_facts: gather_subset: all - name: Display the changed config debug: msg: "The new hostname is {{ ansible_net_hostname }} and the OS is {{ ansible_net_version }}"
- 확장 첫 번째 플레이북에는 단일 플레이에 5개의 작업이 있습니다.
다음 명령을 사용하여 플레이북을 실행합니다.
Copy to Clipboard Copied! Toggle word wrap Toggle overflow ansible-playbook -i vyos.example.net, -u ansible -k -e ansible_network_os=vyos.vyos.vyos first_playbook_ext.yml
$ ansible-playbook -i vyos.example.net, -u ansible -k -e ansible_network_os=vyos.vyos.vyos first_playbook_ext.yml
출력에는 구성에 대한 Ansible 변경 사항이 표시됩니다.
Copy to Clipboard Copied! Toggle word wrap Toggle overflow ansible-playbook -i vyos.example.net, -u ansible -k -e ansible_network_os=vyos.vyos.vyos first_playbook_ext.yml
$ ansible-playbook -i vyos.example.net, -u ansible -k -e ansible_network_os=vyos.vyos.vyos first_playbook_ext.yml PLAY [Network Getting Started First Playbook Extended] ************************************************************************************************************************************ TASK [Get config for VyOS devices] ********************************************************************************************************************************** ok: [vyos.example.net] TASK [Display the config] ************************************************************************************************************************************* ok: [vyos.example.net] => { "msg": "The hostname is vyos and the OS is VyOS 1.1.8" } TASK [Update the hostname] ************************************************************************************************************************************* changed: [vyos.example.net] TASK [Get changed config for VyOS devices] ************************************************************************************************************************************* ok: [vyos.example.net] TASK [Display the changed config] ************************************************************************************************************************************* ok: [vyos.example.net] => { "msg": "The new hostname is vyos-changed and the OS is VyOS 1.1.8" } PLAY RECAP ************************************************************************************************************************************ vyos.example.net : ok=5 changed=1 unreachable=0 failed=0