-2

I am (relatively) new to ansible and should set up an ansible repository to manage our contiuous integration infrastructure with multiple servers (version control, build, build-agents, ...).

After reading about ansible best practices and layout, I believe that this is ideal:

production
staging
group_vars/...
site.yml
roles/{common,vm,buildserver,buildagent,versioncontrol,...}

The whole infrastructure should run in our vmware cluster, and it seems the vmware_guest module is ideal for most of the vm role.

So the build server should have the vm and buildserver roles.

However, the hostname is only meaningfully after the vm has been created. Also, most likely details like the (unfortunately static) IP etc. should be set as vars, but where? The vm role should use {{ jinja2_vars }} for reusability.

I think I am missing or misunderstanding something.

How do I properly set up the Ansible best pratices repository layout together with VMs and vm roles?

Wilbert
  • 137
  • 6

1 Answers1

1

Although there are patterns used by successful playbooks, there is not one best practice. There is more than one way to do it.

Dynamic inventory scripts for VMware exist. Specify one as your inventory file via one of multiple ways, -i option, $ANSIBLE_HOSTS enviornment variable, or /etc/ansible/hosts. vmware_inventory.py will generate host groups for you based on VMware tags, so you don't need static hostnames in inventory files.

Although, if you are creating the guests, you'll need to specify some identifier. Use variables so they can be overridden easily. Don't think too much about where to define them, put them where it makes sense. Play level? vars/main.yml and done. The variable precedence documentation is a good reminder about the many other places for vars.

One pattern that may not be obvious is that you can add_host to add a new host at runtime, then use this in the next play. Not a complete example:

- hosts: localhost
  connection: local
  tasks:
  - vmware_guest:
    register: newvm
  - add_host:
      name: "{{ newvm.instance.ipv4 }}"
      groups: buildhosts

- hosts: buildhosts
  roles:
  - buildagent
John Mahowald
  • 32,050
  • 2
  • 19
  • 34