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

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

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
And a gist of the code here: 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 }}”