0

I am trying to query a list of ec2 instances via ansible using the ec2 plugin for dynamic inventories.

I can see the utility of using dynamic inventories. If new machines are added then ansible will automatically execute a play against them. But I also saw on the net that it is possible to spawn instances with ansible, and manually add the new hosts to the static list of hosts.

So my question is : What would be the use cases where we would use dynamic inventories vs static inventories ? i'm new to the realm of devops so i don't know how often we need to spawn instances automatically Vs doing it manually via the AWS console for example. Thanks !

  • 1
    Dynamic inventories are typically more helpful unless you are feeding the list from Terraform to Ansible, in which case that is almost equivalent. – Matthew Schuchard Jun 28 '19 at 11:07

1 Answers1

1

In case you use autoscaling groups you have to use dynamic inventories.

If you launch ec2s temporary as part of a build pipelines use dynamic inventories. e.g. you just want to test the deployment of your software and terminate the machine after that test.

If you want to disable ansible plays on some machines you can create dynamic inventories based on ec2 tags. e.g. you have a security play that runs an all web server each hour but a developer wants to test something on his machine. So he can tag his machine to be skipped. He doesn't need access to the inventory file (and you can run another play once at midnight to enable the security play again. so it won't be forgotten).

By the way: you can use ec2_instance_facts with filter options and add_host to create dynamic inventories during the playbooks run time.

e.g. you have three types of server "web", "app", "db". you tag the ec2s during launch with servertype: [web|app|db]. You can filter these ec2s with:

- name: collect ec2s 
  ec2_instance_facts:
    region: "{{ region }}"
    filters:
      "tag:servertype": "{{ servertype_list }}"
  register: ec2_list

and run your play selectively on a server group with an external variable ansible-playbook test.yml -e servertype_list=['web','app'] or ansible-playbook test.yml -e servertype_list=['db'].

So by tagging the machine you avoid to take care of a static inventory.

Zeitounator
  • 38,476
  • 7
  • 53
  • 66