请帮我写一个ansible playbook 用于获取服务器已经up的物理网卡的光衰值
时间: 2024-02-19 22:02:34 浏览: 25
好的,以下是一个示例的ansible playbook,可以获取服务器已经up的物理网卡的光衰值:
```yaml
---
- name: 获取物理网卡的光衰值
hosts: your_server_host
gather_facts: yes
become: true
tasks:
- name: 安装ethtool
apt:
name: ethtool
state: present
when: ansible_distribution == 'Ubuntu'
- name: 安装ethtool
yum:
name: ethtool
state: present
when: ansible_distribution == 'CentOS'
- name: 获取物理网卡的光衰值
shell: ethtool -m {{ item }} | grep Rx optical power | awk '{ print $4 }'
register: result
ignore_errors: true
with_items:
- "{{ ansible_interfaces }}"
when: "'state' in hostvars[inventory_hostname]['ansible_eth0'] and hostvars[inventory_hostname]['ansible_eth0']['state'] == 'up'" # 检查eth0是否up
- name: 打印结果
debug:
var: result
```
请将 `your_server_host` 替换为你的服务器主机名或IP地址。此 playbook 将检查服务器上所有物理接口是否处于 `up` 状态,如果已经 up,则使用 `ethtool` 工具获取其光衰值,并将结果打印出来。请注意,此 playbook 只适用于 Ubuntu 和 CentOS 操作系统。如果您使用其他操作系统,请根据需要修改安装 `ethtool` 的任务。