10

I'm having issues working out how to best apply hosts/roles to different operating systems in Ansible.

I'm currently using Ansible to generate Windows/Mac/Linux VMs, and each of the VMs have different roles which need to be applied to them.

I've read the best practices and have tried to follow them, especially to use the grouping method, but I'm still unsure as they mostly mention webservers and dbservers.


site.yml

- name: Apply common configuration to all nodes
  hosts: all
  roles:
    - common

- name: Configure and deploy test node 1
  hosts: build-test-node
  roles:
    - build-machine-test-1

- name: Configure and deploy test node 2
  hosts: build-test-node-2
  roles:
    - build-machine-test-2

environments/test/groups

[win_build_nodes]
win_build_machine_1
win_build_machine_2

[mac_build_nodes]
mac_build_machine_1

environments/test/hosts

win_build_machine_1   ansible_host=......
win_build_machine_2   ansible_host=......
mac_build_machine_1   ansible_host=......

environments/test/meta

[mac_build_nodes]
[win_build_nodes]

[win:children]
win_build_nodes

[mac:children]
mac_build_nodes

[linux:children]

[build_nodes:children]
win_build_nodes
mac_build_nodes

The environment/test/meta file I believe needs to be changed with the build_nodes:children, but I'm not sure what to. Previously I was just having the VMs have all the roles built to it with the following:

Old site.yml

- name: Apply everything
  hosts: build_nodes
  roles:
    - common
    - build-machine-test-1
    - build-machine-test-2

But then realised I didn't want everything being built on the same machine. Note, build-machine-test-1 and 2 both have separate Windows/Mac/Linux builds as they detect what to include based on the operating system.

Can anyone suggest a better way of separating the VMs based on operating systems?

2 Answers 2

6
+100

So I managed to keep the same structure roughly, and managed to separate how the machines are generated, however still is a bit fuzzy and can probably be improved to be more efficient!


site.yml

- name: Apply common configuration to all nodes
  hosts: all
  roles:
    - common

- name: Configure and deploy test node 1
  hosts: build-test-node
  roles:
    - build-machine-test-1

- name: Configure and deploy test node 2
  hosts: build-test-node-2
  roles:
    - build-machine-test-2

environments/test/groups

[win_test_1_nodes]
win_build_machine_1

[mac_test_1_nodes]
mac_build_machine_1

[win_test_2_nodes]
win_build_machine_2

[mac_test_2_nodes]
mac_build_machine_2

environments/test/hosts

win_build_machine_1   ansible_host=......
win_build_machine_2   ansible_host=......
mac_build_machine_1   ansible_host=......
mac_build_machine_2   ansible_host=......

environments/test/meta

[win_test_1_nodes]
[mac_test_1_nodes]
[win_test_2_nodes]
[mac_test_2_nodes]

[win:children]
win_test_1_nodes
win_test_2_nodes

[mac:children]
mac_test_1_nodes
mac_test_2_nodes

[build-machine-test-1:children]
win_test_1_nodes
mac_test_1_nodes

[build-machine-test-2:children]
win_test_2_nodes
mac_test_2_nodes

This solution allows more test nodes to be generated with separate roles added to them, and can just be added to the correct group for the machine to be provisioned.

1

https://github.com/030/ansible-certbot

main.yml

- include: Debian.yml
when: ansible_os_family == 'Debian'

put everything that is required on debian like apt in Debian.yml and yum in Centos.yml.

In summary, it is possible to let all OS types use the same role if the OS specific things are put in separate files.

This is a more comprehensive example https://github.com/geerlingguy/ansible-role-mysql/blob/master/tasks/main.yml

1
  • I have already done this at a lower level, as each of the build-machine-test-1 roles has their own separate builds based off os_family. My question is for a higher level where the build machines themselves need to be separated out before the roles are applied...
    – Rekovni
    Sep 15, 2017 at 14:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.