[英]Ansible: read yaml file from remote location into variable
I want to read a yaml file on the remote location with Ansible and store the content to a variable which can be used my playbook during its runtime.我想用 Ansible 读取远程位置的 yaml 文件,并将内容存储到一个变量中,该变量可以在我的剧本运行时使用。 Is this possible?这可能吗?
In respect to the comment of Zeitounator and the slurp
_module examples, as well the set_fact
_module在相对于的注释Zeitounator和slurp
_module实例以及所述set_fact
_module
---
- hosts: test.example.com
become: no
gather_facts: no
tasks:
- name: Slurp remote YAML file
slurp:
src: "/home/{{ ansible_user }}/test.yaml"
register: result
- name: Set variable with result
set_fact:
SLURPED: "{{ result['content'] | b64decode }}"
- name: Show variable
debug:
msg: "{{ SLURPED }}"
Run a command to output the content of the file (eg curl
if you are fetching over http or cat
if the file is on a local filesystem), then feed it to from_yaml
:运行命令以输出文件的内容(例如curl
如果您通过 http 获取,或者cat
如果文件在本地文件系统上),然后将其提供给from_yaml
:
- name: "Read yaml file"
ansible.builtin.shell: "cat /path/to/file.yaml"
register: result
- name: "Parse yaml into variable"
set_fact:
my_yaml: "{{ result.stdout | from_yaml }}"
You can then access the yaml via the my_yaml
variable.然后,您可以通过my_yaml
变量访问 yaml。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.