我有一本很长的剧本,里面定义了很多角色。现在我有了一个角色的需求,我需要将host作为一个变量传递,该变量将在较早的角色上定义。例如攻略
---
- name: task1
hosts: app1
gather_facts: no
any_errors_fatal: true
roles:
- role-1
- name: task2
hosts: "{{ host }}"
any_errors_fatal: true
gather_facts: no
roles:
- role-2我的角色-1
---
- name: setting the var
set_fact:
host: "app2"
- debug:
var: host我的角色-2
---
- debug:
var: host
- name: do something
file:
path: /home/ec2-user/dir1
state: directory
mode: '0755'然而,当我尝试运行我的攻略时,我的角色2被跳过,因为没有匹配的主机。有人能告诉我如何让这个设置正常工作吗?
发布于 2020-03-19 05:05:26
您需要的是add_host:,然后将新创建或分配的组设置为第二个播放的hosts::
- hosts: app1
tasks:
- add_host:
name: app2
groups:
- my-group
- hosts: my-group
tasks:
- debug: var=ansible_hosthttps://stackoverflow.com/questions/60745185
复制相似问题