Questions regarding "when: condition"

45 views
Skip to first unread message

John Harmon

unread,
Jan 22, 2018, 5:55:22 PM1/22/18
to Ansible Project
The commented section of the following code works.  I was trying to simplify it by adding with_items; however, it seems to change the way it is evaluated?... at least that is my guess based on the verbosity:
skipping: [ansible-oel6] => (item=ansible)  => {
   
"changed": false,
   
"item": "ansible",
   
"skip_reason": "Conditional result was False"
}

Playbook.
---
# tasks file for PS1_customize
- name: Appending customization to /etc/bashrc.  Logout/in to see changes
  blockinfile
:
    path
: /etc/bashrc
    backup
: no
    marker
: "# {mark} ANSIBLE MANAGED BLOCK - ps1_customizations"
    block
: |

     
#If id command returns zero, you've root access.
     
if [ $(id -u) -eq 0 ]; then # set red color prompt for root
        PS1
="\\[$(tput setaf 1)\\]\\u@\\h:\\w # \\[$(tput sgr0)\\]"
     
elif [ $(id -u) -eq 4000  ]; then
       
# Configure special user with green
        PS1
="\[$(tput bold)\]\[$(tput setaf 2)\]\\u@\\h:\\w \\$ \[$(tput sgr0)\]"
     
else
        PS1
="\\u@\\h:\\w \$ "
     
fi
 
when: ansible_hostname | lower | search("item")
#  when: ansible_hostname | lower | search("prod") or
#        ansible_hostname | lower | search("backup") or
#        ansible_hostname | lower | search("nfs") or
#        ansible_hostname | lower | search("ansible")
  with_items
:
   
- prod
   
- backup
   
- nfs
   
- ansible
  failed_when
: false

So, I have a couple of questions.
1.  What would be the proper way to see if the hostname contains "splat" using with_items? 
2.  What would be the reverse of this?  (ie not in, or not contained, etc)

In summary, I am setting production and a list of other servers to red (for root user prompt).  On non-production servers, I will be setting it to yellow (hence the reason for question #2).

Matt Martz

unread,
Jan 22, 2018, 5:58:53 PM1/22/18
to ansible...@googlegroups.com
Using `with_items` like you are, you would need to use `search(item)` without the quotes around item.  Otherwise "item" is treated as the string item, instead of the variable called item.

--
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+unsubscribe@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/12ce80a6-bc0e-4edd-b903-1e4fa44e6546%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Matt Martz
@sivel
sivel.net

John Harmon

unread,
Jan 22, 2018, 6:02:14 PM1/22/18
to Ansible Project
That is good to know, but I made the change and still experience the issue; however, with that info I changed it to the following which works for the first section:

when: item in ansible_hostname
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.

Kai Stian Olstad

unread,
Jan 23, 2018, 2:35:15 PM1/23/18
to ansible...@googlegroups.com
On Monday, 22 January 2018 23.55.22 CET John Harmon wrote:
> when: ansible_hostname | lower | search("item")
> # when: ansible_hostname | lower | search("prod") or
> # ansible_hostname | lower | search("backup") or
> # ansible_hostname | lower | search("nfs") or
> # ansible_hostname | lower | search("ansible")
> with_items:
> - prod
> - backup
> - nfs
> - ansible
> failed_when: false
>
> So, I have a couple of questions.
> 1. What would be the proper way to see if the hostname contains "splat"
> using with_items?

You got the answer from Matt, and that should work.

An alternative is to not use with_items but just use regex or.

when: ansible_hostname | lower is search("prod|backup|nfs|ansible")

> 2. What would be the reverse of this? (ie not in, or not contained, etc)

when: not (ansible_hostname | lower is search("prod|backup|nfs|ansible"))


--
Kai Stian Olstad

John Harmon

unread,
Jan 23, 2018, 2:36:56 PM1/23/18
to Ansible Project
Oh, I like that.  Thank you.
Reply all
Reply to author
Forward
0 new messages