This is the example of ansible repository that shows how to organize managing your hosts with Ansible in one Git repository, how to use Ansible Galaxy and how to utilize ansible-roles repository with some common roles.
This version of playbooks and roles was last tested on:
- Ubuntu 16.04.04
- Ansible 2.4.3.0 (available with official PPA, see installation instruction for Ubuntu)
Previous versions of Ansible and Ubuntu are not supported. If you interested in its support, please, provide patches.
Tip: Authentication with passphrase protected private-key require to enter passphrase for each server. You should execute following on the manager machine:
ssh-agent bash
ssh-add ~/.ssh/id_rsa
Then, execute playbooks.
-
Install ansible on your manager machine:
sudo aptitude install ansible
-
Clone this repository and change working directory to this repository (all following commands assumes that your working directory is this cloned repository directory):
git clone git@github.com:andyceo/ansible-example.git cd ansible-example
-
If you cloned repository
ansible-example
without submodules, then execute following commands in root folder ofansible-example
repository to installansible-roles
repository:git submodule init git submodule update
-
Install andyceo roles from Ansible Galaxy:
ansible-galaxy install -r roles.txt --force
Note that you can install particular role as git submodule:
git submodule add -f git@github.com:andyceo/ansible-role-preconf.git roles/andyceo.preconf
-
Add your target (remote) host name you want to operate with (for example,
example
) to inventory file:ansible/hosts
oransible/hosts.py
(dynamic inventory) -
Add file with your desirable config to
ansible/host_vars/example
. Useansible/host_vars/example
as example -
Usage and runnig:
-
Ping hosts that match pattern
*ampl*
(hostexample
will be pinged):ansible 'ampl' -i hosts.py -m ping
-
Simulate
example.yml
playbook execution on hostexample
and show posible differences that will be made by ansible:ansible-playbook -K -k -v -i hosts -l example example.yml --check --diff
-
Execute playbook
example.yml
on hostexample
with verbose and dump information, asking passwords for ssh and sudo:ansible-playbook -K -k -v -i hosts -l example example.yml
-
Execute playbook with dynamic inventory:
ansible-playbook -K -k -v -i hosts.py -l example example.yml
-
This is cross-role variable, at least three roles use it: andyceo.apache, andyceo.nginx, andyceo.letsencrypt.
vhosts
is a dict were key is a filename were virtual host configuration will be stored, and value (the dict), which represents basic virtual host configuration:
---
vhosts:
www.example.com:
enabled: yes
name: www.example.com
aliases:
- example.com
root: /var/www/example.com
www.example.org:
enabled: yes
name: www.example.org
aliases:
- example.org
- m.example.org
root: /var/www/example.org
Also, each virtual host can have additional variables for andyceo.apache and andyceo.nginx roles.