windows - 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
解决方案
没有办法在 Ansible 运行之间保留变量。但是有一个技巧:您可以将变量保存到 yaml/json 文件中(在本地主机上),然后使用它include_vars
来加载它们。
保存:
- copy:
content: '{{ my_variable|to_json }}'
dest: foo.json
delegate_to: localhost
加载:
- include_vars:
file: foo.json
推荐阅读
- javascript - 使用扩展运算符动态添加属性
- javascript - 哪些文件夹是无用的,哪些文件夹在 ReactJS 中很重要?
- c# - 为什么 XElement 的行为不像引用类型?
- .net - ASP.Net 5 + Angular 部署到 IIS 时找不到 404 如何配置路径
- jestjs - 使用角离子存储的开玩笑测试失败
- ios - onDelete 和 Core Data 删除对象后,SwiftUI 内部崩溃
- spring-boot - 通过 REST 使用 Google 身份验证代码登录/注册
- node.js - 无法使用 Print Js 打印 PDF 文件
- autodesk-forge - 尝试在 Autodesk Forge / Bim360 中显示房间信息
- machine-learning - 结合不同的特征向量,进行 MRI 分类的 SVM 训练