# How to pass dynamic value of Ansible variable to Shell script variable and vice versa.

**URL:** <https://forum.ansible.com/t/how-to-pass-dynamic-value-of-ansible-variable-to-shell-script-variable-and-vice-versa/36144>\
**Category:** Ansible Project\
**Created:** [December 22, 2021, 5:41am UTC](https://forum.ansible.com/t/how-to-pass-dynamic-value-of-ansible-variable-to-shell-script-variable-and-vice-versa/36144 "2021-12-22T05:41:52Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Girish\_Attarde](https://avatars.discourse-cdn.com/v4/letter/g/2bfe46/32.png) [@Girish\_Attarde](https://forum.ansible.com/u/Girish_Attarde)\
**Post date:** [December 22, 2021, 5:41am UTC](https://forum.ansible.com/t/how-to-pass-dynamic-value-of-ansible-variable-to-shell-script-variable-and-vice-versa/36144/1 "2021-12-22T05:41:52Z")

</div>

Can anyone help to for syntax or logic. Thanks in advance.

---

<div class="post-metadata">

**Author:** ![Dennis\_McCarthy1](https://avatars.discourse-cdn.com/v4/letter/d/34f0e0/32.png) [@Dennis\_McCarthy1](https://forum.ansible.com/u/Dennis_McCarthy1)\
**Post date:** [December 23, 2021, 7:56am UTC](https://forum.ansible.com/t/how-to-pass-dynamic-value-of-ansible-variable-to-shell-script-variable-and-vice-versa/36144/2 "2021-12-23T07:56:35Z")

</div>

Can you provide more detail with an example of a variable you are trying to pass?

As I’m not sure what you need to set, this could help?

Using set\_fact: (example below), you can set a variable to be “something” based on a condition. So variable “DC” below, will become one of three variables “DC1, DC2 or DC3” depending on the domain of the server. You can swap out domain for any other ansible fact. Just use “{{ DC }}” in your playbook to reference and it should work in templates too.

I did a video demo of this on my YT channel: [https://www.youtube.com/watch?v=NUGAQQ7p-Ho&t](https://www.youtube.com/watch?v=NUGAQQ7p-Ho&t)  
And a gist of the code here: [https://gist.github.com/dmccuk/63d8f668c96c95087144d2a09976b86f](https://gist.github.com/dmccuk/63d8f668c96c95087144d2a09976b86f)

I hope this helps.  
— - name: set fact/case example hosts: localhost tasks: - set\_fact: one: hello two: “{{ ansible\_domain }}” three: “{{ ansible\_distribution\_file\_variety[3:6] }}” DC: “{% if ansible\_domain == ‘eu-west-1.compute.internal’ %}DC1\ {% elif ansible\_domain == ‘eu-west-2.compute.internal’ %}DC2\ {% else %}DC3\ {% endif %}” - name: one debug: msg: “{{ one }}” - name: two debug: msg: “{{ two }}” - name: three debug: msg: “{{ three }}” - name: dc debug: msg: Your DC is {{ DC }} - name: combine debug: msg: “{{ one }}-{{ two }}-{{ three }}-{{ DC }}”
