Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When we have a reusable role and it had a dependency - the dependency role gets executed with the wrong parameters. #70773

Closed
zentavr opened this issue Jul 21, 2020 · 5 comments
Labels
affects_2.9 This issue/PR affects Ansible v2.9 bug This issue/PR relates to a bug. needs_info This issue requires further information. Please answer any outstanding questions. needs_template This issue/PR has an incomplete description. Please fill in the proposed template correctly. python3 support:core This issue/PR relates to code supported by the Ansible Engineering Team.

Comments

@zentavr
Copy link

zentavr commented Jul 21, 2020

SUMMARY

The latest manual for today for v. 2.9 tells if we pass different parameters to the role - which is declared twice - it would be executed twice. But that does not work for the dependencies.

ISSUE TYPE
  • Bug Report
ANSIBLE VERSION
ansible 2.9.11
  config file = /Users/zentavr/.ansible.cfg
  configured module search path = ['/Users/zentavr/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /Users/zentavr/Development/Public/pb-web-deploy/deploy/.venv/lib/python3.6/site-packages/ansible
  executable location = /Users/zentavr/Development/Public/pb-web-deploy/deploy/.venv/bin/ansible
  python version = 3.6.8 (v3.6.8:3c6b436a57, Dec 24 2018, 02:04:31) [GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)]
CONFIGURATION
CACHE_PLUGIN(/Users/zentavr/.ansible.cfg) = jsonfile
CACHE_PLUGIN_CONNECTION(/Users/zentavr/.ansible.cfg) = ~/.ansible/tmp
DEFAULT_FORKS(/Users/zentavr/.ansible.cfg) = 10
HOST_KEY_CHECKING(/Users/zentavr/.ansible.cfg) = False
OS / ENVIRONMENT

MacOS 10.15.6

STEPS TO REPRODUCE

Set up the role which should be executed several times (saying the role which installs the artifact). This role has a dependency (saying the role which downloads the artifact), which accepts the parameters from the main role as well.

The main role would print the correct values of the parameters, but the dependency role would print the values which we pass at the latest point where we call the main role.

---
# ./deploy.yml
- name: "Doing the deployment"
  hosts:
    - "env_{{ lookup('env','ENVIRONMENT') }}"
  gather_facts: True
  remote_user: root
  serial: "{{ lookup('env','ROLLING_BATCH_SIZE') }}"

  roles:
    - role: "pb-yii-app"
      vars:
        APP_BUILD: "{{ lookup('env','ADMIN_BUILD') }}"
        APP_NAME: "backend"
      when: >
        'comp_backend' in group_names
          and
        lookup('env','ADMIN_BUILD') != "-Keep-"
      tags:
        - backend

    - role: "pb-yii-app"
      vars:
        APP_BUILD: "{{ lookup('env','FRONTEND_BUILD') }}"
        APP_NAME: "frontend"
      when: >
        'comp_frontend' in group_names
          and
        lookup('env','FRONTEND_BUILD') != "-Keep-"
      tags:
        - frontend

    - role: "pb-yii-app"
      vars:
        APP_BUILD: "{{ lookup('env','API_BUILD') }}"
        APP_NAME: "api"
      when: >
        'comp_api' in group_names
          and
        lookup('env','API_BUILD') != "-Keep-"
      tags:
        - api
---
# roles/pb-yii-app/meta/main.yml
dependencies:
  - role: pb-download
    vars:
      pb_down_artifact: "{{ APP_BUILD }}"
      pb_app_name: "{{ APP_NAME }}"
    when: >
      APP_BUILD not in [ "-Keep-", "-None-" ]

  - role: pb-download
    vars:
      pb_down_artifact: "{{ lookup('env','VENDOR_PKG') }}"
      pb_app_name: "vendor"
    when: >
      lookup('env','VENDOR_PKG') not in [ "-Keep-", "-None-" ]
---
# roles/pb-yii-app/tasks/main.yml
---
- name: "{{ APP_NAME }}: Printing debug"
  debug:
    msg: "{{ _message }}"
  loop:
    - "Installing {{ APP_BUILD }} for {{ APP_NAME }} component"
    - "My group names are: {{ group_names | join(' ') }}"
  loop_control:
    loop_var: _message
---
# roles/pb-download/meta/main.yml
allow_duplicates: True
---
# roles/pb-download/tasks/main.yml
- name: "{{ pb_app_name }}: Printing debug"
  debug:
    msg: "{{ _pb_download_message }}"
  loop:
    - "Downloading {{ pb_down_artifact }} for {{ pb_app_name }} component"
    - "My group names are: {{ group_names | join(' ') }}"
  loop_control:
    loop_var: _pb_download_message
EXPECTED RESULTS
  1. The values of APP_BUILD in pb-yii-app and pb_down_artifact in pb-download should be the same.
  2. The values of APP_NAME in pb-yii-app and pb_app_name in pb-download should be the same.
ACTUAL RESULTS

