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

YAML extra vars don't appear to work #46052

Closed
NotAFile opened this issue Sep 23, 2018 · 7 comments
Closed

YAML extra vars don't appear to work #46052

NotAFile opened this issue Sep 23, 2018 · 7 comments
Assignees
Labels
affects_2.6 This issue/PR affects Ansible v2.6 bug This issue/PR relates to a bug. docs This issue/PR relates to or includes documentation. support:core This issue/PR relates to code supported by the Ansible Engineering Team.

Comments

@NotAFile
Copy link

NotAFile commented Sep 23, 2018

SUMMARY

When specifying extra vars in yaml format, they remain undefined

ISSUE TYPE
  • Bug Report
COMPONENT NAME

core

ANSIBLE VERSION
ansible 2.6.4
  config file = /home/notafile/Documents/gb/ansible/ansible.cfg
  configured module search path = [u'/home/notafile/.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.15+ (default, Aug 31 2018, 11:56:52) [GCC 8.2.0]
CONFIGURATION
ANSIBLE_NOCOWS(/home/notafile/Documents/gb/ansible/ansible.cfg) = True
CACHE_PLUGIN(/home/notafile/Documents/gb/ansible/ansible.cfg) = jsonfile
CACHE_PLUGIN_CONNECTION(/home/notafile/Documents/gb/ansible/ansible.cfg) = /tmp/facts_cache
CACHE_PLUGIN_TIMEOUT(/home/notafile/Documents/gb/ansible/ansible.cfg) = 500
DEFAULT_GATHERING(/home/notafile/Documents/gb/ansible/ansible.cfg) = smart
DEFAULT_HOST_LIST(/home/notafile/Documents/gb/ansible/ansible.cfg) = [u'/home/notafile/Documents/gb/ansible/hosts']
DEFAULT_REMOTE_USER(/home/notafile/Documents/gb/ansible/ansible.cfg) = ansible
OS / ENVIRONMENT

Debian Testing

STEPS TO REPRODUCE

Run below playbook with ansible-playbook test.yml -e "test_var: hi"

Compare with ansible-playbook test.yml -e "test_var=hi"

- hosts: localhost
  tasks:
  - debug: var=test_var

It's hard for me to believe this feature wouldn't work considering plenty of people are likely using it. However, this usage seems to be perfectly inline with the examples in the documentation.

EXPECTED RESULTS
ok: [localhost] => {
    "test_var": "hi"
}
ACTUAL RESULTS
ok: [localhost] => {
    "test_var": "VARIABLE IS NOT DEFINED!: 'test_var' is undefined"
}
ansible-playbook 2.6.4
  config file = /home/notafile/Documents/gb/ansible/ansible.cfg
  configured module search path = [u'/home/notafile/.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.15+ (default, Aug 31 2018, 11:56:52) [GCC 8.2.0]
Using /home/notafile/Documents/gb/ansible/ansible.cfg as config file
Parsed /home/notafile/Documents/gb/ansible/hosts inventory source with ini plugin

PLAYBOOK: test.yml ************************************************************************************************************************************************************************************************************************************************************
1 plays in test.yml

PLAY [localhost] **************************************************************************************************************************************************************************************************************************************************************
META: ran handlers

TASK [debug] ******************************************************************************************************************************************************************************************************************************************************************
task path: /home/notafile/Documents/gb/ansible/test.yml:3
ok: [localhost] => {
    "test_var": "VARIABLE IS NOT DEFINED!: 'test_var' is undefined"
}
META: ran handlers
META: ran handlers

PLAY RECAP ********************************************************************************************************************************************************************************************************************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=0   
@ansibot ansibot added affects_2.6 This issue/PR affects Ansible v2.6 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 Sep 23, 2018
@sivel
Copy link
Member

sivel commented Sep 24, 2018

As of right now, to load extra vars as YAML via direct input, requires JSON-like markup. This would require you to utilize:

-e "{test_var: hi}"

To indicate that this is YAML data.

The process at this point is to:

  1. check if the value starts with @ and load it as a YAML file.
  2. check if the value starts with either [ or { and load it as YAML
  3. otherwise load it as key=value syntax.

It's possible that this could be changed to just try loading as YAML between 2 and 3 and gracefully fail.

@sivel sivel removed the needs_triage Needs a first human triage before being processed. label Sep 24, 2018
@NotAFile
Copy link
Author

In this case, perhaps the docs should be adjusted to not show examples where yaml input is used without {} syntax for now?

@jhnlsn
Copy link

jhnlsn commented Mar 9, 2019

Came across this same situation. It looks like -e does not like yaml string. If this is not the case, can the docs be adjusted to omit the yaml parsing example?

@epicanthal
Copy link
Contributor

epicanthal commented Jun 24, 2019

Thank you @sivel, I will adjust how I'm using --extra-vars. The below behavior (Results 3) had been haunting me for a while.

Use the intersect filter with --extra-vars:

# playbook: test_instersect_filter.yml
- hosts: localhost

  vars:
    list1: [apple, apples, orange, grape, grapes, grape, apples, grape_juice]
    list2: [apple, grape_juice]

  tasks:
    - name: Intersect Filter
      set_fact:
        results_intersect: "{{ list1 | intersect(list2) }}"

    - name: Assert Intersect Filter Results
      assert:
        that:
          - results_intersect == ['apple', 'grape_juice']

Results 1 - Works as expected:

$ ansible-playbook test_intersect_filter.yml -i hosts.ini -v
TASK [Intersect Filter] *******************************************************************************
ok: [localhost] => {
    "ansible_facts": {
        "results_intersect": [
            "apple", 
            "grape_juice"
        ]
    }, 
    "changed": false
}

TASK [Assert Intersect Filter Results] ****************************************************************
ok: [localhost] => {
    "changed": false
}

MSG:

All assertions passed

Results 2 - Works as expected based on sivel's YAML formatting example above:

$ ansible-playbook test_intersect_filter.yml -i hosts.ini -v -e "{list2: [apple,grape_juice]}"

Results 3 - Fails as 'grape' was incorrectly include with results:

$ ansible-playbook test_intersect_filter.yml -i hosts.ini -v -e "list2=[apple,grape_juice]"
TASK [Intersect Filter] *******************************************************************************
ok: [localhost] => {
    "ansible_facts": {
        "results_intersect": [
            "apple", 
            "grape", 
            "grape_juice"
        ]
    }, 
    "changed": false
}

TASK [Assert Intersect Filter Results] ****************************************************************
fatal: [localhost]: FAILED! => {
    "assertion": "results_intersect == ['apple', 'grape_juice']", 
    "changed": false, 
    "evaluated_to": false
}

MSG:

Assertion failed

@relrod
Copy link
Member

relrod commented Oct 23, 2020

We should update docs to indicate that simple YAML can be used on the CLI. For more advanced things, a file should be used.

@relrod relrod added the docs This issue/PR relates to or includes documentation. label Oct 23, 2020
@Akasurde
Copy link
Member

@relrod @sivel I think this is already mentioned Defining variables at runtime.

So can we close this issue?

@NotAFile
Copy link
Author

looks like it, thanks!

@Akasurde Akasurde self-assigned this Jan 22, 2021
@ansible ansible locked as resolved and limited conversation to collaborators Jan 22, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
affects_2.6 This issue/PR affects Ansible v2.6 bug This issue/PR relates to a bug. docs This issue/PR relates to or includes documentation. support:core This issue/PR relates to code supported by the Ansible Engineering Team.
Projects
None yet
Development

No branches or pull requests

7 participants