Re: [ansible-project] Conditionals within roles

2023-03-23 Thread Chip Burke
Wow, I didn't know ternary existed. Jinja looks more flexible, but I think 
for this case the same line with different ternary statements should do the 
job. Thanks!

On Thursday, March 23, 2023 at 6:12:58 AM UTC-4 Dick Visser wrote:

> Use ternary, or some inline jinja statements
>
> On Wed, 22 Mar 2023 at 23:12, Chip Burke  wrote:
> >
> > I have a playbook with a role
> >
> > - hosts: somehost
> > roles:
> > - role: community.some.role
> > hostname: "{{ansible_hostname}}"
> > server: 192.168.1.2
> > api_server_url: https://some.server.com
> > url_path: /api
> > api_use_ssl: yes
> > userparameters:
> > - name: SomeName
> >
> > The last task I want to be dynamic. Based on a fact, I want SomeName to 
> change to a null, File1, File2, File3, etc.
> >
> > How does one do this in Ansible?
> >
> > --
> > 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-proje...@googlegroups.com.
> > To view this discussion on the web visit 
> https://groups.google.com/d/msgid/ansible-project/b393898b-2837-46ab-bda3-190323762280n%40googlegroups.com
> .
>

-- 
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/7cd37294-af94-4bc5-a338-62614d9dfb0dn%40googlegroups.com.


Re: [ansible-project] Conditionals within roles

2023-03-23 Thread Dick Visser
Use ternary, or some inline jinja statements

On Wed, 22 Mar 2023 at 23:12, Chip Burke  wrote:
>
> I have a playbook with a role
>
> - hosts: somehost
>   roles:
> - role: community.some.role
>   hostname: "{{ansible_hostname}}"
>   server: 192.168.1.2
>   api_server_url: https://some.server.com
>   url_path: /api
>   api_use_ssl: yes
>   userparameters:
> - name: SomeName
>
> The last task I want to be dynamic. Based on a fact, I want SomeName to 
> change to a null, File1, File2, File3, etc.
>
> How does one do this in Ansible?
>
> --
> 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/b393898b-2837-46ab-bda3-190323762280n%40googlegroups.com.

-- 
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/CAF8BbLbXEZ7LZg6MJYyykcp9D1ADBQm-1b48zZgO-%3D2GQVMa6Q%40mail.gmail.com.


[ansible-project] Conditionals within roles

2023-03-22 Thread Chip Burke
I have a playbook with a role

- hosts: somehost
  roles:
- role: community.some.role
  hostname: "{{ansible_hostname}}"
  server: 192.168.1.2
  api_server_url: https://some.server.com
  url_path: /api
  api_use_ssl: yes
  userparameters:
- name: SomeName

The last task I want to be dynamic. Based on a fact, I want SomeName to 
change to a null, File1, File2, File3, etc. 

How does one do this in Ansible? 

-- 
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/b393898b-2837-46ab-bda3-190323762280n%40googlegroups.com.


[ansible-project] Conditionals using Ansible_facts.disks from the win_disk_facts

2020-07-06 Thread Adam Dyson
Hello world!

Hopefully a relatively simple one, I am attempting to add a when condition 
on a task that will only happen if a certain disk exists on the machine. I 
want the following task to only take place if Ansible facts finds a disk 
with the number 4. however I am struggling to find the right syntax. Has 
anyone done this and know the answer?

- name: Create Partition
  win_partition:
   drive_letter:F
   partition_size: '-1'
   Disk_Number: 4
   state: present
  when:
  - Ansible_facts.disks[4]



-- 
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/4497f2d8-d308-4964-80b9-34342a94efefo%40googlegroups.com.


Re: [ansible-project] conditionals

2019-03-11 Thread Sebastian Meyer
Hi Jeffrey,

On 11.03.19 20:50, Jeffrey Agnitsch wrote:
> - name: vmotion {{ inventory_hostname }}

You'll also need "" here.

- name: "vmotion {{ inventory_hostname }}"

And this will give you only the first hostname as name for the task.

This works for me:

Playbook (without the linebreak in the when: line):

---
- hosts: all
  tasks:
- name: "foobar {{ inventory_hostname }}"
  debug:
  delegate_to: localhost
  when: inventory_hostname | regex_replace('.*([0-9]+).*', '\\1') |
int  > 1


Inventory:

host1
host2

Output:

$ ansible-playbook -i /tmp/hosts /tmp/test.yml

PLAY [all]
***

TASK [Gathering Facts]
***
ok: [host2]
ok: [host1]

TASK [foobar host1]
***
skipping: [host1]
ok: [host2 -> localhost] => {
"msg": "Hello world!"
}

