Ansible运行角色与主机名相对于本地脚本返回



我有一个本地脚本,它返回我需要进行安装的主机的名称。我如何使用此脚本的结果并使用它来设置主机?这是我认为我可以写的代码的示例:

---
 - hosts: localhost
   roles:
    - getHostScript # running this sets a variable called hostname
 - hosts: "{{{hostvars.localhost.hostname}}"
   roles:
    - install # this runs the install script

正确的方法是什么?

使用add_host模块到您的库存的新主机,然后在另一个播放中以该主机为目标:

---
- hosts: localhost
  roles:
    - getHostScript # running this sets a variable called hostname
  tasks:
    - name: add new host to inventory
      add_host:
        name: "{{ hostname }}"
        groups: target
- hosts: target
  roles:
   - install # this runs the install script

在这里,我将新主机添加到名为target的组中,以便我可以参考组名,而不是需要在以下播放中知道实际主机名。

最新更新