I sense that "with templating" does as expected?

% ansible --version

ansible [core 2.16.4]

  config file = None

  configured module search path = ['/Users/wrowe/.ansible/plugins/modules', 
'/usr/share/ansible/plugins/modules']

  ansible python module location = 
/opt/homebrew/lib/python3.11/site-packages/ansible

  ansible collection location = 
/Users/wrowe/.ansible/collections:/usr/share/ansible/collections

  executable location = /opt/homebrew/bin/ansible

  python version = 3.11.8 (main, Feb  6 2024, 21:21:21) [Clang 15.0.0 
(clang-1500.1.0.2.5)] (/opt/homebrew/opt/python@3.11/bin/python3.11)

  jinja version = 3.1.3

  libyaml = True



% ansible-playbook foo.yaml -i localhost,


PLAY [localhost] 
*******************************************************************************************************


TASK [without templating] 
**********************************************************************************************

ok: [localhost] => (item=varA) => {

    "ansible_loop_var": "item",

    "changed": false,

    "item": "varA",

    "msg": "All assertions passed"

}

ok: [localhost] => (item=varB) => {

    "ansible_loop_var": "item",

    "changed": false,

    "item": "varB",

    "msg": "All assertions passed"

}


TASK [with templating] 
*************************************************************************************************

failed: [localhost] (item=varA) => {

    "ansible_loop_var": "item",

    "assertion": "varA is defined",

    "changed": false,

    "evaluated_to": false,

    "item": "varA",

    "msg": "Assertion failed"

}

failed: [localhost] (item=varB) => {

    "ansible_loop_var": "item",

    "assertion": "varB is defined",

    "changed": false,

    "evaluated_to": false,

    "item": "varB",

    "msg": "Assertion failed"

}


PLAY RECAP 
*************************************************************************************************************

localhost                  : ok=1    changed=0    unreachable=0    failed=1    
skipped=0    rescued=0    ignored=0

Walter
--
Walter Rowe, Division Chief
Infrastructure Services Division
Mobile: 202.355.4123

On Mar 13, 2024, at 4:55 PM, Todd Lewis <uto...@gmail.com> wrote:

Maybe it will facilitate understanding what's going on if we name things what 
they really are:

---
- hosts: localhost
  gather_facts: no
  tasks:
  - name: Strings in a loop are indeed defined
    ignore_errors: true
    assert:
      that: item is defined
    loop:
      - RandomStringA
      - RandomStringB
      - ansible_forks

  - name: Strings in a loop are unlikely to be variable names
    ignore_errors: true
    assert:
      that: '{{ item }} is defined'
    loop:
      - UndefinedVariableNameA
      - UndefinedVariableNameB
      - ansible_forks




On 3/13/24 2:27 PM, rjwagn...@gmail.com<mailto:rjwagn...@gmail.com> wrote:
Hi all - I recently upgraded to Ansible core 2.15, and started hitting 
"Conditional is marked as unsafe, and cannot be evaluated."  As discussed at 
https://docs.ansible.com/ansible/latest/porting_guides/porting_guide_9.html, I 
removed templating from when and assert.  All seemed fine, but then I 
discovered loop variables don't appear to be interpolated in assert without 
templating.

As an example, consider the following playbook that aims to confirm two 
variables, varA and varB, are defined (in the sample output, they are not 
defined):
#> cat b.yml
---
- hosts: localhost
  gather_facts: no
  tasks:
  - name: without templating
    assert:
      that: item is defined
    loop:
      - varA
      - varB
  - name: with templating
    assert:
      that: '{{ item }} is defined'
    loop:
      - varA
      - varB

#> ansible-playbook b.yml
[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that 
the
implicit localhost does not match 'all'

PLAY [localhost] 
**********************************************************************

TASK [without templating] 
*************************************************************
ok: [localhost] => (item=varA) => {
    "ansible_loop_var": "item",
    "changed": false,
    "item": "varA",
    "msg": "All assertions passed"
}
ok: [localhost] => (item=varB) => {
    "ansible_loop_var": "item",
    "changed": false,
    "item": "varB",
    "msg": "All assertions passed"
}

TASK [with templating] 
****************************************************************
failed: [localhost] (item=varA) => {
    "ansible_loop_var": "item",
    "assertion": "varA is defined",
    "changed": false,
    "evaluated_to": false,
    "item": "varA",
    "msg": "Assertion failed"
}
failed: [localhost] (item=varB) => {
    "ansible_loop_var": "item",
    "assertion": "varB is defined",
    "changed": false,
    "evaluated_to": false,
    "item": "varB",
    "msg": "Assertion failed"
}

Why is templating required to interpolate the loop variable but not other (non 
loop) variables?  Furthermore, is there a way to write this task without 
templating and get the expected behavior?

Thanks
Rob
--
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to 
ansible-project+unsubscr...@googlegroups.com<mailto:ansible-project+unsubscr...@googlegroups.com>.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/d23f849b-0342-47d8-b2f4-0f1634cdf866n%40googlegroups.com<https://groups.google.com/d/msgid/ansible-project/d23f849b-0342-47d8-b2f4-0f1634cdf866n%40googlegroups.com?utm_medium=email&utm_source=footer>.


--
Todd

--
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to 
ansible-project+unsubscr...@googlegroups.com<mailto:ansible-project+unsubscr...@googlegroups.com>.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/4ea45f07-c044-4b61-a244-e6f2d5980ed6%40gmail.com<https://groups.google.com/d/msgid/ansible-project/4ea45f07-c044-4b61-a244-e6f2d5980ed6%40gmail.com?utm_medium=email&utm_source=footer>.

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to ansible-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/A767305C-C137-4E0B-B1FD-DAAC2DDD32A6%40nist.gov.

Reply via email to