pb-download role which is a dependency of pb-yii-app prints the wrong values of pb_down_artifact and pb_app_name (which is the value at the latest point where we call the role) instead of the values which we pass to the role in the meta/main.yml file.

# ansible-playbook -i hosts deploy.yml | grep -v "skipping"
PLAY [Doing the deployment] **************************************************************************************

TASK [Gathering Facts] ********************************************************************************************
ok: [mc011-stage.app.local]
ok: [fe011-stage.app.local]
ok: [a011-stage.app.local]
ok: [sa011-stage.app.local]
ok: [fe021-stage.app.local]

TASK [pb-download : api: Printing debug] **************************************************************************
ok: [a011-stage.app.local] => (item=Downloading api-v1.tar.bz2.gpg for api component) => {
    "_pb_download_message": "Downloading api-v1.tar.bz2.gpg for api component"
}

MSG:

Downloading api-v1.tar.bz2.gpg for api component
ok: [a011-stage.app.local] => (item=My group names are: comp_backend env_stage) => {
    "_pb_download_message": "My group names are: comp_backend env_stage"
}

MSG:

My group names are: comp_backend env_stage

TASK [pb-download : vendor: Printing debug] ***********************************************************************
ok: [a011-stage.app.local] => (item=Downloading vendor-v1.tar.bz2.gpg for vendor component) => {
    "_pb_download_message": "Downloading vendor-v1.tar.bz2.gpg for vendor component"
}

MSG:

Downloading vendor-v1.tar.bz2.gpg for vendor component
ok: [a011-stage.app.local] => (item=My group names are: comp_backend env_stage) => {
    "_pb_download_message": "My group names are: comp_backend env_stage"
}

MSG:

My group names are: comp_backend env_stage

TASK [pb-yii-app : backend: Printing debug] ************************************************************************
ok: [a011-stage.app.local] => (item=Installing admin-v1.tar.bz2.gpg for backend component) => {
    "_message": "Installing admin-v1.tar.bz2.gpg for backend component"
}

MSG:

Installing admin-v1.tar.bz2.gpg for backend component
ok: [a011-stage.app.local] => (item=My group names are: comp_backend env_stage) => {
    "_message": "My group names are: comp_backend env_stage"
}

MSG:

My group names are: comp_backend env_stage

TASK [pb-download : api: Printing debug] ****************************************************************************
ok: [fe011-stage.app.local] => (item=Downloading api-v1.tar.bz2.gpg for api component) => {
    "_pb_download_message": "Downloading api-v1.tar.bz2.gpg for api component"
}

MSG:

Downloading api-v1.tar.bz2.gpg for api component
ok: [fe011-stage.app.local] => (item=My group names are: comp_frontend env_stage) => {
    "_pb_download_message": "My group names are: comp_frontend env_stage"
}

MSG:

My group names are: comp_frontend env_stage
ok: [fe021-stage.app.local] => (item=Downloading api-v1.tar.bz2.gpg for api component) => {
    "_pb_download_message": "Downloading api-v1.tar.bz2.gpg for api component"
}

MSG:

Downloading api-v1.tar.bz2.gpg for api component
ok: [fe021-stage.app.local] => (item=My group names are: comp_frontend env_stage) => {
    "_pb_download_message": "My group names are: comp_frontend env_stage"
}

MSG:

My group names are: comp_frontend env_stage

TASK [pb-download : vendor: Printing debug] **************************************************************************
ok: [fe011-stage.app.local] => (item=Downloading vendor-v1.tar.bz2.gpg for vendor component) => {
    "_pb_download_message": "Downloading vendor-v1.tar.bz2.gpg for vendor component"
}

MSG:

Downloading vendor-v1.tar.bz2.gpg for vendor component
ok: [fe011-stage.app.local] => (item=My group names are: comp_frontend env_stage) => {
    "_pb_download_message": "My group names are: comp_frontend env_stage"
}

MSG:

My group names are: comp_frontend env_stage
ok: [fe021-stage.app.local] => (item=Downloading vendor-v1.tar.bz2.gpg for vendor component) => {
    "_pb_download_message": "Downloading vendor-v1.tar.bz2.gpg for vendor component"
}

MSG:

Downloading vendor-v1.tar.bz2.gpg for vendor component
ok: [fe021-stage.app.local] => (item=My group names are: comp_frontend env_stage) => {
    "_pb_download_message": "My group names are: comp_frontend env_stage"
}

MSG:

My group names are: comp_frontend env_stage

TASK [pb-yii-app : frontend: Printing debug] ***************************************************************************
ok: [fe011-stage.app.local] => (item=Installing frontend-v1.tar.bz2.gpg for frontend component) => {
    "_message": "Installing frontend-v1.tar.bz2.gpg for frontend component"
}

MSG:

Installing frontend-v1.tar.bz2.gpg for frontend component
ok: [fe011-stage.app.local] => (item=My group names are: comp_frontend env_stage) => {
    "_message": "My group names are: comp_frontend env_stage"
}

MSG:

