Ansible usage examples
# Verify inventory file / playbook syntax
ansible-inventory -i inventory.yaml --list
ansible-playbook <playbook-file> --check
# Run playbook
ansible-playbook -i <inventory-file> <playbook-file>
# Execute moduls directly
ansible -i <inventory-file> -m <module-name> -a <module-args> <target>
# setup = Gather facts about target
ansible -i inventory.yaml -m setup aws_linux_servers
ansible -i inventory.yaml -m setup -a 'filter=ansible_dns' aws_linux_servers
all:
children:
webservers:
hosts:
ec2-98-81-199-113.compute-1.amazonaws.com:
ec2-3-84-177-11.compute-1.amazonaws.com:
loadbalancers:
hosts:
ec2-34-229-45-132.compute-1.amazonaws.com:
vars:
ansible_user: ec2-user
ansible_ssh_private_key_file: ./my_ssh_key
- name: Play to install apache virtual hosts
hosts: webservers # targets
become: true # defines privilege escalation to install as root
vars:
domain: example.com
directories:
- /var/www/{{ domain }}/html
- /var/www/{{ domain }}/log
- /etc/httpd/sites-available
- /etc/httpd/sites-enabled
tasks: # list of tasks
- name: Install apache # task name
ansible.builtin.dnf: # module
name: httpd
state: present
- name: Create directories
ansible.builtin.file:
path: "{{ item }}"
state: directory
mode: "0655"
loop: "{{ directories }}"
- name: Copy the html content to the target using a template
ansible.builtin.template:
src: ./index.html.j2
dest: /var/www/{{ domain }}/html/index.html
mode: "0644"
- name: Add line to Apache config
ansible.builtin.lineinfile:
path: /etc/httpd/conf/httpd.conf
line: IncludeOptional sites-enabled/*.conf
- name: Copy the virtual host file to the target using a template
ansible.builtin.template:
src: ./virtual_host.conf.j2
dest: /etc/httpd/sites-available/{{ domain }}.conf
mode: "0644"
notify: Restart apache
- name: Create symlink
ansible.builtin.file:
src: /etc/httpd/sites-available/example.com.conf
dest: /etc/httpd/sites-enabled/example.com.conf
state: link
mode: "0644"
notify: Restart apache
- name: Validating config
ansible.builtin.command: httpd -t
register: output
changed_when: output.failed
ignore_errors: true
- name: Write error message
ansible.builtin.fail:
msg: "Wrong Apache Configuration"
when: output.failed
handlers:
- name: Restart apache
ansible.builtin.service:
name: httpd
state: restarted
Loadbalancer
- name: Play to install apache load balancer # play 1
hosts: loadbalancers # targets
become: true # defines privilege escalation to install as root
vars:
domain: example.com
tasks: # list of tasks
- name: Install apache # task name
ansible.builtin.dnf: # module
name: httpd
state: present
- name: Copy the load balancer configuration from template
ansible.builtin.template:
src: ./load_balancer.conf.j2
dest: /etc/httpd/conf.d/lb.conf
mode: '0644'
notify: Restart apache
- name: Validating config
ansible.builtin.command: httpd -t
register: output
changed_when: output.failed
ignore_errors: true
- name: Write error message
ansible.builtin.fail:
msg: "Wrong Apache Configuration"
when: output.failed
handlers:
- name: Restart apache
ansible.builtin.service:
name: httpd
state: restarted
<VirtualHost *:80>
ServerName www.{{ domain }}
ServerAlias {{ domain }}
DocumentRoot /var/www/{{ domain }}/html
ErrorLog /var/www/{{ domain }}/log/error.log
CustomLog /var/www/{{ domain }}/log/requests.log combined
</VirtualHost>
ProxyRequests off
<Proxy balancer://webcluster >
ProxySet lbmethod=byrequests
{% for host in groups['webservers'] %}
BalancerMember http://{{host}}
{% endfor %}
</Proxy>
<Location /balancer-manager>
SetHandler balancer-manager
</Location>
ProxyPass /balancer-manager !
ProxyPass / balancer://webcluster/