Getting started with Ansible
Sample layout:
For Guidance: https://docs.ansible.com/ansible/devel/user_guide/sample_setup.html
production # inventory file for production servers
staging # inventory file for staging environment
group_vars/
group1.yml # here we assign variables to particular groups
group2.yml
host_vars/
hostname1.yml # here we assign variables to particular systems
hostname2.yml
library/ # if any custom modules, put them here (optional)
module_utils/ # if any custom module_utils to support modules, put them here (optional)
filter_plugins/ # if any custom filter plugins, put them here (optional)
site.yml # master playbook
webservers.yml # playbook for webserver tier
dbservers.yml # playbook for dbserver tier
tasks/ # task files included from playbooks
webservers-extra.yml # <-- avoids confusing playbook with task files
roles/
common/ # this hierarchy represents a "role"
tasks/ #
main.yml # <-- tasks file can include smaller files if warranted
handlers/ #
main.yml # <-- handlers file
templates/ # <-- files for use with the template resource
ntp.conf.j2 # <------- templates end in .j2
files/ #
bar.txt # <-- files for use with the copy resource
foo.sh # <-- script files for use with the script resource
vars/ #
main.yml # <-- variables associated with this role
defaults/ #
main.yml # <-- default lower priority variables for this role
meta/ #
main.yml # <-- role dependencies
library/ # roles can also include custom modules
module_utils/ # roles can also include custom module_utils
lookup_plugins/ # or other types of plugins, like lookup in this case
webtier/ # same kind of structure as "common" was above, done for the webtier role
monitoring/ # ""
fooapp/ # ""Installing Azure Modules:
Using the Azure Resource Manager modules requires having specific Azure SDK modules installed on the host running Ansible.
If you are running Ansible from source, you can install the dependencies from the root directory of the Ansible repo.
Using Environment Variables
To pass service principal credentials via the environment, define the following variables:
To pass Active Directory username/password via the environment, define the following variables:
When working in a development environment, it may be desirable to store credentials in a file. The modules will look for credentials in $HOME/.azure/credentials. This file is an ini style file. It will look as follows:
Passing as Parameters
If you wish to pass credentials as parameters to a task, use the following parameters for service principal:
Or, pass the following parameters for Active Directory username/password:
Create Credentials file:
Ansible looks in specific locations to auto load credentials if certain files exists. The Azure Ansible module uses the path ~/.azure/credentials. Placing a file in this location with the proper values will result in Ansible being able to connect to Azure. Keep in mind that credential files in Ansible are used for development environments. To use this method create a file at ~/.azure/credentials and populated the variables subscription_id, client_id, secret, and tenant.
Option 1: Create a credentials file
Populate the required Ansible variables. Replace with actual values.
Option 2: use Env Variables
nstead of using a credentials file you can also populate specific environment variables that Ansible Azure module will use to connect to Azure. Using the bash command export you can define these values. Replace with actual values.
Step 2 - Run an Ansible Playbook
After you provided the necessary values for Ansible to connect to Azure through either a credentials file or environment variables you can test the connection by running an Ansible playbook.
Create a playbook file
Ansible playbooks are written in YAML. Create a playbook by creating a new file named playbook.yaml and opening it in vi.
Paste playbook contents in
Below is an Ansible playbook that creates an Azure resource group named rg-cs-ansible in the eastus region. It also registers the output to an Ansible variable and outputs it with the debug module. Copy and paste in the contents below to populate the playbook.
Run the playbook using ansible-playbook
To execute the playbook use the ansible command ansible-playbook followed by the name of the playbook which is playbook.yaml. Once the playbook finishes running you will have a newly created resource group called rg-cs-ansible in Azure!
Example Creations
Resource Group:
Virtual network:
Public IP:
NSG
Full file for a VM:
Get a Fact:
Waiting for a resource:
Resources:
Read more about Providing Credentials to Azure Modules.
Last updated
Was this helpful?