一、背景
项目需求:需要获取以下主机信息
主机名称、内核版本、IP地址、网关
二、操作步骤
#创建role,get-host
ansible-galaxy init role/get-host
#编辑roles/get-host/templates/Host_info.j2
{% for host in groups['node'] %}
设备名称:{{ hostvars[host].ansible_hostname }}
内核版本:{{ hostvars[host].ansible_kernel }}
IP地址:{{ hostvars[host].ansible_ens192.ipv4.address }}
网关: {{ hostvars[host].ansible_default_ipv4.gateway }}
{% endfor %}
#编辑roles/get-host/tasks/main.yml
---
# tasks file for roles/get-host
- name: ceshi
template:
src: Host_info.j2
dest: /tmp/Host_info
#编辑Host_info.yaml
---
- name: Get all controlled hostnames
hosts: node
roles:
- get-host
#ansible-playbook运行Host_info.yaml
ansible-playbook --syntax-check Host_info.yaml #检擦yaml格式
ansible-playbook Host_info.yaml #执行剧本