본문 바로가기
인프라/앤서블(Ansible)

[ansible] 앤서블 윈도우 서버 파일 배포 실행하기

by IT맥구리나스 2023. 9. 21.

다수의 윈도우 서버에 배치파일이나 파워쉘스크립트를 배포하고

배포한 파일을 실행시킬 수 있다.

취약점점검파일을 배포 및 실행할 때 유용하게 사용할 수 있다.

윈도우 파일 배포는 win_copy 모듈을 사용했고

배포된 파일 실행은 win_shell 모듈을 사용하였다.

기본적으로 win_shell 모듈은 배치스크립트 실행이며, powershell 명령어나 파일을 실행하려면 앞에 powershell.exe를 붙여줘야한다.

 

exec_status 변수를 추가하여 단순히 파일 배포만 하거나

파일 배포 후 스크립트파일 실행을 할 수 있도록 when으로 조건문을 추가하였다.

---
- name: win_copy module demo
  hosts: all
  become: false
  gather_facts: false
  vars:
    ansible_port: 5985
    ansible_connection: winrm
    ansible_winrm_scheme: http
    ansible_winrm_transport: basic
  tasks:
    - name: copy report.bat
      win_copy:
        src: "{{ file_name }}"
        dest: "{{ dest }}"
    - name: run test.bat
      win_shell: powershell.exe "{{ dest }}"
      args:
        executable: cmd
      when: exec_status == "yes"

 

목적지인 윈도우 서버로 파일 주소 기재시 역슬래쉬(\)는 두번 기재한다.

그렇지 않으면 아래와 같은 에러가 발생한다.

 

예시 ) C:\\test\\test.txt

An exception occurred during task execution. To see the full traceback, use -vvv. The error was:    at Microsoft.PowerShell.Commands.FileSystemProvider.ItemExists(String path, ErrorRecord& error)
반응형

댓글