以下是Ansible 2.1中剧本的一部分:
- hosts: localhost
any_errors_fatal: true
tasks:
- name: Bla Bla
file: path=/var/tmp/somedir state=directory
#ignore_errors: no
- name: Create directory for every host
file: path=/var/tmp/somedir/{{ item }} state=directory
with_items: "{{ groups['XYZ'] }}"
- name: Get File contents of NewFile
shell: cat NewFile.txt executable=/bin/bash
register: file_contents
- hosts: XYZ
#any_errors_fatal: true
vars:
num_hosts: "{{ groups['XYZ'] | length }}"
serial: num_hosts
tasks:
- name: Copy files to corresponding directories
vars:
path: /var/tmp/somedir/{{ item[0] }}
synchronize: mode=pull src={{ item[1] }} dest={{ path }}
with_nested:
- "{{ groups['XYZ'] }}"
- with_lines: cat NewFile.txt
这不起作用。
现在问题是我无法引用已在localhost下注册的file_contents
而Ansible不支持来自cat
的{{1}} NewFile
有没有办法以某种简单的方式做到这一点?我只需要检查此剧本中hosts: XYZ
的内容,然后使用相同的方法将文件从远程复制到本地。
答案 0 :(得分:0)
如评论中所述,事实(或所有变量)存储在主机上。如果您已在 localhost 上运行的任务中注册了值,则可以通过全局hostvars
dict从在其他主机的上下文中运行的任何任务访问它。所有主持人及其事实都存储在那里:
hostvars['localhost']['file_contents']
我不完全确定注册的变量在hostvars dict中可用。如果没有,您必须在第一次播放中使用set_fact
将其存储为事实。