While doing the first task
Let us practice Jinja2 Templates with Ansible. Let us start with the monitoring server scenario. A monitoring server has newly been introduced in your environment. We need to update the agents.conf
file with a list of servers in your environment and their details.
Navigate to the ~/playbooks/monitoring-server
. The list of servers are given in the inventory
file under group lamp_app
. This information must be used to generate an agents.conf file at /etc/monitor/agents.conf
on the monitoring_server
. Develop a playbook generate-agent-info.yml
A template file is already given. Use it. A sample output file is given at sample_agents.conf
for your reference. The target servers are not up yet. So don’t try to connect to them. Just use their information from the inventory file.
My template file
thor@ansible-controller ~/playbooks/monitoring-server/templates$ cat agents.conf.j2
hostname, ipaddress, monitor_port, type, protocol
{% for host in groups[‘lamp_app’] %}
{{ host }}, {{ hostvars[host][‘ansible_host’] }}, {{ hostvars[host][‘monitor_port’] }},
{{ hostvars[host][‘protocol’] }}
{% endfor %}
my generate-agent-info.yml
generate-agent-info.yml
- hosts: monitoring-server
tasks:- template:
src: agents.conf.j2
dest: /etc/monitor/agents.conf
- template:
on task verify, receiving following error:
Command ‘docker exec node00 cat /etc/monitor/agents.conf | grep “web0001.*10.1.1.101.*8080.*http”’ returned non-zero exit status 1.