-1

我需要在同一主机上执行一些任务,但想将任务分组到需要彼此共享一些输出的不同角色中。考虑下面的例子

│── hosts
├── playbooks
│   ├── Playbook1.yml
│
├── roles
│   └── role1
│       ├── files
│       │   └── project1.conf
│       ├── handlers
│       │   └── main.yml
│       ├── meta
│       │   └── main.yml
│       ├── tasks
│       │   └── main.yml [Creates variable role1_a, role1_b]
│       ├── templates
│       └── vars
│           └── main.yml
│   └── role2
│       ├── handlers
│       │   └── main.yml
│       ├── meta
│       │   └── main.yml
│       ├── tasks
│       │   └── main.yml [uses variable from role1 role1_a and creates variable role2_c]
│       ├── templates
│       └── vars
│           └── main.yml
│   └── role3
│       ├── handlers
│       │   └── main.yml
│       ├── meta
│       │   └── main.yml
│       ├── tasks
│       │   └── main.yml [uses variable from role1 role1_b and role2 role2_c]
│       ├── templates
│       └── vars
│           └── main.yml
│

有没有办法收集角色1的输出并将其传递给角色2和角色3

- hosts: localhost
  roles:
    - role: role1_a, role1_b = {role1}
    - role: role2_c = {role2 role1_a: role1_a, role1_b: role1_b}
    - role: {role2 role1_b: role1_b role2_c: role2_c}

或任何其他在角色之间共享变量的机制?

4

1 回答 1

0

在role1中包含任务,将role1_a和role1_b的值存储到role2和role3的变量定义中,通过local_action结合lineinfile

- name: Copy value of role1_a to var definition of role2
  local_action:
    module: lineinfile
    path: ~/ansible/roles/role2/vars/main.yml
    regexp: '^role1_a'
    line: "role1_a: {{ role1_a }}"

- name: Copy value of role1_b to var definition of role3
  local_action:
    module: lineinfile
    dest: ~/ansible/roles/role3/vars/main.yml
    regexp: '^role1_b'
    line: "role1_b: {{ role1_b }}"
于 2017-11-16T10:47:38.930 回答