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

import_role with extra-vars option #69097

Closed
Altiire opened this issue Apr 22, 2020 · 9 comments · Fixed by #75269
Closed

import_role with extra-vars option #69097

Altiire opened this issue Apr 22, 2020 · 9 comments · Fixed by #75269
Labels
affects_2.9 This issue/PR affects Ansible v2.9 bug This issue/PR relates to a bug. module This issue/PR relates to a module. P3 Priority 3 - Approved, No Time Limitation support:core This issue/PR relates to code supported by the Ansible Engineering Team. utilities Utilities category

Comments

@Altiire
Copy link

Altiire commented Apr 22, 2020

SUMMARY

variable from extra-vars is not set while import_role is parsed

ISSUE TYPE
  • Bug Report
COMPONENT NAME

import_role

ANSIBLE VERSION
ansible 2.9.7
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/home/altiire/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/dist-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.16 (default, Oct 10 2019, 22:02:15) [GCC 8.3.0]
CONFIGURATION
ALLOW_WORLD_READABLE_TMPFILES(/etc/ansible/ansible.cfg) = True
OS / ENVIRONMENT

Debian 10 hosted on GCP compute engine

STEPS TO REPRODUCE
ansible-playbook --connection=local --inventory 127.0.0.1, deploy.yml --tags "docker-compose" --extra-vars '{"deployment_name": test,"del_services": [webserver]}'
---
- name: Create or update a deployment
  hosts: all
  tasks:
    - name: import deploy role with its overriden variables
      import_role:
        name: deploy
        vars_from: "{{ deployment_name }}"
EXPECTED RESULTS

run tasks specified by the tags

ACTUAL RESULTS
ansible-playbook 2.9.7
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/home/altiire/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/dist-packages/ansible
  executable location = /usr/bin/ansible-playbook
  python version = 2.7.16 (default, Oct 10 2019, 22:02:15) [GCC 8.3.0]
Using /etc/ansible/ansible.cfg as config file
setting up inventory plugins
Set default localhost to 127.0.0.1
Parsed 127.0.0.1, inventory source with host_list plugin
ERROR! Could not find specified file in role: vars/{{ deployment_name }}
@sivel
Copy link
Member

sivel commented Apr 22, 2020

I'm not positive if there is a resolution to this. It may not be feasible to consult extra vars at this phase of the playbook run. I'd recommend that you switch to include_role instead of import_role to resolve this issue.

@bcoca bcoca added bug This issue/PR relates to a bug. needs_triage Needs a first human triage before being processed. support:core This issue/PR relates to code supported by the Ansible Engineering Team. labels Apr 22, 2020
@Altiire
Copy link
Author

Altiire commented Apr 23, 2020

Tags are not taken into account if I use include_role

@sivel
Copy link
Member

sivel commented Apr 23, 2020

Tags are not taken into account if I use include_role

Look into using apply for include_role, which allows you to apply tags to the included tasks.

@bcoca bcoca added needs_verified This issue needs to be verified/reproduced by maintainer P3 Priority 3 - Approved, No Time Limitation and removed needs_triage Needs a first human triage before being processed. labels Apr 23, 2020
@ansibot ansibot added affects_2.9 This issue/PR affects Ansible v2.9 module This issue/PR relates to a module. utilities Utilities category labels Apr 24, 2020
@Altiire
Copy link
Author

Altiire commented Apr 27, 2020

I can't succeed to make it works

my deploy.yml file

---
- name: Create or update a deployment
  hosts: all

  vars:
    - deployment_template_path: "{{ deployment_path }}/deployment_template"
    - current_deployment_path: "{{ deployment_path }}/{{ deployment_name }}"

  tasks:
    - name: include deploy role with its overriden variables
      include_role:
        name: deploy
        vars_from: "{{ deployment_name }}"
        apply:
          tags:
            - dockercompose

and my roles/deploy/tasks/main.yml

---
- fail:
    msg: The variable 'locale' is not set
  when: locale is none
- debug:
    msg: The variable 'sftp_password' is not set
  when: sftp_password is none
- include: stop-inotify.yml
- include: files.yml
- include: common_database.yml
- include: network.yml
- include: sftp.yml
- include_tasks:
    file: docker.yml
    apply:
      tags:
        - dockercompose
  tags:
    - dockercompose
- include: stop-inotify.yml
- include: backend.yml
- include: db.yml
- include: module_seed.yml
- include: office.yml
- include: start-inotify.yml
- include: cron.yml

My tasks in docker.yml are not executed

@sivel
Copy link
Member

sivel commented Apr 27, 2020

apply only affects the tasks within the deploy role. It does not tag the include_role itself. As such if you are using -t dockercompose from the CLI, it will skip the include_rolebecause it is not tagged.

You would need to use something like:

    - name: include deploy role with its overriden variables
      include_role:
        name: deploy
        vars_from: "{{ deployment_name }}"
        apply:
          tags:
            - dockercompose
      tags:
        - dockercompose

@Altiire
Copy link
Author

Altiire commented Apr 27, 2020

But if I set tags on the include_role, all my tasks in the role get tagged too.

PLAY [Create or update a deployment] *******************************************************************************************************************************************************************************

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

TASK [include deploy role with its overriden variables] ************************************************************************************************************************************************************

TASK [deploy : fail] ***********************************************************************************************************************************************************************************************
skipping: [127.0.0.1]

TASK [deploy : debug] **********************************************************************************************************************************************************************************************
ok: [127.0.0.1] => {
    "msg": "The variable 'sftp_password' is not set"
}

TASK [deploy : Stop inotify service] *******************************************************************************************************************************************************************************
changed: [127.0.0.1]

@sivel
Copy link
Member

sivel commented Apr 27, 2020

But if I set tags on the include_role, all my tasks in the role get tagged too

No, that is what apply does.

I think we've confused something here. You stated:

Tags are not taken into account if I use include_role

Which I assumed meant you wanted to use tags: on that include_role, but I think what you are saying, is that if a task is tagged somewhere inside of the included role, that it isn't found. I think what you want instead is to use the always tag:

    - name: include deploy role with its overriden variables
      include_role:
        name: deploy
        vars_from: "{{ deployment_name }}"
      tags:
        - always

Then the include_role always happens, and can then still filter tags that are specified on tasks explicitly within the role.

@Altiire
Copy link
Author

Altiire commented Apr 27, 2020

I was thinking of the always tag too but the real problem was the story with apply/tags.

Thank you very much, it is now working and I learned a few things.

I let the issue open for the original issue.

Regards,

@xammi
Copy link

xammi commented Jun 5, 2020

+1 user with this problem;

Have changed "include_role" on "import_role" and nothing good happened.

Created my own issue here - #69916

@ansible ansible locked and limited conversation to collaborators Sep 27, 2021
@sivel sivel removed the needs_verified This issue needs to be verified/reproduced by maintainer label Feb 3, 2022
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. module This issue/PR relates to a module. P3 Priority 3 - Approved, No Time Limitation support:core This issue/PR relates to code supported by the Ansible Engineering Team. utilities Utilities category
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants