ansible / ansible-container

DEPRECATED -- Ansible Container was a tool to build Docker images and orchestrate containers using only Ansible playbooks.
GNU Lesser General Public License v3.0
2.19k stars 392 forks source link

include_role overrides variables from previous role #889

Closed clvx closed 6 years ago

clvx commented 6 years ago
ISSUE TYPE
container.yml
$tree                                                   
.                                                       
├── inventory                                           
├── roles                                               
│   ├── dummy1                                          
│   │   ├── defaults                                    
│   │   │   └── main.yml                                
│   │   ├── files                                       
│   │   │   └── foo.conf.j2                             
│   │   └── tasks                                       
│   │       └── main.yml                                
│   └── dummy2                                          
│       ├── files                                       
│       └── tasks                                       
│           └── main.yml                                                              
└── test.yml           

$find . -type f -print -exec cat {} \; | grep -v '.git'
./test.yml
---
- hosts: lab
  roles:
    - roles/dummy1
    - roles/dummy2
./roles/dummy2/tasks/main.yml
---

- name: Debuggin variables
  debug:
    msg: "{{ role_dir }}/files/{{ service_name }}.conf.j2"

- name: Copying file files/
  template:
    src: "{{ role_dir }}/files/{{ service_name }}.conf.j2"
    dest: "{{ role_path }}/files/{{ service_name }}.conf"
./roles/dummy1/files/foo.conf.j2
echo {{ service_name }}
./roles/dummy1/tasks/main.yml
---

- name: debug role path
  debug:
    msg: "{{ role_location }}"

- name: Calling dummy2
  include_role:
    name: dummy2
    private: True
  vars:
    role_dir: "{{ role_location }}"
    vhost_name: "{{ service_name }}"
./roles/dummy1/defaults/main.yml
---

service_name: foo
bar: "{{ service_name }}"
role_location: "{{ role_path }}"
./inventory
[lab]
127.0.0.1
OS / ENVIRONMENT
$ansible --version                                      
ansible 2.4.2.0

$cat /etc/os-release 
NAME="Ubuntu"
VERSION="17.10 (Artful Aardvark)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 17.10"
VERSION_ID="17.10"
VERSION_CODENAME=artful
UBUNTU_CODENAME=artful
SUMMARY

include_role doesn't reads variables from role dummy1 instead of dummy2. In this case role_location is defined in dummy1 but not in dummy2.

STEPS TO REPRODUCE
$ansible-playbook test.yml -i inventory -k
SSH password: 

PLAY [lab] **********************************************************************************************************************************************************************************************************************

TASK [Gathering Facts] **********************************************************************************************************************************************************************************************************
ok: [127.0.0.1]

TASK [roles/dummy1 : debug role path] *******************************************************************************************************************************************************************************************
ok: [127.0.0.1] => {
    "msg": "/tmp/lab/roles/dummy1"
}

TASK [roles/dummy1 : Calling dummy2] ********************************************************************************************************************************************************************************************

TASK [dummy2 : Debuggin variables] **********************************************************************************************************************************************************************************************
ok: [127.0.0.1] => {
    "msg": "/tmp/lab/roles/dummy2/files/foo.conf.j2"
}

TASK [dummy2 : Copying file files/] *********************************************************************************************************************************************************************************************
fatal: [127.0.0.1]: FAILED! => {"changed": false, "msg": "Could not find or access '/tmp/lab/roles/dummy2/files/foo.conf.j2'"}
        to retry, use: --limit @/tmp/lab/test.retry

PLAY RECAP **********************************************************************************************************************************************************************************************************************
127.0.0.1                  : ok=3    changed=0    unreachable=0    failed=1   
EXPECTED RESULTS

Success.

roles/dummy1/files/foo.conf should be deployed with echo foo.

ACTUAL RESULTS
TASK [dummy2 : Copying file files/] *********************************************************************************************************************************************************************************************
fatal: [127.0.0.1]: FAILED! => {"changed": false, "msg": "Could not find or access '/tmp/lab/roles/dummy2/files/foo.conf.j2'"}