首页 文章

使用Ansible Pull进行主机名映射的角色[关闭]

提问于
浏览
0

我在常规推送模式下使用了Ansible,其中有一些库存,其中有几个组 . 我有“工作站”和“服务器”以及其他一些东西 .

现在使用 ansible-pull 我已经向“工作站”推出了一个 cron 作业,该工作站将拉出一个git存储库,然后将 localhost.yml 应用于自己 . 这工作正常,但我希望将不同的角色映射到不同的组 .

我唯一的想法是将不同的 cron 作业推广到不同的机器上,并有一个 workstations.yml 剧本等等 .

让Ansible找出(有库存或变量)主机所属的组是否明智?我想可以用主机名定义列表字典 . 然后我这样做:

- role: workstation
  when: ansible_hostname in inventory['workstations']
- role: laptop
  when: ansible_hostname in inventory['laptops']

这是一个很好的解决方案还是我宁愿做更好的事情?

1 回答

  • 1

    默认情况下 ansible-pull 在调用 ansible-playbook 时附加 -l localhost,node-name,127.0.0.1 参数 .

    这样你就可以拥有库存:

    [workstations]
    node1
    [laptops]
    node2
    

    和剧本 local.yml

    - hosts: all
      roles:
        - generic
    - hosts: workstations
      roles:
        - workstation
    - hosts: laptops
      roles:
        - laptop
    

    由于 -l ,只有 generic+laptops 播放将应用于 node2generic+workstationsnode1 . 将跳过与当前节点无关的播放

    跳过:没有匹配的主机

相关问题