首页 > 解决方案 > Ansible variables between playbooks

问题描述

I am trying to build a workflow where first playbook runs on Windows hosts, set some variable with all host names that it’s being executed. Second playbook supposed to read this variable and create a directory in Unix with windows hostname.

Tried using set_stats in first playbook, but variable is getting concatenated with all hostnames and one directory is getting created on Unix with big string.

Any workarounds or suggestions to achieve this.

Playbook runs on Windows

- name: set hostname in a variable  
  set_stats:  
      data:  
        current_hostname: "{{ ansible_hostname }}"  
      per_host: no  

Playbook runs on Unix:

- name: Creates UNIX directory  
  file:  
    path: "{{ ARCH_DIR_LOC + current_hostname + '/'}}"  
    state: directory  
    mode: 0777  

标签: windowsunixstatisticsansibleset

解决方案


没有办法在 Ansible 运行之间保留变量。但是有一个技巧:您可以将变量保存到 yaml/json 文件中(在本地主机上),然后使用它include_vars来加载它们。

保存:

- copy:
    content: '{{ my_variable|to_json }}'
    dest: foo.json
  delegate_to: localhost

加载:

- include_vars:
    file: foo.json

推荐阅读