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

查看:527
本文介绍了在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:

致命:[localhost]:失败! => {失败":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'警告':[]}:模板化模板时出错,字符串:意外的'.'.字符串: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天全站免登陆