Running Ansible Playbooks
You'll use Ansible playbooks to build your [Example Corp Active Directory (AD) network](https://github.com/immersive-labs-content/Introductory-Range/wiki/wiki-resources/img/simple_network.jpg). A playbook is simply a YAML file containing a list of tasks to be executed on one more more hosts. Run the playbooks from the Temp-Ansible host. You can run them from your local system, but this isn't recommended as it requires you to have the correct permissions and configurations to run the playbooks.
[!WARNING]
This guide won't cover how to run Ansible playbooks from local systems.
Prerequisites
You can directly [download the released ZIP file from the releases](https://github.com/immersive-labs-content/Introductory-Range/releases/download/v1.0.1/release-v1.0.1.zip) locally and transfer it to the Temp-Ansible host by dragging and dropping the ZIP file onto the instance session. Once you have the ZIP file on the Temp-Ansible host, you can extract its contents using the following command:
```bash
unzip <release-v1.0.1.zip>
```
As you're also managing Ubuntu instances, you'll need to download the private key file from your range panel and copy it to the `/home/ubuntu/Introductory-Range-main/range` directory on Temp-Ansible host. Rename it to `ssh_private_key.pem`.
[!NOTE]
Ensure you provide the correct permission to the private key file by running the command `chmod 600 <private_key_file>`.
Executing playbooks
Playbook tasks are executed from top to bottom, one at a time, against all machines matched by a host pattern. Host pattern matching is done through [`inventory.yml`](https://github.com/immersive-labs-content/Introductory-Range/blob/main/range/inventory.yml).
For example, while preparing and configuring an AD, you'd have to configure a few things like hostnames and opt to choose a Windows server to be promoted to a Domain Controller (DC). The playbook for this example would be:
```yaml
- name: Set Windows hostnames
ansible.builtin.import_playbook: ./windows/misc/hostnames.yml
- name: Set up domain controller
ansible.builtin.import_playbook: ./windows/domain_controller/setting_domain.yml
```
This will tell Ansible to first execute `hostnames.yml` to provide a hostname for all the machines in the network and then execute `setting_domain.yml` to promote a Windows server to a DC respectively.
[!NOTE]
The `playbook.yml` file is the main playbook you need to execute to build the infrastructure. It consists of all the playbooks you need to execute to set up the network infrastructure successfully.
How to run the playbook
You only need to run one file, which is [`playbook.yml`](https://github.com/immersive-labs-content/Introductory-Range/blob/main/range/playbooks/playbook.yml). This file contains all the other playbooks required to build the infrastructure and runs them in order from top to bottom.
Before running the playbook, it's necessary to configure your Ansible environment to prevent premature timeouts and disable host key checking. To achieve this, execute the following commands in your terminal within the directory you're running the playbook:
```
export ANSIBLE_HOST_KEY_CHECKING=False
export ANSIBLE_TIMEOUT=120
```
These commands will disable SSH host key checking and set the Ansible timeout to 120 seconds, to ensure a smoother and uninterrupted playbook execution.
To start provisioning the infrastructure, you need to run the following command from the project's root directory in the `Temp-Ansible` host:
```bash
cd /home/ubuntu/Introductory-Range-main/range
export ANSIBLE_TIMEOUT=120;
export ANSIBLE_HOST_KEY_CHECKING=False
ansible-playbook -i inventory.yml playbooks/playbook.yml
```
To complete manual tasks in Ansible, refer to the Manual-tasks page.
[!TIP]
To get verbose output, add the `-v` flag to your command. For example, `ansible-playbook -i inventory.yml playbooks/playbook.yml -v`.