My group names are: comp_frontend env_stage
ok: [fe021-stage.app.local] => (item=Installing frontend-v1.tar.bz2.gpg for frontend component) => {
    "_message": "Installing frontend-v1.tar.bz2.gpg for frontend component"
}

MSG:

Installing frontend-v1.tar.bz2.gpg for frontend component
ok: [fe021-stage.app.local] => (item=My group names are: comp_frontend env_stage) => {
    "_message": "My group names are: comp_frontend env_stage"
}

MSG:

My group names are: comp_frontend env_stage

TASK [pb-download : api: Printing debug] *******************************************************************************

TASK [pb-download : vendor: Printing debug] ****************************************************************************

TASK [pb-yii-app : api: Printing debug] *******************************************************************************

PLAY RECAP *************************************************************************************************************
a011-stage.app.local    : ok=4    changed=0    unreachable=0    failed=0    skipped=6    rescued=0    ignored=0
fe011-stage.app.local   : ok=4    changed=0    unreachable=0    failed=0    skipped=6    rescued=0    ignored=0
fe021-stage.app.local   : ok=4    changed=0    unreachable=0    failed=0    skipped=6    rescued=0    ignored=0
mc011-stage.app.local   : ok=1    changed=0    unreachable=0    failed=0    skipped=9    rescued=0    ignored=0
sa011-stage.app.local   : ok=1    changed=0    unreachable=0    failed=0    skipped=9    rescued=0    ignored=0
@ansibot
Copy link
Contributor

ansibot commented Jul 21, 2020

Files identified in the description:
None

If these files are incorrect, please update the component name section of the description or use the !component bot command.

click here for bot help

@ansibot
Copy link
Contributor

ansibot commented Jul 21, 2020

@zentavr: Greetings! Thanks for taking the time to open this issue. In order for the community to handle your issue effectively, we need a bit more information.

Here are the items we could not find in your description:

  • component name

Please set the description of this issue with an appropriate template from:
https://github.com/ansible/ansible/tree/devel/.github/ISSUE_TEMPLATE

click here for bot help

@ansibot ansibot added affects_2.9 This issue/PR affects Ansible v2.9 bug This issue/PR relates to a bug. needs_info This issue requires further information. Please answer any outstanding questions. needs_template This issue/PR has an incomplete description. Please fill in the proposed template correctly. needs_triage Needs a first human triage before being processed. python3 support:core This issue/PR relates to code supported by the Ansible Engineering Team. labels Jul 21, 2020
@zentavr
Copy link
Author

zentavr commented Jul 21, 2020

Maybe related to #69298, #68922 (and probably this comment explains the problem).

@zentavr
Copy link
Author

zentavr commented Jul 21, 2020

This can be disabled through the config with https://docs.ansible.com/ansible/latest/reference_appendices/config.html#default-private-role-vars

In my case the setting of DEFAULT_PRIVATE_ROLE_VARS didn't help. What helped is to remove vars: and provide the variables as parameters, i.e.:

    - role: "pb-yii-app"
#      vars:
#        APP_BUILD: "{{ lookup('env','ADMIN_BUILD') }}"
#        APP_NAME: "backend"
      APP_BUILD: "{{ lookup('env','ADMIN_BUILD') }}"
      APP_NAME: "backend"
      when: >
        'comp_backend' in group_names
          and
        lookup('env','ADMIN_BUILD') != "-Keep-"
      tags:
        - backend
        - admin

roles/pb-yii-app/meta/main.yml looks like:

---
dependencies:
  - role: pb-download
#    vars:
#      pb_down_artifact: "{{ APP_BUILD }}"
#      pb_app_name: "{{ APP_NAME }}"
    pb_down_artifact: "{{ APP_BUILD }}"
    pb_app_name: "{{ APP_NAME }}"
    when: >
      APP_BUILD not in [ "-Keep-", "-None-" ]

  - role: pb-download
#    vars:
#      pb_down_artifact: "{{ lookup('env','VENDOR_PKG') }}"
#      pb_app_name: "vendor"
    pb_down_artifact: "{{ lookup('env','VENDOR_PKG') }}"
    pb_app_name: "vendor"
    when: >
      lookup('env','VENDOR_PKG') not in [ "-Keep-", "-None-" ]

@sivel
Copy link
Member

sivel commented Jul 21, 2020

Duplicate of #67718

@sivel sivel marked this as a duplicate of #67718 Jul 21, 2020
@sivel sivel closed this as completed Jul 21, 2020
@bcoca bcoca removed the needs_triage Needs a first human triage before being processed. label Jul 21, 2020
@ansible ansible locked and limited conversation to collaborators Aug 18, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
affects_2.9 This issue/PR affects Ansible v2.9 bug This issue/PR relates to a bug. needs_info This issue requires further information. Please answer any outstanding questions. needs_template This issue/PR has an incomplete description. Please fill in the proposed template correctly. python3 support:core This issue/PR relates to code supported by the Ansible Engineering Team.
Projects
None yet
Development

No branches or pull requests

4 participants