# Running Playbooks w/ Dynamic Variables

**URL:** <https://forum.ansible.com/t/running-playbooks-w-dynamic-variables/5266>\
**Category:** Get Help\
**Tags:** azure, playbook\
**Created:** [May 2, 2024, 2:52pm UTC](https://forum.ansible.com/t/running-playbooks-w-dynamic-variables/5266 "2024-05-02T14:52:44Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![cmennens](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/cmennens/32/3081_2.png) [@cmennens](https://forum.ansible.com/u/cmennens)\
**Post date:** [May 2, 2024, 2:52pm UTC](https://forum.ansible.com/t/running-playbooks-w-dynamic-variables/5266/1 "2024-05-02T14:52:45Z")

</div>

I am currently using Ansible to deploy a resource in Azure as follows:

```auto
- name: Deploy a Resource Group in Azure
  hosts: localhost
gather_facts: no
  connection: local

  vars:
    resource_group: test-rg
    location: eastus2

  tasks:
    - name: Create a resource group in Azure
azure.azcollection.azure_rm_resourcegroup:
name: "{{ resource_group }}"
location: "{{ location }}"
state: present
register: rg_output

```

My question is how can I define the above playbook for Ansible to run as expected but allowing users to pick or define the value for the vars.

Example, I want to run the above playbook to create a resource group called “dev”. Is there a way I can programmatically define the playbook to allow the user to input the variables at run time?

Something like:  
`$ ansible-playbook deploy_rg.yaml name = dev region = useast2`

---

<div class="post-metadata">

**Author:** ![jrglynn2](https://avatars.discourse-cdn.com/v4/letter/j/94ad74/32.png) [@jrglynn2](https://forum.ansible.com/u/jrglynn2)\
**Post date:** [May 2, 2024, 3:03pm UTC](https://forum.ansible.com/t/running-playbooks-w-dynamic-variables/5266/2 "2024-05-02T15:03:18Z")

</div>

Do you mean this - “defining variables at runtime”? [Using Variables — Ansible Community Documentation](https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_variables.html#defining-variables-at-runtime)

---

<div class="post-metadata">

**Author:** ![cmennens](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/cmennens/32/3081_2.png) [@cmennens](https://forum.ansible.com/u/cmennens)\
**Post date:** [May 2, 2024, 3:33pm UTC](https://forum.ansible.com/t/running-playbooks-w-dynamic-variables/5266/3 "2024-05-02T15:33:05Z")

</div>

I think that works. I’m not sure if there’s a more traditional way in Ansible to run playbooks but define the name of the resource I’m building at runtime. Usually this would be a blank field in a form I fill out in some front end. I guess that works but curious how others configure Ansible from a CI/CD automation deployment lens to create resources repeatedly and not hard code variables in the playbook. Is the URL you recommended above the best / default option for Ansible?

---

<div class="post-metadata">

**Author:** ![jrglynn2](https://avatars.discourse-cdn.com/v4/letter/j/94ad74/32.png) [@jrglynn2](https://forum.ansible.com/u/jrglynn2)\
**Post date:** [May 2, 2024, 3:57pm UTC](https://forum.ansible.com/t/running-playbooks-w-dynamic-variables/5266/4 "2024-05-02T15:57:20Z")

</div>

If variable values change for each run, then yes, you have to feed the new values every time you run it. I do this in one of two ways, depending on the amount of variables needed:

1. Many variables needed - I create a variables file for each implementation, and feed the whole file - `-e "@config/variable-file.yml"`

2. Minimum amount of variables needed - I feed each variable - `-e '{"hosts_used":"server1","url_name":"url-server1.test.com"}'`

---

<div class="post-metadata">

**Author:** ![cmennens](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/cmennens/32/3081_2.png) [@cmennens](https://forum.ansible.com/u/cmennens)\
**Post date:** [May 2, 2024, 5:25pm UTC](https://forum.ansible.com/t/running-playbooks-w-dynamic-variables/5266/5 "2024-05-02T17:25:03Z")

</div>

This was extremely helpful. Thank you!

---

<div class="post-metadata">

**Author:** ![jrglynn2](https://avatars.discourse-cdn.com/v4/letter/j/94ad74/32.png) [@jrglynn2](https://forum.ansible.com/u/jrglynn2)\
**Post date:** [May 3, 2024, 1:31pm UTC](https://forum.ansible.com/t/running-playbooks-w-dynamic-variables/5266/6 "2024-05-03T13:31:50Z")

</div>

You’re welcome. You can mark one of my responses as the answer and set this as solved, to help people that might be looking for this information in the future.

---

<div class="post-metadata">

**Author:** ![bcoca](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/bcoca/32/9269_2.png) [@bcoca](https://forum.ansible.com/u/bcoca)\
**Post date:** [May 3, 2024, 4:58pm UTC](https://forum.ansible.com/t/running-playbooks-w-dynamic-variables/5266/7 "2024-05-03T16:58:04Z")

</div>

You can even use interactive prompts:

[https://docs.ansible.com/ansible/latest/playbook\_guide/playbooks\_prompts.html#interactive-input-prompts](https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_prompts.html#interactive-input-prompts)

FYI, it won’t prompt if you already passed the info as extra vars.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [June 2, 2024, 4:58pm UTC](https://forum.ansible.com/t/running-playbooks-w-dynamic-variables/5266/8 "2024-06-02T16:58:13Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
