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

ternary() filter does not appear to work as documented #10763

Closed
candlerb opened this issue Apr 18, 2015 · 3 comments
Closed

ternary() filter does not appear to work as documented #10763

candlerb opened this issue Apr 18, 2015 · 3 comments

Comments

@candlerb
Copy link
Contributor

Issue Type:

Bug Report

Ansible Version:
ansible 2.0.0 (devel f4172fb9da) last updated 2015/04/18 09:55:10 (GMT +100)
  lib/ansible/modules/core: (detached HEAD 2970b339eb) last updated 2014/11/06 13:57:19 (GMT +100)
  lib/ansible/modules/extras: (detached HEAD ad181b7aa9) last updated 2014/11/06 13:57:33 (GMT +100)
  v2/ansible/modules/core: (detached HEAD cb69744bce) last updated 2014/11/06 13:57:45 (GMT +100)
  v2/ansible/modules/extras: (detached HEAD 8a4f07eecd) last updated 2014/11/06 13:57:58 (GMT +100)
  configured module search path = None
Environment:

OSX 10.9.5

Summary:

The ternary() filter does not appear to work as documented

Steps To Reproduce:

Use this playbook:

- hosts: localhost
  vars:
    foo: "{{ ansible_os_family == 'Debian' | ternary('Mr','Ms') }}"
  tasks:
    - set_fact: bar="{{ ansible_os_family == 'Debian' | ternary('Mr','Ms') }}"
    - debug: var=foo
    - debug: var=bar
Expected Results:

That the variables foo and bar would be set to either "Mr" or "Mrs". Instead they are set to the string "False"

Actual Results:
TASK: [set_fact bar="{{ ansible_os_family == "Debian" | ternary('Mr','Ms') }}"] ***
ok: [localhost]

TASK: [debug var=foo] *********************************************************
ok: [localhost] => {
    "var": {
        "foo": "False"
    }
}

TASK: [debug var=bar] *********************************************************
ok: [localhost] => {
    "var": {
        "bar": "False"
    }
}
@MrMMorris
Copy link

Yes! I was running into this too:

- docker:
    image: "company/tv-server:{{ item }}-latest"
    links:
      - "{{ 'production' in node_envs | ternary('foo' + item, omit) }}"
  with_items: node_envs

msg: Docker API Error: Could not get container for True

sivel added a commit to sivel/ansible that referenced this issue Apr 18, 2015
sivel added a commit to sivel/ansible that referenced this issue Apr 18, 2015
@sivel
Copy link
Member

sivel commented Apr 18, 2015

Take a look at #10767

I've submitted a pull request to update the docs.

@candlerb, what you need is: "{{ (ansible_os_family == 'Debian') | ternary('Mr','Ms') }}" to apply the ternary filter to the evaluation instead of just the string 'Debian'.

The addition is the () around the evaluation of ansible_os_family == 'Debian'

@candlerb
Copy link
Contributor Author

That works, thank you.

I found there is an open bug on jinja2 for not documenting operator precedence. I believe that it implicitly uses the same operator precedences as python, which would be reasonable, except that jinja2 uses the pipe operator for applying filters to expressions, not bitwise-OR of its operands.

bcoca added a commit that referenced this issue Apr 19, 2015
Correct the ternary example. Fixes #10763
sat01a added a commit to AtlasOfLivingAustralia/ala-install that referenced this issue Jan 8, 2016
@ansible ansible locked and limited conversation to collaborators Apr 25, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants