Ignore SSH authenticity checking, to avoid any human intervention in the middle of the script execution for below prompts:
1 2 3 4
# GATHERING FACTS *************************************************************** # The authenticity of host 'xxx.xxx.xxx.xxx (xxx.xxx.xxx.xxx)' can't be established. # RSA key fingerprint is xx:yy:zz:.... # Are you sure you want to continue connecting (yes/no)?
Edit the ansible.cfg, set host_key_checking=false
ansible configuration file is only created automatically during the installation if you perform the installation with package managers like yum or apt-get. If you installed ansible using pip you should create the configuration file manually.
set variables for tempalte associated with hosts/groups
create two folders named as group_vars and host_vars
Group vars
The check interval will be set in a group_vars file for haproxy. This will ensure all haproxies will inherit from it.
We just need to create the file group_vars/haproxy.yml below the inventory directory. The file has to be named after the group you want to define the variables for. If we wanted to define variables for the web group, the file would be named group_vars/web.yml.
@haproxy.yaml
1 2
--- haproxy_check_interval:3000
Note that the .yml is optionalm: we could name haproxy group vars file group_vars/haproxy and Ansible would be ok with it. The extension just helps editors picking the right syntax highlighter.
The name is arbitrary. Meaningful names are recommended of course, but there is no required syntax. You could even use complex variables (a.k.a. Python dict) like this:
This is just a matter of taste. Complex vars can help group stuff logically. They can also, under some circumstances, merge subsequently defined keys (note however that this is not the default ansible behaviour). For now we’ll just use simple variables.
Hosts vars
Hosts vars follow exactly the same rules, but live in files under host_vars directory.
Let’s define weights for our backends in host_vars/host1.example.com:
1
haproxy_backend_weight: 100
and host_vars/host2.example.com:
1
haproxy_backend_weight: 150
If we’d define haproxy_backend_weight in group_vars/web, it would be used as a ‘default’: variables defined in host_vars files overrides variables defined in group_vars.
global daemon maxconn256 {%ifhaproxy_stats_socket%} statssocket {{ haproxy_stats_socket }} # use variables: haproxy_stats_socket defined in host var {%endif%}
we don’t need to run httpd.yaml any more, as there’s no change to web servers. However we added an empty play for web hosts at the top. It does nothing except gather_facts: true. But it’s here because it will trigger facts gathering on hosts in group web. This is required because the haproxy playbook needs to pick facts from hosts in this group. If we don’t do this, ansible will complain saying that ansible_all_ipv4_addresses key doesn’t exist.