PLAY RECAP
***
host1  : ok=1changed=0unreachable=0failed=0
host2  : ok=1changed=0unreachable=0failed=0


Hope that helps,
Sebastian

-- 
Sebastian Meyer
Linux Consultant & Trainer
Mail: me...@b1-systems.de

B1 Systems GmbH
Osterfeldstraße 7 / 85088 Vohburg / http://www.b1-systems.de
GF: Ralph Dehner / Unternehmenssitz: Vohburg / AG: Ingolstadt,HRB 3537

-- 
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 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/2db654d8-0af7-97b0-6351-27f58c487b54%40b1-systems.de.
For more options, visit https://groups.google.com/d/optout.


[ansible-project] conditionals

2019-03-11 Thread Jeffrey Agnitsch
Trying to do a conditional that when my inventory host name once converted 
to a string is greater than a number 

Inventory file
server1
server2
server3 - 
..
..
..
..
..





server205



this is the important parts of the role

- name: vmotion {{ inventory_hostname }}
  vmware_vmotion:
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}
password: "{{ vcenter_password }}"
vm_name: "{{ inventory_hostname }}"
destination_datastore: "TP1"
  when: "`(inventory_hostname | regex_replace('.*([0-9]+).*', '\\1') | 
int)  > 150`"
  delegate_to: localhost

- name: vmotion {{ inventory_hostname }}
  vmware_vmotion:
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}@scriptpro.com"
password: "{{ vcenter_password }}"
vm_name: "{{ inventory_hostname }}"
destination_datastore: "TP2"
  when: "`(inventory_hostname | regex_replace('.*([0-9]+).*', '\\1') | 
int)  180`"
  delegate_to: localhost


it fails with 


": "The conditional check '`(inventory_hostname | 
regex_replace('.*([0-9]+).*', '\\1') | int) is even`' failed. The error 
was: template error while templating string: unexpected char u'`' at 6. 
String: {% if `(inventory_hostname | regex_replace('.*([0-9]+).*', '\\1') | 
int) is even` %} True {% else %} False {% endif %}\n\nThe error appears to 
have been in '/etc/ansible/roles/vmotion_app/tasks/main.yml': line 2, 
column 3, but may\nbe elsewhere in the file depending on the exact syntax 
problem.\n\nThe offending line appears to be:\n\n---\n- name: vmotion {{ 
inventory_hostname }}\n  ^ here\nWe could be wrong, but this one looks like 
it might be an issue with\nmissing quotes.  Always quote template 
expression brackets when they\nstart a value. For instance:\n\n
with_items:\n  - {{ foo }}\n\nShould be written as:\n\n
with_items:\n  - \"{{ foo }}\"\n"}


any thoughts are appreciated 

-- 
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 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/8f11d72d-4078-437b-94b3-907e24e8ff29%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[ansible-project] Conditionals based on empty JSON dict

2017-06-19 Thread Panagiotis Atmatzidis
Hello list,

I'm trying to automate an API call that adds an 'A' record to the 
linode DNS service. The relevant playbook snippet is:


   - name: create linode 'A' record
  uri:
url: "https://api.linode.com/?api_key={{ linode_api_key 
}}_action={{ linode_api_action }}={{ linode_domain_id 
}}={{ linode_record_type }}={{ public_ip }}={{ 
instance_name }}_sec={{ linode_record_ttl }}"
return_content: yes
  register: json_resp
- name: set JSON response
  set_fact:
linode_api_error:   "{{ (json_resp.content|from_json).ERRORARRAY.0 
}}"
- fail: msg="Setting DNS entry failed with error '{{ 
linode_api_error.ERRORARRAY.0.ERRORMESSAGE }}'"
  when: (linode_api_error is defined)   # <=== The problem is here
- debug: msg="DNS A record set for '{{ instance_name  }}.host.com' 
targets to '{{ public_ip }}'"
  when: (linode_api_error not defined) # <=== The problem is here


A successful entry looks returns a JSON object that looks like this (the 
ERRORARRAY is empty):

{
  "ACTION": "domain.resource.create",
  "DATA": {
"ResourceID": 8016441
  },
  "ERRORARRAY": []
}

An unsuccessful entry looks like this:

{
  "ACTION": "domain.resource.create",
  "DATA": {},
  "ERRORARRAY": [
{
  "ERRORMESSAGE": "Authentication failed",
  "ERRORCODE": 4
}
  ]
}

After I figured how the API works[1] I opted for a check on the JSON 
"ERRORARRAY" dictionary. But every approach I took failed. What I'd like to 
achieve is the following:

