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

use*_*618 0 ansible ansible-2.x ansible-facts

因此,我对 Linux 服务器序列号的 ansible“内置”事实进行了高低审视,而且如果没有自定义的 dmidecode ansible 模块,它似乎不可用。真的是这样吗?我觉得奇怪的是,由于有大量内置的 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)]
Run Code Online (Sandbox Code Playgroud)

Cri*_*alu 5

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

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

ansible all -m setup
Run Code Online (Sandbox Code Playgroud)

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

ansible -b myserver01 -m setup -a 'filter=ansible_product_serial'
Run Code Online (Sandbox Code Playgroud)

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

ansible -b -i myserver01, all -m setup -a 'filter=ansible_product_serial'
Run Code Online (Sandbox Code Playgroud)

更新了响应以纳入@Henk 的评论:ansible 命令需要与become( -b)一起运行才能获取 ansible_product_serial 数据。-K如果您没有使用无密码权限提升,也请添加。

  • 公平是公平的:Cristi 是对的,但你必须以 root 身份运行命令,所以使用“--become”,比如 `ansible -b -K -i inventory-file hostname.example.com -m setup - 'filter=ansible_product_serial'` (2认同)
  • @CristiDascalu @user3155618 好吧,当使用“become”运行命令时,您只能从事实中获取它。作为普通用户,您不允许使用 `cat /sys/devices/virtual/dmi/id/product_serial`。也许它对您有用,因为您在配置中启用了“become=True”作为默认设置。这样你的答案是第一个也是正确的。我认为此时如果您的答案被标记为正确而不是我的答案会更加公平,但它应该包括提及“成为”。 (2认同)