如何从hosts文件获取主机名及从模板访问monitoring_hostname?
monitoring_hostname from Templates (With Dynamic Hosts Files) Got it, let's walk through how to handle this since your hosts file is dynamic—hardcoding monitor: monitoring_hostname just won't work here. Here's a practical, step-by-step approach depending on your setup:
1. First: Extract monitoring_hostname from the Dynamic Hosts File
Before you can reference it in a template, you need to capture this value from your dynamically generated hosts file. Here are two reliable methods:
Option A: Capture the Value During Hosts Generation
If you’re using a script to generate the hosts file, modify that script to save the monitoring_hostname as a variable or temporary file your templating system can access. For example, in a bash script:
# When generating/updating the hosts file, pull the monitoring hostname MONITORING_HOST=$(grep -oP '^\d+\.\d+\.\d+\.\d+\s+\K\w+' /path/to/dynamic_hosts | grep monitoring_hostname) # Export it as an environment variable so your template can pick it up export MONITORING_HOST
Option B: Parse the Hosts File Directly in Your Template
If you’re using a templating engine like Jinja2 (common in Ansible, Django, etc.), you can read and parse the hosts file right inside the template. Here’s a quick snippet:
{% set monitoring_host = None %} {% for line in open('/etc/hosts').readlines() %} {% if 'monitoring_hostname' in line %} {% set monitoring_host = line.split()[1] %} {% endif %} {% endfor %} # Now use the variable in your config monitor: {{ monitoring_host }}
2. Reference the Value in Your Template
Once you have the monitoring_hostname captured as a variable, referencing it is straightforward. Here are examples for common tools:
Example with Ansible
If you’re using Ansible, use the lineinfile module to extract the value into a variable, then pass it to your template:
- name: Pull monitoring hostname from hosts file lineinfile: path: /etc/hosts regexp: '^\d+\.\d+\.\d+\.\d+\s+(monitoring_hostname)' register: monitoring_host_result - name: Generate config from template template: src: your_config_template.j2 dest: /path/to/final_config vars: monitoring_host: "{{ monitoring_host_result.matched_groups[0] }}"
Then in your_config_template.j2:
monitor: {{ monitoring_host }}
Example with Shell + Jinja2-CLI
If you’re using a plain shell script with Jinja2-cli, pass the variable as an argument when rendering the template:
MONITORING_HOST=$(grep -oP '^\d+\.\d+\.\d+\.\d+\s+\K\w+' /etc/hosts | grep monitoring_hostname) jinja2 --format=env -D MONITORING_HOST=$MONITORING_HOST your_template.j2 > final_config_file
Template content:
monitor: {{ MONITORING_HOST }}
Quick Best Practices
- Prefer capturing during generation over parsing: It’s cleaner and more efficient to grab the
monitoring_hostnamewhen you generate the hosts file, rather than parsing the file every time you render the template. - Check permissions: Make sure the process rendering the template has read access to your dynamic hosts file.
内容的提问来源于stack exchange,提问作者Jimmy Sanchez