1) If the "linode_api_key" is defined make the URI call (this is not 
implemented here in case you're wondering). The variable is set by 
linode_api_key: "{{ lookup('env','LINODE_API_KEY') }}" - not sure what 
happens when the env variable is not set.
2) If the "uri" call returns error (this can be checked only by the 
ERRORARRAY), fail.

Any suggestions on how to fix the code or handle this in a more optimised 
way are more than welcome!


[1]: My first thought was to test against status code (401 for failure and 
20x for success). But a failed request will still return a "200" status 
code. I believe that is a peculiar API design choice. I think that Facebook 
returns "200" for failed calls too.

-- 
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 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/1805ea6c-2d60-437b-ba05-08543d4ba700%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [ansible-project] Conditionals in sudo

2015-01-13 Thread Greg Andrews
I wonder if inventory variables might do what you need.  Have a look at the
Ansible docs page for them here
http://docs.ansible.com/intro_inventory.html, especially the part of the
page starting with Host Variables, but also the descriptions of files in
host_vars and group_vars directories starting with Splitting Out Host and
Group Specific Data.  One thing that's not mentioned in the page is that
the host_vars/all file will set variables for all hosts.

I haven't checked to see if the sudo: yes flag can be controlled in these
ways.  If it can, then this approach could be better than running tasks
twice.

  -Greg


On Sat, Jan 10, 2015 at 7:33 PM, Rajagopal V rajas...@gmail.com wrote:

 Hi,
 (Originally posted to Ansible Dev by mistake)

 Im a newbie trying to automate a few commands across boxes using Ansible.

 I would like the sudo command on certain tasks to be conditional, so
 depending on the node, I'd like to turn on sudo or not. For e.g.  I have a
 development machine (my laptop) and a stage server where I need certain
 sets of commands to be run. On the dev machine, I dont need any of the
 commands to be run as sudo but need them to run as sudo on the stage
 server.

 I thought something like

 - hosts: all
   tasks:
   - name: Execute Command X
 command: Command X
 sudo: inventory_hostname != 'localhost'

 My Hosts file contains entries like
 localhost
 demo7  ansible_ssh_host=... ansible_ssh_port=..

 would make the task not run as sudo on localhost (my dev machine) but
 would run with sudo on the other nodes. Unfortunately, this doesnt happen
 and it always treats this as sudo: False.

 Is it possible to have conditionals in sudo ?

 UPDATE: Looking through the source, this doesn't seem to be the case. The
 Expression doesnt seem to be evaluated and sent directly to utils.boolean.
 Is there any other way of achieving this goal other than running every
 task twice -- once with sudo and once without.


 Thanks
 Raja

 --
 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 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/cd3109da-57f5-4c0a-9812-29742c81726b%40googlegroups.com
 https://groups.google.com/d/msgid/ansible-project/cd3109da-57f5-4c0a-9812-29742c81726b%40googlegroups.com?utm_medium=emailutm_source=footer
 .
 For more options, visit https://groups.google.com/d/optout.


-- 
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 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/CAPdwyX78p9ApNr63VfOaai08de0cYx63eN%2BKfmN6A6x2KK-%2BfQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


[ansible-project] Conditionals in sudo

2015-01-13 Thread Rajagopal V
Hi,
(Originally posted to Ansible Dev by mistake)

Im a newbie trying to automate a few commands across boxes using Ansible. 

I would like the sudo command on certain tasks to be conditional, so 
depending on the node, I'd like to turn on sudo or not. For e.g.  I have a 
development machine (my laptop) and a stage server where I need certain 
sets of commands to be run. On the dev machine, I dont need any of the 
commands to be run as sudo but need them to run as sudo on the stage 
server. 

I thought something like

- hosts: all
  tasks:
  - name: Execute Command X
command: Command X
sudo: inventory_hostname != 'localhost'

My Hosts file contains entries like
localhost
demo7  ansible_ssh_host=... ansible_ssh_port=.. 

would make the task not run as sudo on localhost (my dev machine) but 
would run with sudo on the other nodes. Unfortunately, this doesnt happen 
and it always treats this as sudo: False.

Is it possible to have conditionals in sudo ?

UPDATE: Looking through the source, this doesn't seem to be the case. The 
Expression doesnt seem to be evaluated and sent directly to utils.boolean.
Is there any other way of achieving this goal other than running every task 
twice -- once with sudo and once without.


Thanks
Raja

-- 
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 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/cd3109da-57f5-4c0a-9812-29742c81726b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[ansible-project] Conditionals with facts

