Ansible Play Example

Ansible is an IT automation tool. It can configure systems, deploy software, and orchestrate more advanced IT tasks such as continuous deployments or zero downtime rolling updates.

Ansible’s goals are foremost those of simplicity and maximum ease of use. It also has a strong focus on security and reliability, featuring a minimum of moving parts, usage of OpenSSH for transport (with an accelerated socket mode and pull modes as alternatives), and a language that is designed around auditability by humans – even those not familiar with the program.

# jinja2 template:
filename: test_file.j2:
----------------------------

{% for varr in varss %} {{ varr }} {% endfor %} Radius Networks: {% for rad_net in radius_clients %} Network: {{ rad_net['name'] }} / IP Network: {{ rad_net['network'] }} {% endfor %} {% for host in groups['servers'] %} {{ host }} {{ hostvars[host]['ipaddr'] }} {% endfor %}

----------------------------

#inventory file:
filename: hosts.inv:

----------------------------

[servers] loozer.ccc.net ipaddr=10.0.0.10

 

-----------------------------

# Playbook - yaml file:
test_play.yml:
-----------------------------

--- - hosts: servers vars: gather_facts: no vars: varss: - var1 - var2 - var3 - var4 - var5 radius_clients: - name: aa:-mgt-network network: 10.x.x.x/24 - name: bb-mgt-network network: 10.y.y.y/24 - name: cc-mgt-network network: 10.z.z.z/24 tasks: # - name: ensure apache is at the latest version # yum: pkg=httpd state=latest # - name: ensure https is running # service: name=httpd state=running - name: copy ansible template into test file template: src=test_file.j2 dest=/var/tmp/test_file.txt notify: - restart httpd handlers: - name: restart httpd service: name=httpd state=restarted

 

Run ansible playbook with the following command:

[root@nyc ansible]# ansible-playbook test_play.yml -i hosts.inv