Basic installation
sudo yum install ansible (or) sudo pip install ansible
Configuring your hosts file with inventory
cat /etc/ansible/hosts
[haproxy]
10.0.184.12
[all_servers:vars]
ansible_connection=ssh
ansible_user=root
ansible_ssh_private_key_file=/Users/home/yoursshkey.pem
I have only one server in my inventory where i will be setting up an haproxy
For easy management we will be writing common variables under [all_servers:vars]
When we run ansible play books hosts will be picked from the above inventory file
Example : hosts: haproxy ( this will run on all servers under haproxy )
Now test servers doing a ping ( ping in ansible will connect to the server using ssh )
ansible haproxy -m ping
10.0.184.12 | SUCCESS => {
"changed": false,
"ping": "pong"
}
Then Download ready made packages from ansible galaxy
https://galaxy.ansible.com/devops/haproxy/
Also download dependencies that will configure EPEL repositories
https://galaxy.ansible.com/sfromm/epel/
Then create a common playbook for both the roles
- hosts: haproxy
roles:
- { role: devops.epel }
- hosts: haproxy
roles:
- role: devops.haproxy
haproxy_stats:
name: 'global_monitor'
ip: "{{ ansible_default_ipv4.address }}"
port: '38888'
stats:
enabled: True
hide_version: true
uri: /slb_stats_url
realm: Welcome\ to\ slb\ monitor
auth: admin:admin
refresh: 2s
haproxy_frontends:
- name: 'fe-testsite'
ip: '{{ ansible_default_ipv4.address }}'
port: '80'
maxconn: '1000'
default_backend: 'be-testsite'
haproxy_backends:
- name: 'be-testsite'
description: 'testsite'
servers:
- name: 'be-testsite-01'
ip: '192.168.1.100'
sh-3.2#
ansible-playbook main.yml
PLAY [haproxy] *****************************************************************
TASK [setup] *******************************************************************
ok: [10.0.184.12]
TASK [devops.epel : Installs python dependencies] ******************************
changed: [10.0.184.12] => (item=[u'libselinux-python'])
TASK [devops.epel : Disable SElinux] *******************************************
changed: [10.0.184.12]
TASK [devops.epel : create EPEL yum repository] ********************************
changed: [10.0.184.12]
TASK [devops.epel : import EPEL GPG key] ***************************************
changed: [10.0.184.12]
PLAY [haproxy] *****************************************************************
TASK [setup] *******************************************************************
ok: [10.0.184.12]
TASK [devops.haproxy : Include OS-specific variables.] *************************
ok: [10.0.184.12]
TASK [devops.haproxy : Installs haproxy as well as socat for socket api.] ******
changed: [10.0.184.12] => (item=[u'haproxy', u'socat'])
TASK [devops.haproxy : Ensure HAProxy is started and enabled on boot.] *********
changed: [10.0.184.12]
TASK [devops.haproxy : Ensure chroot directory exists.] ************************
ok: [10.0.184.12]
TASK [devops.haproxy : Create directory for the frontend] **********************
changed: [10.0.184.12]
TASK [devops.haproxy : Empty the folder if not already empty] ******************
changed: [10.0.184.12]
TASK [devops.haproxy : Build up the frontends] *********************************
[DEPRECATION WARNING]: Using bare variables is deprecated. Update your playbooks so that the environment value uses the full variable syntax ('{{haproxy_frontends}}').
This feature will be
removed in a future release. Deprecation warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.
changed: [10.0.184.12] => (item={u'ip': u'10.0.184.12', u'maxconn': u'1000', u'default_backend': u'be-testsite', u'port': u'80', u'name': u'fe-testsite'})
TASK [devops.haproxy : Create directory for the backends] **********************
changed: [10.0.184.12]
TASK [devops.haproxy : Empty the folder if not already empty] ******************
changed: [10.0.184.12]
TASK [devops.haproxy : Build up the backends] **********************************
[DEPRECATION WARNING]: Using bare variables is deprecated. Update your playbooks so that the environment value uses the full variable syntax ('{{haproxy_backends}}').
This feature will be
removed in a future release. Deprecation warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.
changed: [10.0.184.12] => (item={u'servers': [{u'ip': u'192.168.1.100', u'name': u'be-testsite-01'}], u'description': u'testsite', u'name': u'be-testsite'})
TASK [devops.haproxy : Create directory for the listen sections] ***************
changed: [10.0.184.12]
TASK [devops.haproxy : Empty the folder if not already empty] ******************
changed: [10.0.184.12]
TASK [devops.haproxy : Build up the listen sections] ***************************
skipping: [10.0.184.12] => (item=haproxy_listen)
TASK [devops.haproxy : Create directory for the userlists] *********************
changed: [10.0.184.12]
TASK [devops.haproxy : Empty the folder if not already empty] ******************
changed: [10.0.184.12]
TASK [devops.haproxy : Build up the userlist sections] *************************
skipping: [10.0.184.12] => (item=haproxy_userlists)
TASK [devops.haproxy : Create the compiled folder] ****************************
changed: [10.0.184.12]
TASK [devops.haproxy : Empty the folder if not already empty] ******************
changed: [10.0.184.12]
TASK [devops.haproxy : Build up the global config] *****************************
changed: [10.0.184.12]
TASK [devops.haproxy : Build up the default config] ****************************
changed: [10.0.184.12]
TASK [devops.haproxy : Build up the stats config] ******************************
changed: [10.0.184.12]
TASK [devops.haproxy : Assemble the frontends configuration file] **************
changed: [10.0.184.12]
TASK [devops.haproxy : Assemble the backends configuration file] ***************
changed: [10.0.184.12]
TASK [devops.haproxy : Assemble the listen sections configuration file] ********
changed: [10.0.184.12]
TASK [devops.haproxy : Assemble the userlists sections configuration file] *****
changed: [10.0.184.12]
TASK [devops.haproxy : Assemble the final configuration file] ******************
changed: [10.0.184.12]
RUNNING HANDLER [devops.haproxy : restart haproxy] *****************************
changed: [10.0.184.12]
PLAY RECAP *********************************************************************
10.0.184.12 : ok=31 changed=27 unreachable=0 failed=0
This is just a test deployment that will forward request to background test server 192.168.1.100