2014-09-29 Thread Rich Burroughs
Hi,

We use Puppet when I work and we've done a bit of orchestration with both 
Mcollective and Fabric. I'm very interested in using Ansible instead of Fabric 
since I've learned it can use Facter facts.

We have a mix of Solaris and Linux nodes, and for the Solaris nodes it looks 
like I need to set the path to sudo specifically for that to work with 
sudo_exe. This wasn't surprising as I had to do it with Fabric too.

So my question is basically, what's the best way to do that globally for all 
the Solaris hosts? It seems like I could leverage a fact to do that, both 
Facter and Ansible itself have facts I could use with a conditional somehow. 
But where would I put that? Or would it be better to just have a separate 
inventory file for them and do it there?

Another example is determining which environment (Dev/UAT/Prod) a host is in. 
That's a custom Facter fact we already set through Puppet, and seems like it 
would make sense to leverage that somehow. But the docs on the web site seem to 
indicate that it would be better to split my inventory into different files per 
environment.

I like the idea of using the same facts to make decisions in Puppet and Asible, 
so we know we're working off the same data with both tools. 


Thanks,

Rich 

-- 
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 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/c1ad0e29-e1b0-4a31-957d-64d1458b22a3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [ansible-project] Conditionals with facts

2014-09-29 Thread Michael DeHaan
On Sun, Sep 28, 2014 at 6:48 PM, Rich Burroughs r...@richburroughs.com
wrote:

 Hi,

 We use Puppet when I work and we've done a bit of orchestration with both
 Mcollective and Fabric. I'm very interested in using Ansible instead of
 Fabric since I've learned it can use Facter facts.


I'm sorry to hear about the tools you have to use.   That being said, I'll
answer the questions below :)




 We have a mix of Solaris and Linux nodes, and for the Solaris nodes it
 looks like I need to set the path to sudo specifically for that to work
 with sudo_exe.


There's a pull request in queue to make this work as an inventory
variable.  It's not merged yet.



 So my question is basically, what's the best way to do that globally for
 all the Solaris hosts? It seems like I could leverage a fact to do that,
 both Facter and Ansible itself have facts I could use with a conditional
 somehow. But where would I put that? Or would it be better to just have a
 separate inventory file for them and do it there?


You would normally define it in a group variable but you'd need the pull
request mentioned above as right now sudo_exe is a global setting.


 Another example is determining which environment (Dev/UAT/Prod) a host is
 in. That's a custom Facter fact we already set through Puppet, and seems
 like it would make sense to leverage that somehow. But the docs on the web
 site seem to indicate that it would be better to split my inventory into
 different files per environment.


Ansible likes to make this statement declaratively using host groups and
playbooks.

The playbooks describe what roles/tasks to apply to which groups of hosts.

You can also use the group_by module in Ansible to create dynamic groups
based on the values of certain variables such as the OS type.

-- 
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 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/CA%2BnsWgykFfFMNe5nWivTt_fYZ83OCQuFJ1nH9g_rXh97ksRG7Q%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: [ansible-project] Conditionals with facts

2014-09-29 Thread Rich Burroughs
Thanks, that was very helpful :)


Rich



On Monday, September 29, 2014 6:18:32 AM UTC-7, Michael DeHaan wrote:



 On Sun, Sep 28, 2014 at 6:48 PM, Rich Burroughs ri...@richburroughs.com 
 javascript: wrote:

 Hi,

 We use Puppet when I work and we've done a bit of orchestration with both 
 Mcollective and Fabric. I'm very interested in using Ansible instead of 
 Fabric since I've learned it can use Facter facts.


 I'm sorry to hear about the tools you have to use.   That being said, I'll 
 answer the questions below :)

  


 We have a mix of Solaris and Linux nodes, and for the Solaris nodes it 
 looks like I need to set the path to sudo specifically for that to work 
 with sudo_exe. 


 There's a pull request in queue to make this work as an inventory 
 variable.  It's not merged yet.
  


 So my question is basically, what's the best way to do that globally for 
 all the Solaris hosts? It seems like I could leverage a fact to do that, 
 both Facter and Ansible itself have facts I could use with a conditional 
 somehow. But where would I put that? Or would it be better to just have a 
 separate inventory file for them and do it there?


 You would normally define it in a group variable but you'd need the pull 
 request mentioned above as right now sudo_exe is a global setting.


 Another example is determining which environment (Dev/UAT/Prod) a host is 
 in. That's a custom Facter fact we already set through Puppet, and seems 
 like it would make sense to leverage that somehow. But the docs on the web 
 site seem to indicate that it would be better to split my inventory into 
 different files per environment.


 Ansible likes to make this statement declaratively using host groups and 
 playbooks.

 The playbooks describe what roles/tasks to apply to which groups of hosts.

 You can also use the group_by module in Ansible to create dynamic groups 
 based on the values of certain variables such as the OS type.
  



