如何使用 ansible 查找 Linux 主机的序列号

How to find the Serial Number for a Linux host using ansible

因此,对于 Linux 服务器序列号的 ansible "built-in" 事实,我一直在上下寻找,看起来没有自定义 dmidecode ansible 模块就无法使用。真的是这样吗?我觉得奇怪的是,由于内置了过多的可靠事实,因此不包括序列号。

$ /usr/bin/ansible --version
ansible 2.4.2.0
  config file = /home/user/.ansible.cfg
  configured module search path = ['/home/user/.ansible/plugins/modules', 
u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.6/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.6.6 (r266:84292, Aug  9 2016, 06:11:56) [GCC 4.4.7 
20120313 (Red Hat 4.4.7-17)]

Ansible documentation 中所述,收集事实是通过 setup 模块完成的。

查看 Ansible 清单文件中定义的所有主机的所有事实:

ansible all -m setup

要查看清单文件中存在的特定 Linux 主机 (myserver01) 的序列号:

ansible -b myserver01 -m setup -a 'filter=ansible_product_serial'

对于从不在清单文件中的主机 (myserver01) 收集的相同事实:

ansible -b -i myserver01, all -m setup -a 'filter=ansible_product_serial'

更新响应以纳入@Henk 的评论:ansible 命令需要 运行 和 become (-b) 以获得 ansible_product_serial 数据。如果您没有使用无密码权限升级,还添加 -K