Ansible:将变量设置为文件内容 [英] Ansible: Set variable to file content
本文介绍了Ansible:将变量设置为文件内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我将ec2模块与ansible-playbook
结合使用,我想将变量设置为文件的内容.这是我目前的操作方式.
I'm using the ec2 module with ansible-playbook
I want to set a variable to the contents of a file. Here's how I'm currently doing it.
- 使用文件名
- shell任务来
cat
文件 - 使用
cat
的结果传递给ec2模块.
- Var with the filename
- shell task to
cat
the file - use the result of the
cat
to pass to the ec2 module.
我的剧本的示例内容.
vars:
amazon_linux_ami: "ami-fb8e9292"
user_data_file: "base-ami-userdata.sh"
tasks:
- name: user_data_contents
shell: cat {{ user_data_file }}
register: user_data_action
- name: launch ec2-instance
local_action:
...
user_data: "{{ user_data_action.stdout }}"
我认为有一种更简单的方法来执行此操作,但是在搜索Ansible文档时找不到它.
I assume there's a much easier way to do this, but I couldn't find it while searching Ansible docs.
推荐答案
您可以使用查找在Ansible中以获取文件的内容,例如
You can use lookups in Ansible in order to get the contents of a file, e.g.
user_data: "{{ lookup('file', user_data_file) }}"
注意事项::此查找将适用于本地文件,而不适用于远程文件.
Caveat: This lookup will work with local files, not remote files.
这是文档中的完整示例:
- hosts: all
vars:
contents: "{{ lookup('file', '/etc/foo.txt') }}"
tasks:
- debug: msg="the value of foo.txt is {{ contents }}"
这篇关于Ansible:将变量设置为文件内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文