-- 
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 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/52215652-5686-408b-bd88-e6f895a60f8c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[ansible-project] conditionals in meta includes

2014-07-25 Thread Michael Dur
I have a role which is just a meta playbook for installing many packages.
I only want these to run once during an ansible run  since interacting with 
the package manager is slow.
I thought I'd register a variable and check it, however I'm getting an 
error.

TASK: [feature/zypper | remove non-seas repos] 
 
fatal: [tr808.seas.upenn.edu] = error while evaluating conditional: ( 
common_has_executed == 'false')

Here the relevant files:

common/tasks/main.yml
---
- name: log common status
  set_fact:  common_has_executed = 'true'

common/meta/main.yml
---
allow_duplicates: no
dependencies:

  - { role: feature/rc.firewall, when: ( common_has_executed == 'false') }
  - { role: feature/perl, when: ( common_has_executed == 'false') }
  - { role: feature/python, when: ( common_has_executed == 'false') }
  - { role: feature/ruby, when: ( common_has_executed is == 'false') }

-- 
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 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/f35bb98e-6582-4c75-bcbb-e0909421c8c7%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[ansible-project] Conditionals - when: var1 and var2 is defined works in a weird way

2014-02-03 Thread Roman Revyakin
Hi,

I am not sure whether someone has come across this issue previously - 
trying to search the group for 'when' and 'is defined' did not yield me 
anything useful. So I am posting it as a new topic here.
I have encountered that in case I use 'when:' clause where I need to test 
whether one variable is set to 'true' and another one is defined, the 
following playbook triggers action even if the first variable is 'false':

cat  test_playbook.yaml EOF
---
- hosts: all
  gather_facts: false

  tasks:
- name: Play only if var1 is true and var2 is defined
  local_action: command echo Yes {{ var1 }} is true and {{ var2 }} is 
defined
  when: var1 and var2 is defined
EOF

$ ansible-playbook -i host -v test_playbook.yaml --extra-vars var1=false 
var2='something'

PLAY [all] 
 

TASK: [Play only if var1 is true and var2 is defined] 
* 
changed: [localhost] = {changed: true, cmd: [echo, Yes false is 
true and something is defined], delta: 0:00:00.003256, end: 
2014-02-04 11:19:56.720699, rc: 0, start: 2014-02-04 
11:19:56.717443, stderr: , stdout: Yes false is true and something 
is defined}

PLAY RECAP 
 
localhost  : ok=1changed=1unreachable=0failed=0 
  
 
Only if I change the conditions to be supplied on separate 'when:' lines, 
it works as expected:

cat  test1_playbook.yaml EOF
---
- hosts: all
  gather_facts: false

  tasks:
- name: Play only if var1 is true and var2 is defined
  local_action: command echo Yes {{ var1 }} is true and {{ var2 }} is 
defined
  when: var2 is defined
  when: var1
EOF

$ ansible-playbook -i host -v test_playbook.yaml --extra-vars var1=false 
var2='something'

PLAY [all] 
 

TASK: [Play only if var1 is true and var2 is defined] 
* 
skipping: [localhost]

PLAY RECAP 
 
localhost  : ok=0changed=0unreachable=0failed=0 
  

$ ansible-playbook -i host -v test_playbook.yaml --extra-vars var1=true 
var2='something'

PLAY [all] 
 

TASK: [Play only if var1 is true and var2 is defined] 
* 
changed: [localhost] = {changed: true, cmd: [echo, Yes true is true 
and something is defined], delta: 0:00:00.002733, end: 2014-02-04 
11:22:20.289360, rc: 0, start: 2014-02-04 11:22:20.286627, stderr: 
, stdout: Yes true is true and something is defined}

PLAY RECAP 
 
localhost  : ok=1changed=1unreachable=0failed=0 
  

However in this case it fails (not skips) when the var2 is undefined and 
var1 is true, which is not really a desired behaviour:

$ ansible-playbook -i host -v test_playbook.yaml --extra-vars var1=true

PLAY [all] 
 

TASK: [Play only if var1 is true and var2 is defined] 
* 
fatal: [localhost] = One or more undefined variables: 'var2' is undefined

FATAL: all hosts have already failed -- aborting

PLAY RECAP 
 
   to retry, use: --limit @/Users/roman/test_playbook.yaml.retry

