这可能是一个简单的。我正在使用 Ansible 中的三元过滤器来优化正在检查多个路径的剧本,然后在找到该路径时动态设置该路径的事实,但在 Windows 中。
这是剧本:
---
- name: Check if D drive exists
win_stat:
path: D:\
register: d_dir
- name: Create app installation in d drive
win_file:
path: D:\app
state: directory
when: d_dir.stat.exists
- name: Check for deployment directory
win_stat:
path: C:\app
register: deployment_dir
- name: Create Splunk folder in deployment dir
win_file:
path: C:\app\someapp
state: directory
when: deployment_dir.stat.exists
- name: Create temporary dir if none of the directories exists
win_file:
path: C:\temp
state: directory
when: not install_dir.stat.exists and not deployment_dir.stat.exists
- name: Set Splunk install directory dynamically
set_fact:
splunk_win_forwarder_install_dir: "{{ install_dir.stat.exists | ternary('D:\\app\\', (deployment_dir.stat.exists | ternary('C:\\app\\someapp\\', 'C:\\temp\\'))) }}"
但是,当我以这种方式使用三元时,它返回一个错误说明:
{
"exception": "Traceback (most recent call last):\n File \"/usr/lib/python2.7/site-packages/ansible/executor/task_executor.py\", line 592, in _execute\n self._task.post_validate(templar=templar)\n File \"/usr/lib/python2.7/site-packages/ansible/playbook/task.py\", line 299, in post_validate\n super(Task, self).post_validate(templar)\n File \"/usr/lib/python2.7/site-packages/ansible/playbook/base.py\", line 431, in post_validate\n value = templar.template(getattr(self, name))\n File \"/usr/lib/python2.7/site-packages/ansible/template/__init__.py\", line 663, in template\n disable_lookups=disable_lookups,\n File \"/usr/lib/python2.7/site-packages/ansible/template/__init__.py\", line 618, in template\n disable_lookups=disable_lookups,\n File \"/usr/lib/python2.7/site-packages/ansible/template/__init__.py\", line 846, in do_template\n data = _escape_backslashes(data, myenv)\n File \"/usr/lib/python2.7/site-packages/ansible/template/__init__.py\", line 147, in _escape_backslashes\n for token in jinja_env.lex(d2):\n File \"/var/lib/awx/venv/ansible/lib/python2.7/site-packages/jinja2/lexer.py\", line 739, in tokeniter\n name, filename)\nTemplateSyntaxError: unexpected char u'\\\\' at 116\n line 1\n",
"changed": false,
"_ansible_no_log": false
}
这是我试过的。我认为此错误指向正确转义 Windows 路径。我尝试了以下操作:
- name: Set Splunk install directory dynamically
set_fact:
splunk_win_forwarder_install_dir: "{{ install_dir.stat.exists | ternary('D:\\\\app\\\\', (deployment_dir.stat.exists | ternary('C:\\\\app\\\\someapp\\\\', 'C:\\\\temp\\\\'))) }}"
有效,但问题是:
{
"_ansible_no_log": false,
"msg": "the file at the local path C:\\\\app\\\\someapp\\\\app.msi cannot be reached",
"changed": false,
"reboot_required": false
}
--------尝试 2---------------------------------------- --------------
- name: Set Splunk install directory dynamically
set_fact:
splunk_win_forwarder_install_dir: "{{ install_dir.stat.exists | ternary(\'D:\\app\\\', (deployment_dir.stat.exists | ternary(\'C:\\app\\someapp\\\', \'C:\\temp\\\'))) }}"
(ps我尝试了多次其他尝试,我只是尽力简化这个)
第一次尝试补丁有点奏效,路径解析为
C:\\\\app\\\\someapp
。所以看起来逃脱是有效的,然而,它实际上被解析为'\\\\'
而不是简单的c:\
。通常我会简单地使用 '\\'
来逃避,但是使用这个过滤器,它在使用 Windows 路径时似乎有点不同。有什么建议吗?