> For the complete documentation index, see [llms.txt](https://www.devops.buzz/public/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://www.devops.buzz/public/ansible/dynamic-inventory.md).

# Dynamic inventory

Download ec2.py from [here](https://raw.githubusercontent.com/ansible/ansible/devel/contrib/inventory/ec2.py). Save it in your Ansible root folder.

Edit `ansible.cfg` as follows:

```bash
...
inventory = ./ec2.py
...
```

Your playbook host must be as follows:

```bash
...
hosts: "{{ variable_host | default('tag_tfplayer_vpninstance') }}"
...
```

Run the playbook:

```
ansible-playbook playbooks/vpn.yml \
  --extra-vars "variable_host=tag_tfplayer_vpninstance"
```

{% hint style="info" %}
`variable_host` is the EC2 instance tag. In this example the tag name is `tfplayer` and the tag content is `vpninstance`.
{% endhint %}