localhost  : ok=0changed=0unreachable=1failed=0 
  

The order of  'when's also matters, in case their order is reverted like 
follows

  when: var1
  when: var2 is defined

the playbook starts behaving like in the first example where 'when' 
conditions were supplied on one line using the 'and' operator.
If I use 

 when: var1==true and var2 is defined

the playbook would skip the action even all conditions are satisfied: 

$ ansible-playbook -i host -v test_playbook.yaml --extra-vars var1=true 
var2='defined'

PLAY [all] 
 

TASK: [Play only if var1 is true and var2 is defined] 
* 
skipping: [localhost]

PLAY RECAP 
 
localhost  : ok=0changed=0unreachable=0failed=0 
  

The 
docshttp://docs.ansible.com/playbooks_conditionals.html#the-when-statement do 
not shed any more light on that strange behaviour.

Thanks a lot in advance,

With kind regards,
Roman

-- 
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 post to this group, send email to ansible-project@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


Re: [ansible-project] Conditionals - when: var1 and var2 is defined works in a weird way

2014-02-03 Thread Adam Heath
false != false.  You have a string/boolean issue.  Aka, the var1 is 
not the boolean value false, but the string value false.


On 02/03/2014 06:32 PM, Roman Revyakin wrote:

Hi,

I am not sure whether someone has come across this issue previously -
trying to search the group for 'when' and 'is defined' did not yield me
anything useful. So I am posting it as a new topic here.
I have encountered that in case I use 'when:' clause where I need to
test whether one variable is set to 'true' and another one is defined,
the following playbook triggers action even if the first variable is
'false':

cat  test_playbook.yaml EOF
---
- hosts: all
   gather_facts: false

   tasks:
 - name: Play only if var1 is true and var2 is defined
   local_action: command echo Yes {{ var1 }} is true and {{ var2 }}
is defined
   when: var1 and var2 is defined
EOF

$ ansible-playbook -i host -v test_playbook.yaml --extra-vars
var1=false var2='something'

PLAY [all]


TASK: [Play only if var1 is true and var2 is defined]
*
changed: [localhost] = {changed: true, cmd: [echo, Yes false is
true and something is defined], delta: 0:00:00.003256, end:
2014-02-04 11:19:56.720699, rc: 0, start: 2014-02-04
11:19:56.717443, stderr: , stdout: Yes false is true and
something is defined}

PLAY RECAP

localhost  : ok=1changed=1unreachable=0failed=0

Only if I change the conditions to be supplied on separate 'when:'
lines, it works as expected:

cat  test1_playbook.yaml EOF
---
- hosts: all
   gather_facts: false

   tasks:
 - name: Play only if var1 is true and var2 is defined
   local_action: command echo Yes {{ var1 }} is true and {{ var2 }}
is defined
   when: var2 is defined
   when: var1
EOF

$ ansible-playbook -i host -v test_playbook.yaml --extra-vars
var1=false var2='something'

PLAY [all]


TASK: [Play only if var1 is true and var2 is defined]
*
skipping: [localhost]

PLAY RECAP

localhost  : ok=0changed=0unreachable=0failed=0

$ ansible-playbook -i host -v test_playbook.yaml --extra-vars var1=true
var2='something'

PLAY [all]


TASK: [Play only if var1 is true and var2 is defined]
*
changed: [localhost] = {changed: true, cmd: [echo, Yes true is
true and something is defined], delta: 0:00:00.002733, end:
2014-02-04 11:22:20.289360, rc: 0, start: 2014-02-04
11:22:20.286627, stderr: , stdout: Yes true is true and
something is defined}

PLAY RECAP

localhost  : ok=1changed=1unreachable=0failed=0

However in this case it fails (not skips) when the var2 is undefined and
var1 is true, which is not really a desired behaviour:

$ ansible-playbook -i host -v test_playbook.yaml --extra-vars var1=true

PLAY [all]


TASK: [Play only if var1 is true and var2 is defined]
*
fatal: [localhost] = One or more undefined variables: 'var2' is undefined

FATAL: all hosts have already failed -- aborting

PLAY RECAP

to retry, use: --limit @/Users/roman/test_playbook.yaml.retry

localhost  : ok=0changed=0unreachable=1failed=0

The order of  'when's also matters, in case their order is reverted like
follows

   when: var1
   when: var2 is defined

the playbook starts behaving like in the first example where 'when'
conditions were supplied on one line using the 'and' operator.
If I use

  when: var1==true and var2 is defined

the playbook would skip the action even all conditions are satisfied:

$ ansible-playbook -i host -v test_playbook.yaml --extra-vars var1=true
var2='defined'

