基于主机可达性检测Ansible脚本生成Jinja2模板及导入Ping结果需求
Got it, let's walk through both of your requirements clearly—getting Ansible ping results into a Jinja2 template, and building a Jinja2 template based on your reachability check playbook while preserving all original technical terms and code.
1. Importing Ansible Ping Results into a Jinja2 Template
First, you need to ensure your Ansible playbook captures the ping result properly, then pass that data to a Jinja2 template for rendering. Here's how to adjust your existing playbook and create the template:
Updated Ansible Playbook Snippet
--- - name: check reachable hosts hosts: group1 gather_facts: no tasks: - set_fact: ping_state: "failed" - debug: msg: "play hosts {{ ansible_play_batch }}" run_once: true - ping: register: ping_result ignore_errors: yes - group_by: key: reachable when: ping_result is success - set_fact: ping_state: "OK" when: ping_result is success # Add this task to render the Jinja2 template with ping results - name: Generate ping result report template: src: ping_results.j2 dest: /path/to/ping_report.txt run_once: true
Note: I adjusted the when condition for the final set_fact to only update ping_state when the ping succeeds, which fixes a logic gap in the original script.
Jinja2 Template (ping_results.j2)
This template will display the ping status for each host in the group:
# Ping Reachability Report Generated on: {{ ansible_date_time.iso8601 }} Playbook Targets: {{ ansible_play_batch | join(', ') }} --- {% for host in groups['group1'] %} Host: {{ host }} Ping Status: {{ hostvars[host]['ping_state'] }} {% if hostvars[host]['ping_result'] is defined %} Ping Output: {{ hostvars[host]['ping_result'].msg | default('No additional output') }} {% endif %} --- {% endfor %}
When the playbook runs, this template will populate with each host's ping state (OK/failed) and any output from the ping module.
2. Building a Jinja2 Template from the Reachability Check Playbook
If you want to create a reusable Jinja2 template that generates the reachability check playbook itself (with configurable variables), here's how to structure it while keeping all original technical terms:
Reusable Playbook Template (reachability_check.j2)
--- - name: check reachable hosts hosts: {{ target_group | default('group1') }} gather_facts: {{ gather_facts | default('no') }} tasks: - set_fact: ping_state: "failed" - debug: msg: "play hosts {{ ansible_play_batch }}" run_once: true - ping: register: ping_result ignore_errors: yes - group_by: key: reachable when: ping_result is success - set_fact: ping_state: "OK" when: ping_result is success {% if run_post_check_commands | default(false) %} - name: run command hosts: reachable become: yes gather_facts: {{ post_check_gather_facts | default('yes') }} tasks: # Add your post-reachability commands here, or leave as a placeholder - command: uptime register: uptime_result - debug: msg: "Uptime for {{ inventory_hostname }}: {{ uptime_result.stdout }}" {% endif %}
How to Use This Template
You can render this template into a concrete Ansible playbook using a simple playbook task:
--- - name: Generate reachability check playbook hosts: localhost gather_facts: no vars: target_group: "web_servers" run_post_check_commands: true tasks: - name: Render playbook template template: src: reachability_check.j2 dest: ./check_web_servers.yml
This lets you customize the target host group, whether to run post-reachability commands, and other variables without modifying the core playbook logic.
内容的提问来源于stack exchange,提问作者Romain

