2016-06-17 52 views
8

ho i seguenti compiti in un playbook che sto scrivendo (risultati elencati accanto alla dichiarazione di debug in <>):Ansible "quando la variabile == true" non comportarsi come previsto

- debug: var=nrpe_installed.stat.exists <true> 
    - debug: var=force_install <true> 
    - debug: var=plugins_installed.stat.exists <true> 

    - name: Run the prep 
    include: prep.yml 
    when: (nrpe_installed.stat.exists == false or plugins_installed.stat.exists == true or force_install == true) 
    tags: ['prep'] 

    - debug: var=nrpe_installed.stat.exists <true> 
    - debug: var=force_install <true> 
    - debug: var=force_nrpe_install <false> 

    - name: Install NRPE 
    include: install-nrpe.yml 
    when: (nrpe_installed.stat.exists == false or force_install == true or force_nrpe_install == true) 
    tags: ['install_nrpe'] 
    vars: 
     nrpe_url: 'http://url.goes.here' 
     nrpe_md5: 3921ddc598312983f604541784b35a50 
     nrpe_version: 2.15 
     nrpe_artifact: nrpe-{{ nrpe_version }}.tar.gz 
     nagios_ip: {{ nagios_ip }} 
     config_dir: /home/ansible/config/ 

E' m di esecuzione con il seguente comando:

ansible-playbook install.yml -i $invFile --extra-vars="hosts=webservers force_install=True" 

la prima comprende piste, ma il secondo salta con questa uscita:

skipping: [server1] => {"changed": false, "skip_reason": "Conditional check failed", "skipped": true} 

Ho l'impressione che il controllo condizionale debba passare per tutti come force_install == true valutazioni a true che dovrebbe rendere l'intero when valutato su true (poiché si tratta di una serie di "OR").

Come ottengo il momento in cui eseguire quando le variabili sono impostate in modo appropriato?


Edit:

Cambiare il secondo in cui per la Install NRPE includere le seguenti opere, ma non spiega il motivo per cui l'altra, Run the prep corre in modo appropriato:

di lavoro:

when: (not nrpe_installed.stat.exists or force_install or force_nrpe_install) 

Funzionante anche:

when: (nrpe_installed.stat.exists == false or plugins_installed.stat.exists == true or force_install == true) 

non funziona:

when: (nrpe_installed.stat.exists == false or force_install == true or force_nrpe_install == true) 

Il troncato (duplicati rimossi) uscita di quella particolare sezione del gioco è:

TASK [debug] ******************************************************************* 
ok: [server2] => { 
    "nrpe_installed.stat.exists": true 
} 

TASK [debug] ******************************************************************* 
ok: [server2] => { 
    "plugins_installed.stat.exists": true 
} 

TASK [debug] ******************************************************************* 
ok: [server2] => { 
    "force_install": true 
} 

TASK [Run the prep] ************************************************************ 
included: /tasks/nrpe-install/prep.yml for server2, server3, server4, server5, server6, server7 

TASK [Prep and configure for installation | Install yum packages] ************** 
ok: [server6] => (item=[u'gcc', u'glibc', u'glibc-common', u'gd', u'gd-devel', u'make', u'net-snmp', u'openssl-devel', u'unzip', u'tar', u'gzip', u'xinetd']) => {"changed": false, "item": ["gcc", "glibc", "glibc-common", "gd", "gd-devel", "make", "net-snmp", "openssl-devel", "unzip", "tar", "gzip", "xinetd"], "msg": "", "rc": 0, "results": ["gcc-4.1.2-55.el5.x86_64 providing gcc is already installed", "glibc-2.5-123.el5_11.3.i686 providing glibc is already installed", "glibc-common-2.5-123.el5_11.3.x86_64 providing glibc-common is already installed", "gd-2.0.33-9.4.el5_4.2.x86_64 providing gd is already installed", "gd-devel-2.0.33-9.4.el5_4.2.i386 providing gd-devel is already installed", "make-3.81-3.el5.x86_64 providing make is already installed", "net-snmp-5.3.2.2-20.el5.x86_64 providing net-snmp is already installed", "openssl-devel-0.9.8e-40.el5_11.x86_64 providing openssl-devel is already installed", "unzip-5.52-3.el5.x86_64 providing unzip is already installed", "tar-1.15.1-32.el5_8.x86_64 providing tar is already installed", "gzip-1.3.5-13.el5.centos.x86_64 providing gzip is already installed", "xinetd-2.3.14-20.el5_10.x86_64 providing xinetd is already installed"]} 

TASK [Prep and configure for installation | Make nagios group] ***************** 
ok: [server2] => {"changed": false, "gid": 20002, "name": "nagios", "state": "present", "system": false} 

TASK [Prep and configure for installation | Make nagios user] ****************** 
ok: [server6] => {"append": false, "changed": false, "comment": "User for Nagios NRPE", "group": 20002, "home": "/home/nagios", "move_home": false, "name": "nagios", "shell": "/bin/bash", "state": "present", "uid": 20002} 

TASK [debug] ******************************************************************* 
ok: [server2] => { 
    "nrpe_installed.stat.exists": true 
} 

TASK [debug] ******************************************************************* 
ok: [server2] => { 
    "force_install": true 
} 

TASK [debug] ******************************************************************* 
ok: [server2] => { 
    "force_nrpe_install": false 
} 

TASK [Install NRPE] ************************************************************ 
skipping: [server2] => {"changed": false, "skip_reason": "Conditional check failed", "skipped": true} 

risposta

30

è necessario convertire la variabile da un valore booleano:

force_install|bool == true 

Non pretendo di capire La logica dietro. In Python qualsiasi stringa non vuota dovrebbe essere sincera. Ma se usato direttamente in una condizione, viene valutato come falso.

Il filtro bool interpreta nuovamente le stringhe "true" e "yes" (case-insensitive) come true. Qualsiasi altra stringa è falsa.

Si potrebbe desiderare di impostare anche un valore di default nel caso in cui force_install non è definita, in quanto si tradurrebbe in una non definita errore variabile:

force_install|default(false)|bool == true 
+4

non è 'true' == ridondanti? –

+4

Esatto, puoi ometterlo ovviamente. 'force_install | default (false) | bool' – udondan

+0

E poiché' None', 'False',' 0' e '" "' (la stringa vuota) sono valori falsi e stringhe non vuote sono valori veri, di solito è possibile omettere la conversione 'bool' e basta dire ad es 'Force_install | default (Falso)'. – akaihola