PLAY [all]


TASK: [Play only if var1 is true and var2 is defined]
*
skipping: [localhost]

PLAY RECAP

localhost  : ok=0changed=0unreachable=0failed=0

The docs
http://docs.ansible.com/playbooks_conditionals.html#the-when-statement do
not shed any more light on that strange behaviour.

Thanks a lot in advance,

With kind regards,
Roman

--
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 post to this group, send email to 

Re: [ansible-project] Conditionals - when: var1 and var2 is defined works in a weird way

2014-02-03 Thread Roman Revyakin
Hi Adam,

I do not think so, please see my example above with the

ansible-playbook -i host -v test_playbook.yaml --extra-vars var1=false
var2='something'

being executed as expected (skipped). In case var1 was a string, the

  when: var2 is defined
  when: var1

conditions would evaluate to true IMHO and the action would be triggered.



On Tue, Feb 4, 2014 at 11:38 AM, Adam Heath a...@brainfood.com wrote:

 false != false.  You have a string/boolean issue.  Aka, the var1 is not
 the boolean value false, but the string value false.


 On 02/03/2014 06:32 PM, Roman Revyakin wrote:

 Hi,

 I am not sure whether someone has come across this issue previously -
 trying to search the group for 'when' and 'is defined' did not yield me
 anything useful. So I am posting it as a new topic here.
 I have encountered that in case I use 'when:' clause where I need to
 test whether one variable is set to 'true' and another one is defined,
 the following playbook triggers action even if the first variable is
 'false':

 cat  test_playbook.yaml EOF
 ---
 - hosts: all
gather_facts: false

tasks:
  - name: Play only if var1 is true and var2 is defined
local_action: command echo Yes {{ var1 }} is true and {{ var2 }}
 is defined
when: var1 and var2 is defined
 EOF

 $ ansible-playbook -i host -v test_playbook.yaml --extra-vars
 var1=false var2='something'

 PLAY [all]
 

 TASK: [Play only if var1 is true and var2 is defined]
 *
 changed: [localhost] = {changed: true, cmd: [echo, Yes false is
 true and something is defined], delta: 0:00:00.003256, end:
 2014-02-04 11:19:56.720699, rc: 0, start: 2014-02-04
 11:19:56.717443, stderr: , stdout: Yes false is true and
 something is defined}

 PLAY RECAP
 
 localhost  : ok=1changed=1unreachable=0
  failed=0

 Only if I change the conditions to be supplied on separate 'when:'
 lines, it works as expected:

 cat  test1_playbook.yaml EOF
 ---
 - hosts: all
gather_facts: false

tasks:
  - name: Play only if var1 is true and var2 is defined
local_action: command echo Yes {{ var1 }} is true and {{ var2 }}
 is defined
when: var2 is defined
when: var1
 EOF

 $ ansible-playbook -i host -v test_playbook.yaml --extra-vars
 var1=false var2='something'

 PLAY [all]
 

 TASK: [Play only if var1 is true and var2 is defined]
 *
 skipping: [localhost]

 PLAY RECAP
 
 localhost  : ok=0changed=0unreachable=0
  failed=0

 $ ansible-playbook -i host -v test_playbook.yaml --extra-vars var1=true
 var2='something'

 PLAY [all]
 

 TASK: [Play only if var1 is true and var2 is defined]
 *
 changed: [localhost] = {changed: true, cmd: [echo, Yes true is
 true and something is defined], delta: 0:00:00.002733, end:
 2014-02-04 11:22:20.289360, rc: 0, start: 2014-02-04
 11:22:20.286627, stderr: , stdout: Yes true is true and
 something is defined}

 PLAY RECAP
 
 localhost  : ok=1changed=1unreachable=0
  failed=0

 However in this case it fails (not skips) when the var2 is undefined and
 var1 is true, which is not really a desired behaviour:

 $ ansible-playbook -i host -v test_playbook.yaml --extra-vars var1=true

 PLAY [all]
 

 TASK: [Play only if var1 is true and var2 is defined]
 *
 fatal: [localhost] = One or more undefined variables: 'var2' is undefined

 FATAL: all hosts have already failed -- aborting

 PLAY RECAP
 
 to retry, use: --limit @/Users/roman/test_playbook.yaml.retry

 localhost  : ok=0changed=0unreachable=1
  failed=0

 The order of  'when's also matters, in case their order is reverted like
 follows

