在 Ansible 中的 var 中使用 var(查找)- aws_ssm 插件 [英] Use a var in a var in Ansible (lookup) - aws_ssm plugin

查看:36
本文介绍了在 Ansible 中的 var 中使用 var(查找)- aws_ssm 插件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Ansible (2.7.10) 的 var 声明中使用 var

I'm trying to use a var in a var declaration on Ansible (2.7.10)

我正在使用 aws_ssm 查找插件(https://docs.ansible.com/ansible/latest/plugins/lookup/aws_ssm.html)

I'm using aws_ssm lookup plugin (https://docs.ansible.com/ansible/latest/plugins/lookup/aws_ssm.html)

工作示例(硬编码值):

Working example (hardcoded values):

var: "{{ lookup('aws_ssm', '/path/server00', region='eu-west-3') }}"

我想对服务器名称和 AWS 区域使用变量,但我所有的尝试都出错了.

I want to use variables for the server name and the AWS region, but all my tentatives went on errors.

到目前为止我尝试过的:

What I've tried so far:

var: "{{ lookup('aws_ssm', '/path/{{ server }}', region={{ region }}) }}"

var: "{{ lookup('aws_ssm', '/path/{{ server }}', region= + region) }}"

    - name: xxx
      debug: msg="{{ lookup('aws_ssm', '/path/{{ server }}', region='{{ region }}' ) }}"
      register: var

还没有成功,感谢您的帮助,

Without any success yet, thanks for your help,

推荐答案

你从不嵌套 {{...}} 模板表达式.如果您已经在模板表达式中,则只需按名称引用变量即可.例如:

You never nest {{...}} template expressions. If you're already inside a template expression, you can just refer to variables by name. For example:

var: "{{ lookup('aws_ssm', '/path/' + server, region=region) }}"

(这里假设变量 serverregion 已定义.)

(This assumes that the variables server and region are defined.)

您还可以利用 Python 字符串格式化语法.以下内容都会给您相同的结果:

You can also take advantage of Python string formatting syntax. The following will all give you the same result:

  • '/path/' + 服务器
  • '/path/%s' %(服务器)
  • '/path/{}'.format(server)

并且您可以使用 Jinja ~ 连接运算符代替 +,它的作用有点像 + 但强制参数为字符串.所以虽然这是一个错误:

And instead of + you can use the Jinja ~ concatenation operator, which acts sort of like + but forces arguments to be strings. So while this is an error:

  • 'some string' + 1

这将导致文本 some string1:

  • 'some string' ~ 1

这篇关于在 Ansible 中的 var 中使用 var(查找)- aws_ssm 插件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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