Ansible - Windows path variable

我的梦境 提交于 2021-01-29 05:33:17

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/56022727/ansible-windows-path-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!