This project contains Ansible examples and a Vagrantfile to create a virtual machine. The connection settings of the examples rely on the default SSH connection settings created by vagrant for the virtualbox virtual machine.
- host: 127.0.0.1
- port: 2222
- ssh private key: $PROJECT_ROOT/.vagrant/machines/default/virtualbox/private_key
- user: vagrant
You can create your own inventory files if you need: Ansible: Working with Inventory
Each example has a "run.sh" file which contains the command you can execute or create your own command based on the example.
Requirements:
- Linux host machine (Tested on Kubuntu 19.04)
- VirtualBox (Optional, Tested with VirtualBox 6.0)
- Vagrant (Optional, Tested with Vagrant 2.2.3)
- Python interpreter (Tested with Python 3)
- Ansible (Tested with Ansible 2.8.4)
Make sure you have python3 and pip3 installed on your machine. Otherwise, install them or use "pip" and "python" instead if you want to try it with your default interpreter. Open a terminal, go to the project root and run the following commands:
sudo apt install python3 python3-pip
pip3 install --user --upgrade pip
The above command can change the path of python if they were already installed. Log out and log in again or just log in using su:
su - $USER
Or source the .bashrc file if you use Bash shell.
source $HOME/.bashrc
Install the virtualenv module:
pip3 install --user virtualenv
And create the virtual environment:
python3 -m virtualenv ansible-examples-env
source ansible-examples-env/bin/activate
You can finally install Ansible:
pip3 install -r requirements.txt
This step is optional but recommended if you don't have your own test machine or you don't know how to configure Ansible to set that as target.
In the project root you will find a simple Vagrantfile to download and start an Ubuntu 18.04 LTS machine.
Open a terminal, navigate to the project root and run the following command:
vagrant up
The examples are in the examples folder. Step into the chosen example folder and execute the run.sh script.
- 01-ad-hoc: The simplest Ad-Hoc command
- 02-ad-hoc-with-inventory: The simplest Ad-Hoc command with an inventory.yml
- 03-playbook: The first and simplest playbook
- 04-playbook-debug: The simplest playbook with the debug task which displays the standard output of the command
- 05-playbook-block: Use blocks to organize tasks
- 06-playbook-role: Use roles to organize tasks
- 07-playbook-role-with-include: Use multiple tasks file in a role to organize tasks
- 08-playbook-role-with-loop: Run tasks in a loop
- 09-playbook-override-parameter: Demonstrate how to override a parameter
- 10-playbook-variable-precedence: Demonstrate all the 22 level of variable precedence
- 11-playbook-variable-scope: Some demonstration of variable scoping and environment variables