-1

I need to permform few task on same hosts but wanted to group the tasks into the different roles which need to share some output to each other. Consider below example

│── 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
│

Is there any way to collect output of role1 and pass it to role2 and role3 like

- 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}

or anyother mechanism to share the variable between the roles ?

va1bhav
  • 365
  • 1
  • 5
  • 21
  • Hi techraf, thanks for replying to my question. I am new to the ansible (less than 15 days) and trying to automate some stuff. But while writing the tasks i found that my task file size getting bigger (more than 500 lines). So i wanted to devide it in different roles but some tasks are dependent on other tasks output. If I devide them into the roles i am not able to share my output of dependent tasks across the roles. Please share your suggestion to manage such big dependent tasks. – va1bhav Nov 16 '17 at 16:15

1 Answers1

0

What about including tasks in role1 to store the value of role1_a and role1_b into the variable definition of role2 and role3, by combining local_action with 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 }}"
matus
  • 703
  • 3
  • 13