简体   繁体   English

如何以正确的方式在 ansible playbook 中转义字符?

[英]How to escape chars in an ansible playbook the proper way?

My playbook intends to extends the LDAP schema by uploading a schema file.我的剧本打算通过上传架构文件来扩展 LDAP 架构。 The command looks like this:该命令如下所示:

- name: LDAP extend schema
  copy: src={{ 'cn={10}subs.ldif' }} dest={{ '/opt/data/ldap/config/cn=config/cn=schema/cn={10}subs.ldif }} owner=joe group=joe mode=0600
  ignore_errors: True

As you can see, I {{eval}} the paths as \\escaping did not work.如您所见,我 {{eval}} 作为 \\escaping 的路径不起作用。 The schema is uploaded correctly but the playbook stops, I suppose it can't do the checksum test because it cant resolve the path.模式已正确上传,但剧本停止,我想它无法进行校验和测试,因为它无法解析路径。 The error I get is the following:我得到的错误如下:

fatal: [xx.domain.com] => failed to parse: Exception OSError: (2, 'No such file or directory', '/opt/data/ldap/config/cn=config/cn=schema/.ansible_tmp6XkGOucn={10}subs.ldif') in <bound method _TemporaryFileWrapper.__del__ of <closed file '<fdopen>', mode 'w+b' at 0x813660>> ignored
{"src": "/root/.ansible/tmp/ansible-tmp-1457985747.12-41932258379232/source", "md5sum": "651e7b60ebdcad75a95a5ff8e91695a8", "group": "joe", "uid": 498, "dest": "/opt/data/ldap/config/cn=config/cn=schema/cn={11}subs.ldif", "changed": true, "state": "file", "gid": 498, "secontext": "system_u:object_r:usr_t:s0", "mode": "0600", "owner": "joe", "size": 2481}

FATAL: all hosts have already failed -- aborting

I've done some googling and it appears that this is NOT a bug, here is a similar issue: https://github.com/ansible/ansible/issues/8032我已经做了一些谷歌搜索,看起来这不是一个错误,这是一个类似的问题: https : //github.com/ansible/ansible/issues/8032

I suppose I am doing it all wrong and there is some trivial way to pass escapable characters as src and dest to anisble's copy module but I failed to find out what that would be.我想我做错了,有一些简单的方法可以将可转义字符作为srcdest传递给 anisble 的复制模块,但我没能找出那会是什么。

I'm not sure which characters you exactly wanted to escape but I suppose it's the equal signs.我不确定您到底想转义哪些字符,但我想这是等号。

The task you posted throws a parser error in Ansible 2 so I also suppose you run Ansible 1.x since you get something different.您发布的任务在 Ansible 2 中引发了解析器错误,因此我也假设您运行 Ansible 1.x,因为您得到了一些不同的东西。

Unexpected Exception: error while splitting arguments, either an unbalanced jinja2 block or quotes意外异常:拆分参数时出错,jinja2 块或引号不平衡

Given my fist assumption is correct you can easily solve the escaping issue with not using the key=value notation but proper YAML syntax.鉴于我的第一个假设是正确的,您可以通过不使用 key=value 符号但使用正确的 YAML 语法轻松解决转义问题。

- name: LDAP extend schema
  copy:
    src: cn={10}subs.ldif
    dest: /opt/data/ldap/config/cn=config/cn=schema/cn={10}subs.ldif
    owner: joe
    group: joe

Also has the small side effect that it actually is readable.也有一个小的副作用,它实际上是可读的。 ;-) ;-)

This works with Ansible 2. If that does not work with your Ansible version I don't know if you can't be helped without upgrading Ansible.这适用于 Ansible 2。如果这不适用于您的 Ansible 版本,我不知道如果不升级 Ansible,您是否无法获得帮助。 Because this part of the error message:因为这部分错误信息:

/opt/data/ldap/config/cn=config/cn=schema/ .ansible_tmp6XkGOu cn={10}subs.ldif /opt/data/ldap/config/cn=config/cn=schema/.ansible_tmp6XkGOu cn={10}subs.ldif

shows it is injecting a unique string into the filename, which usually does not happen in the middle.表明它正在向文件名中注入一个唯一的字符串,这通常不会发生在中间。 So I guess it has issues with some special characters in filenames.所以我猜它在文件名中存在一些特殊字符的问题。 This might be true for Ansible 1 no matter how you defined the task (YAML or k=v)无论您如何定义任务(YAML 或 k=v),这对于 Ansible 1 来说都可能是正确的

A workaround would be to copy the file into place with a different name and then to simply move it to the desired location with a shell task.一种解决方法是将文件复制到具有不同名称的位置,然后使用 shell 任务简单地将其移动到所需位置。 That should work independent of the Ansible version.这应该独立于 Ansible 版本。

- name: LDAP extend schema
  copy:
    src: cn={10}subs.ldif
    dest: /tmp/ldap.extension
    owner: joe
    group: joe
  register: extension

- shell: "mv /tmp/ldap.extension /opt/data/ldap/config/cn=config/cn=schema/cn={10}subs.ldif"
  when: extension | changed
  creates: /opt/data/ldap/config/cn=config/cn=schema/cn={10}subs.ldif
  removes: /tmp/ldap.extension

But it also is ugly...但它也很丑......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM