How to join Path in Ansible?

Viewed 2315

I want to make or join path in Ansible. When I was running below code then I am getting an error.

Code:

---
- name: Project1
  hosts: target_hosts

  vars:
    Data: '{{get_data}}' #input from user
    src_inst: '{{get_inst}}' #input from user 

  tasks:
  - name: set path 
    set_fact:
      main_path : '{{ ("\\abc.de.net\xyz\\mn\", src_inst, "\temp") | path_join }}'
    when: Data == 'qaz'

Error:

Unexcepted failure during module execution.

If user give input Data = 'qaz' and src_inst = 'w_123'

Desired Output:
main_path = "\\abc.de.net\xyz\\mn\w_123\temp"

Do you know how to solve this error or how to join path?

1 Answers

As far as I know, path_join cannot be used for Windows paths, use string concatenation instead, the module error was most probably because you were escaping a " so quotes were imbalanced.

Try something like this instead:

---
- name: Project1
  hosts: target_hosts

  vars:
    Data: '{{get_data}}' #input from user
    src_inst: '{{get_inst}}' #input from user 

  tasks:
  - name: set path 
    set_fact:
      main_path : '\\abc.de.net\xyz\\mn\{{ src_inst }}\temp'
    when: Data == 'qaz'
Related