when: var1
when: var2 is defined

 the playbook starts behaving like in the first example where 'when'
 conditions were supplied on one line using the 'and' operator.
 If I use

   when: var1==true and var2 is defined

 the playbook would skip the action even all conditions are satisfied:

 $ ansible-playbook -i host -v test_playbook.yaml --extra-vars var1=true
 var2='defined'

 PLAY [all]
 

 TASK: [Play only if var1 is true and var2 is defined]
 *
 skipping: [localhost]

 PLAY RECAP
 
 localhost  

Re: [ansible-project] Conditionals - when: var1 and var2 is defined works in a weird way

2014-02-03 Thread Jesse Keating

On 2/3/14, 4:32 PM, Roman Revyakin wrote:

cat  test_playbook.yaml EOF
---
- hosts: all
   gather_facts: false

   tasks:
 - name: Play only if var1 is true and var2 is defined
   local_action: command echo Yes {{ var1 }} is true and {{ var2 }}
is defined
   when: var1 and var2 is defined
EOF

$ ansible-playbook -i host -v test_playbook.yaml --extra-vars
var1=false var2='something'

PLAY [all]


TASK: [Play only if var1 is true and var2 is defined]
*
changed: [localhost] = {changed: true, cmd: [echo, Yes false is
true and something is defined], delta: 0:00:00.003256, end:
2014-02-04 11:19:56.720699, rc: 0, start: 2014-02-04
11:19:56.717443, stderr: , stdout: Yes false is true and
something is defined}


You most certainly have a string to bool comparison issue going on.

You can change this by using the bool filter.

when: var1|bool and var2 is defined

When I use this, var1 being false will still cause the task to be 
skipped as expected.



-jlk

--
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 post to this group, send email to ansible-project@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


Re: [ansible-project] Conditionals - when: var1 and var2 is defined works in a weird way

2014-02-03 Thread Roman Revyakin
Hi Jesse,

Thanks for getting to the bottom of this and providing a working solution!
Works as charm indeed! Jinja framework is crazy IMHO.

Adam: grouping does not help, I tried it before submitting my first
request, as well as swapping the order.

The issue is solved, thank you! I wonder what is the process of altering
documentation on docs.ansible.com, googling for it provided me a link which
does not contain the text that is shown in the search preview.
Does submitting an issue to the Ansible
repohttps://github.com/ansible/ansible sound
like a proper idea?



On Tue, Feb 4, 2014 at 1:17 PM, Jesse Keating jkeat...@j2solutions.netwrote:

 On 2/3/14, 4:32 PM, Roman Revyakin wrote:

 cat  test_playbook.yaml EOF
 ---
 - hosts: all
gather_facts: false

tasks:
  - name: Play only if var1 is true and var2 is defined
local_action: command echo Yes {{ var1 }} is true and {{ var2 }}
 is defined
when: var1 and var2 is defined
 EOF

 $ ansible-playbook -i host -v test_playbook.yaml --extra-vars
 var1=false var2='something'

 PLAY [all]
 

 TASK: [Play only if var1 is true and var2 is defined]
 *
 changed: [localhost] = {changed: true, cmd: [echo, Yes false is
 true and something is defined], delta: 0:00:00.003256, end:
 2014-02-04 11:19:56.720699, rc: 0, start: 2014-02-04
 11:19:56.717443, stderr: , stdout: Yes false is true and
 something is defined}


 You most certainly have a string to bool comparison issue going on.

 You can change this by using the bool filter.

 when: var1|bool and var2 is defined

 When I use this, var1 being false will still cause the task to be
 skipped as expected.


 -jlk


 --
 You received this message because you are subscribed to a topic in the
 Google Groups Ansible Project group.
 To unsubscribe from this topic, visit https://groups.google.com/d/
 topic/ansible-project/YPfK1KYpQeI/unsubscribe.
 To unsubscribe from this group and all its topics, send an email to
 ansible-project+unsubscr...@googlegroups.com.
 To post to this group, send email to ansible-project@googlegroups.com.
 For more options, visit https://groups.google.com/groups/opt_out.


-- 
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 post to this group, send email to ansible-project@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


Re: [ansible-project] Conditionals - when: var1 and var2 is defined works in a weird way

2014-02-03 Thread Jesse Keating

On 2/3/14, 7:00 PM, Roman Revyakin wrote:


The issue is solved, thank you! I wonder what is the process of altering
documentation on docs.ansible.com http://docs.ansible.com, googling
for it provided me a link which does not contain the text that is shown
in the search preview.
Does submitting an issue to the Ansible repo
https://github.com/ansible/ansible sound like a proper idea?


Yeah, the docs are driven from the ansible code base -- so an issue (or 
better yet a pull request) is appropriate.



-jlk

--
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 post to this group, send email to ansible-project@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.