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-examplewithout submodules, then execute following commands in root folder ofansible-examplerepository to installansible-rolesrepository: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/hostsoransible/hosts.py(dynamic inventory) -
Add file with your desirable config to
ansible/host_vars/example. Useansible/host_vars/exampleas example -
Usage and runnig:
-
Ping hosts that match pattern
*ampl*(hostexamplewill be pinged):ansible 'ampl' -i hosts.py -m ping
-
Simulate
example.ymlplaybook execution on hostexampleand 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.ymlon hostexamplewith 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.