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

vars_files cannot see facts or other variables that a role in same play can #28024

Closed
pszabop opened this issue Aug 10, 2017 · 3 comments
Closed
Labels
affects_2.4 This issue/PR affects Ansible v2.4 bug This issue/PR relates to a bug. support:core This issue/PR relates to code supported by the Ansible Engineering Team.

Comments

@pszabop
Copy link

pszabop commented Aug 10, 2017

ISSUE TYPE
  • Bug Report
COMPONENT NAME

playbooks

ANSIBLE VERSION
ansible 2.4.0
  config file = None
  configured module search path = [u'/Users/szabop/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /Library/Python/2.7/site-packages/ansible
  executable location = /usr/local/bin/ansible
  python version = 2.7.10 (default, Jul 14 2015, 19:46:27) [GCC 4.2.1 Compatible Apple LLVM 6.0
CONFIGURATION

I'm importing roles and plays from another directory so I have in ansible.cfg

[defaults]
roles_path = ../../../deployment/playbooks/roles
OS / ENVIRONMENT

OSX 10.10.5

SUMMARY

When I try to dynamically decide which vault file I want to import by using a variable in vars_files (unit tests shouldn't be using production passwords!) for imported plays, I'm unable to do so using either group_vars, host_vars, or facts set in an setup play. I can only use an ansible command line argument.

STEPS TO REPRODUCE

ansible-playbook -t app_test -i inventories/localhost/local.hosts unit.yml

---
#
# network.yml
#
- name: Setup the networks
  hosts: dockerhosts
  vars_files:
     - demoVars.yml
     - "{{ vault_file }}"   # this will only be valid if declared on the ansible command line.
  roles:
    - role: debug     # note this will print out a valid {{ vault_file }} in all cases (but comment out the vars_files line or it will error first)
    - role: network_setup
      options:
        internal: "{{ networks.internal }}"
        external: "{{ networks.external }}"
        nginx: "{{ networks.nginx }}"
  tags:
    - setup
    - standalone
    - app_test
# unit.yml
---
- name: create facts used to compose variables later in this playbook
  hosts: dockerhosts
  vars:
    esTmpDir: "/tmp/es"
  roles:
    - role: unitenv_setup
  tags:
    - app_test
- import_playbook: ../../../deployment/playbooks/network.yml
EXPECTED RESULTS

If the variable vault_file is specified on the ansible command line using --extra-vars the script runs just fine. It should run just fine when vault_file is set from an inventory file or set as a fact in unitenv_setup, but it does not

ACTUAL RESULTS

Using any method besides the command line to set vault_file, I get the following error:

PLAY [Setup the networks] *********************************************************************
ERROR! vars file {{ vault_file }} was not found

extremely verbose mode:

ETA: ran handlers
META: ran handlers
Read vars_file 'demoVars.yml'
skipping vars_file '{{ vault_file }}' due to an undefined variable
Read vars_file 'demoVars.yml'
skipping vars_file '{{ vault_file }}' due to an undefined variable

PLAY [Setup the networks] *********************************************************************
Read vars_file 'demoVars.yml'
ERROR! vars file {{ vault_file }} was not found

@ansibot ansibot added affects_2.4 This issue/PR affects Ansible v2.4 bug_report 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 Aug 10, 2017
@pszabop
Copy link
Author

pszabop commented Aug 11, 2017

I note there's something complex about my production environment that I can't seem to get into a simple sharable reproduction. At any rate, here's the attempt to reproduce.

@bcoca bcoca removed the needs_triage Needs a first human triage before being processed. label Aug 11, 2017
@bcoca
Copy link
Member

bcoca commented Aug 11, 2017

Possible Misunderstanding

Hi!

Thanks very much for your submission to Ansible. It sincerely means a lot to us.

We believe the ticket you have filed is being somewhat misunderstood, as one thing works a little differently than stated.

facts, host and group vars .. basically any 'host associated vars' are not available for vars_files, only play scoped vars or extra vars are available at that point. Use an include_vars task instead.

In the future, this might be a topic more well suited for the user list, which you can also post here if you'd like some more help with the above.

Thank you once again for this and your interest in Ansible!

@bcoca bcoca closed this as completed Aug 11, 2017
@pszabop
Copy link
Author

pszabop commented Aug 11, 2017

perhaps you should fix the documentation? Not exactly obvious...

alanorth added a commit to ilri/rmg-ansible-public that referenced this issue Nov 19, 2017
We've relied on ansible's ability to use host-based facts to include
variables for quite some time. This functionality is apparently not
supported and has been silently failing for a while now. If you run
your playbooks with -vvvv you will see messages like:

skipping vars_file 'vars/{{ ansible_os_family }}.yml' due to an undefined variable

The solution seems to be to move these includes to an include_vars
task inside the role. The "always" tag means it always runs.

See: ansible/ansible#28024
@ansibot ansibot added bug This issue/PR relates to a bug. and removed bug_report labels Mar 7, 2018
@ansible ansible locked and limited conversation to collaborators Apr 26, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
affects_2.4 This issue/PR affects Ansible v2.4 bug This issue/PR relates to a bug. support:core This issue/PR relates to code supported by the Ansible Engineering Team.
Projects
None yet
Development

No branches or pull requests

3 participants