Picking the right module

If you were running Ansible against a Linux server, and wanted to create a directory and then copy a file into it, you would use the file and copy Ansible modules, in a playbook that looks something like the following:

---
- name: Linux file example playbook
hosts: all
gather_facts: false

tasks:
- name: Create temporary directory
file:
path: /tmp/mastery
state: directory
- name: Copy across a test file
copy:
src: mastery.txt
dest: /tmp/mastery/mastery.txt

However, on Windows, this playbook would fail to run, as the file and copy modules are not compatible with WinRM. As a result, an equivalent playbook to perform the same task, but on Windows, would look like this:

---
- name: Windows file example playbook
hosts: all
gather_facts: false

tasks:
- name: Create temporary directory
win_file:
path: 'C:Mastery Test'
state: directory
- name: Copy across a test file
win_copy:
src: ~/src/mastery/mastery.txt
dest: 'C:Mastery Testmastery.txt'

Note the following differences between the two playbooks:

  • win_file and win_copy are used in place of the file and copy modules for Windows.
  • It is recommended in the documentation for the win_file and win_copy modules to use a backslash () when dealing with remote (Windows paths).
  • Continue to use forward slashes (/) on the Linux host.
  • Use single quotes (not double quotes) to quote paths that contain spaces.

It is always important to consult the documentation for the individual modules used in your playbooks. For example, reviewing the documentation for the win_copy module documentation, it recommends using the win_get_url module for large file transfers because the WinRM transfer mechanism is not very efficient.

Also note that, if a filename contains certain special characters (for example, square braces), they need to be escaped using the PowerShell escape character, `. For example, the following task would install the c: empsetupdownloader_[aaff].exe file:

  - name: Install package
win_package:
path: 'c: empsetupdownloader_`[aaff`].exe'
product_id: {00000000-0000-0000-0000-000000000000}
arguments: /silent /unattended
state: present

There are many other Windows modules that should suffice to complete your Windows playbook needs, and, combined with these tips, you would get the end results you need, quickly and with ease.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.142.40.43