Group_by主机变量

时间:2017-05-23 15:17:40

标签: ansible

如何缩短此代码,以便在我按主机分组时不会重复?而不是重复rhel5,rhel6和rhel7块。我试过with_items但没有运气?

---
- hosts: "{{hosts}}"
  remote_user: root
  tasks:
    - group_by: key=rhel{{ansible_distribution_major_version}}    
- hosts: rhel5
  tasks:
    - debug: msg="{{ansible_distribution}} {{ansible_distribution_release}} {{ansible_distribution_version}}"

- hosts: rhel6
  tasks:
    - debug: msg="{{ansible_distribution}} {{ansible_distribution_release}} {{ansible_distribution_version}}"

- hosts: rhel7
  tasks:
    - debug: msg="{{ansible_distribution}} {{ansible_distribution_release}} {{ansible_distribution_version}}"

1 个答案:

答案 0 :(得分:0)

您可以使用patterns

---
- hosts: "{{hosts}}"
  remote_user: root
  tasks:
    - group_by: key=rhel{{ansible_distribution_major_version}}    

- hosts: rhel5:rhel6:rhel7
  tasks:
    - debug: msg="{{ansible_distribution}} {{ansible_distribution_release}} {{ansible_distribution_version}}"

如果你需要除了某些东西,你可以使用:

- hosts: rhel*:!rhel4