在 ansible shell 命令中使用 {{ 和 }} [英] Using {{ and }} in ansible shell command

查看:49
本文介绍了在 ansible shell 命令中使用 {{ 和 }}的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的剧本中有这个:

- name: Get facts about containers
  shell: "docker ps -f name=jenkins --format {%raw%}{{.Names}}{% endraw %}"
  register: container

请注意,我插入了 {%raw%}{%endraw%} 以便 ansible 不会评估{{".

Note, that I inserted the {%raw%} and {%endraw%} so that ansible does not evaulate the '{{'.

如果我运行这个,我会得到这个错误:

If I run this, I get this error:

致命:[本地主机]:失败!=> {"failed": true, "msg": "{u'cmd': u'docker ps -f name=jenkins --format {{.Names}}', u'end': u'2017-01-19 10:04:27.491648', u'stdout': u'ecs-sde-abn-17-jenkins-ec8eccee8c9eb8e38f01', u'changed': True, u'start': u'2017-01-19 1904:27.481090', u'delta': u'0:00:00.010558', u'stderr': u'', u'rc': 0, 'stdout_lines': [u'ecs-task-17-jenkins-ec8eccee8c9eb8e38f01'], u'warnings': []}: 模板字符串时出现模板错误:意外的'.'.字符串:docker ps -f name=jenkins --format {{.Names}}"}

fatal: [localhost]: FAILED! => {"failed": true, "msg": "{u'cmd': u'docker ps -f name=jenkins --format {{.Names}}', u'end': u'2017-01-19 10:04:27.491648', u'stdout': u'ecs-sde-abn-17-jenkins-ec8eccee8c9eb8e38f01', u'changed': True, u'start': u'2017-01-19 10:04:27.481090', u'delta': u'0:00:00.010558', u'stderr': u'', u'rc': 0, 'stdout_lines': [u'ecs-task-17-jenkins-ec8eccee8c9eb8e38f01'], u'warnings': []}: template error while templating string: unexpected '.'. String: docker ps -f name=jenkins --format {{.Names}}"}

换句话说,该命令运行成功(输出 ecs-task-17-jenkins-ec8eccee8c9eb8e38f01 是正确的),但比它尝试模板字符串......再次?

In other words, the command ran succesfully (the output ecs-task-17-jenkins-ec8eccee8c9eb8e38f01 is correct), but than it tries the template the string ... again?

这里出了什么问题,我该如何解决?

What is going wrong here and how can I fix this?

已编辑

像这样转义 {{:

shell: "docker ps -f name=jenkins --format {{ '{' }}{.Names}{{ '}' }}"

导致同样的错误.

推荐答案

这是 Ansible 2.2.1 中引入的问题:https://github.com/ansible/ansible/issues/20400.使用 {{ 注册变量时发生.

This is a problem introduced in Ansible 2.2.1: https://github.com/ansible/ansible/issues/20400. Happens when registering variable with {{ inside.

解决方法可防止 {{ 出现在注册变量中:

This workaround prevent {{ from appearing in registered variable:

shell: "docker inspect --format '{''{ .NetworkSettings.IPAddress }''}' consul"

重现错误的方法:

- hosts: localhost
  gather_facts: no
  tasks:
    - shell: "echo '{''{.Names}''}'"
      register: myvar    
    - debug: var=myvar

这篇关于在 ansible shell 命令中使用 {{ 和 }}的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