Ansible Structure Folder and Files - thopper91/Ansible-on-Windows GitHub Wiki

Here documented is the layout to both folder and file structure to the repository. It states how the variables need to be declared, the configuration file, the role playbooks themselves and the folder structure.

Repository structure

There are many ways to structure the Ansible repository, however this is the form I found worked well (when using native Ansible):

  • Inventory folder (holds hosts file)
  • Roles folder (all playbooks)
  • ansible.cfg (sets default parameters)
  • config.json (sets configuration details)
  • TASK1.yml (Playbook to run specific set of tasks within the roles folder)
  • TASK2.yml (Playbook to run specific set of tasks within the roles folder)

CONFIG FILE:

The configuration file within this repository needs to be altered depending on your own environment but below is a skeleton file content to edit:

{
    "ip": "0.0.0.1",
    "credentials": {
        "userName":"NAME",
        "password":"PASSWORD"
        },
}

Remember this needs to be named as 'config.json' on the route level to our repository. The IP can be used instead of hostname.

Hosts file (within Inventory folder)

Within this file, multiple hosts and groups of hosts can be added. They hold all ip/hostnames required to complete these tasks. Example:

[localhost]
127.0.0.1 ansible_connection=local

VARIABLES:

If needing a variable file, ensure a 'vars' folder has been created in each folder on the same level as the 'tasks' folder (so roles > pick a role > HERE). The actual variable files are here due to the security level of the repository, but as a further example, the layout to a variable file is here:

---
user: 'hoppert'
Tool: 'putty-0.70.msi'

So the variable name first and within the single quote marks, the contents of that variable. This file needs to be called 'main.yml' otherwise Ansible will not read it.

Roles > tasks playbooks

Now these are very simple playbooks, they only require the tasks that are going to be executed. So as an example:

---
- name: Download "{{ Tool }}"
  win_get_url:
    url: http://URL/{{ Tool }}
    dest: C:\Users\{{ user }}\Documents\{{ Tool }}

This has been taken from the "Install MSI" role and that is literally all that is needed to download a piece of software. We have assigned the task a name and the work we want it to do directly under. In this case we want Ansible to download a piece of software from one URL to a specific destination whilst using variables to declare multiple options. Just like the variable file, this file also needs to be named 'main.yml' otherwise it will not be read.

Executable playbooks

All playbooks stored in this repository are executable if populated correctly. The layout of this repository allows us to break down the tasks individually and have a 'higher playbook' (as I have been referencing) to actually execute the tasks. In this playbook, we need to ensure there 3 are within it to successfully run:

  1. hosts
  2. become (similar to sudo, gives us admin privileges)
  3. roles (the tasks themselves) Here is a copy of "Install_MSI.yml" playbook:
---
- hosts: windows
  connection: local
  roles:
    - {role: Install MSI's}

Notice we have not named it 'main.yml', this allows us to create specific tasks with multiple roles

Alternative File layout

The folder structure in this project is exactly how it should be. If preferred to have ALL the ansible code in 1 playbook, you require to have those playbooks at this level and for the playbook to look something similar to the below. It is worth noting, splitting them as I have is seen as best practice:

---
- hosts: windows #The hosts file in inventory
  vars:
    Tool: 'test.msi'
    user: 'hoppert'
  tasks:
    - name: Install "{{ Tool }}" and wait for it to complete
      win_msi:
        path: C:\Users\{{ user }}\Documents\{{ Tool }}
        wait: yes