Ansible Playbook Snippets/Scripts

Delete File

Delete Local

- name: Delete uploaded file at ansible centralize data location
  file:
    path: "/tmp/directory/filename.extension"
    state: absent
  delegate_to: localhost

Delete Remote

- name: Delete uploaded file at target machine
  win_file:
    path: "C:\temp\abc.zip"
    state: absent

 

Upload File to Remote Directory

- name: Upload program file to target machine
  win_copy:
    src: "/tmp/setup.exe"
    dest: "{{targetlocation}}\\" # or "C:\\temp\\

 

Set Fail Msg

Set fail message when directory does not exist

- name: Check if src directory exist in target machine
  win_stat:
    path: "{{ dirtoscan }}"
  register: srcdir_data
- name: Skip if src directory exist
  fail:
    msg: "Directory {{dirtoscan}} do not exist in {{inventory_hostname}}"
  when: not srcdir_data.stat.exists  

 

Reboot Windows Server

- name: Reboot server
  win_reboot:
    connect_timeout: 1200
    reboot_timeout: 600 # Gives 5min to reboot
  register: rebootlog
  ignore_errors: yes