conditionals

20 views
Skip to first unread message

Jeffrey Agnitsch

unread,
Mar 11, 2019, 3:50:57 PM3/11/19
to Ansible Project
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 

Sebastian Meyer

unread,
Mar 11, 2019, 4:07:29 PM3/11/19
to ansible...@googlegroups.com, Jeffrey Agnitsch
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=1 changed=0 unreachable=0 failed=0
host2 : ok=1 changed=0 unreachable=0 failed=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
Reply all
Reply to author
Forward
0 new messages