Search code examples
windowspathansible

Ansible - Windows path variable


Is there away to use variables and string together? For example I would like to, define my path's and other options combining variables and string?

#Add Directory
- name: Add Directory
win_file: 
      path: "{{directory_path}}\AppName-{{env}}"
      state: directory

#Add IUSR to directory path
- name: ADD IUSR
win_acl:
      path: "{{directory_path}}\AppName-{{env}}"
      user: IUSR
      rights: Read
      type: allow
      state: present
      propagation: 'NoPropagateInherit'

#Add website
- name: "{{env}} Add App Name"
win_iis_website:
      name: "AppName-{{env}}"
      state: started
      port: 80
      ip: "{{serverip}}"
      hostname: "appname-{{env}}.com"
      application_pool: "{{application_pool4}}"
      physical_path: "{{directory_path}}\AppName-{{env}}"
register: website

Sure there is a simple answer but can't find one at the minute


Solution

  • The declarations of path shall be single-quoted ('). Then the backslash (\) won't be interpreted as an escape character. See Gotchas

    The difference between single quotes and double quotes is that in double quotes you can use escapes

        path: '{{ directory_path }}\AppName-{{ env }}'
    

    The indentation of the code is wrong. The correct syntax is below

        tasks:
    
            #Add Directory
          - name: Add Directory
            win_file:
              path: '{{ directory_path }}\AppName-{{ env }}'
              state: directory
    
            #Add IUSR to directory path
          - name: ADD IUSR
            win_acl:
              path: '{{ directory_path }}\AppName-{{ env }}'
              user: IUSR
              rights: Read
              type: allow
              state: present
              propagation: 'NoPropagateInherit'
    
            #Add website
          - name: "{{ env }} Add App Name"
            win_iis_website:
              name: "AppName-{{ env }}"
              state: started
              port: 80
              ip: "{{ serverip }}"
              hostname: "appname-{{ env }}.com"
              application_pool: "{{ application_pool4 }}"
              physical_path: '{{ directory_path }}\AppName-{{ env }}'
            register: website
    

    It's a good idea to test the playbooks with ansible-lint.