title: Ansible: Start role if service present
body: <p>I want to run a role, but before i want to check if the program is already installed.</p>
  
  <p>I guessed I should use the module "service" ? In Ubuntu the service is pointed with <strong>[ ? ] arkeia</strong></p>
  
  <p>I tried: </p>
  
  <pre><code>  tasks:
      - name: check arkeia
        service: name=arkeia state=started
        register: result
  
    roles:
      - { role: arkeia, when: result==true }
  </code></pre>
  
  <p>Otherwise if it is not possible to check if a program is already installed, i would check if the program folder is present and next start the role.</p>
  
  <p>EDIT:</p>
  
  <p>Thanks for your fast answer!</p>
  
  <p>I think my flaw is the "when:" command.
  But how can I check if the result is false? </p>
  
  <p><strong>I am getting following error:</strong></p>
  
  <pre><code>&gt; PLAY [all] ******************************************************************** 
  
  GATHERING FACTS *************************************************************** 
  ok: [managed-centos]
  ok: [managed-ubuntu]
  ok: [ansible]
  
  TASK: [arkeia | check arkeia] ************************************************* 
  ok: [ansible]
  ok: [managed-ubuntu]
  ok: [managed-centos]
  
  TASK: [arkeia | create directory] ********************************************* 
  skipping: [managed-centos]
  skipping: [managed-ubuntu]
  changed: [ansible]
  
  TASK: [arkeia | download sources debian] ************************************** 
  skipping: [managed-centos]
  skipping: [managed-ubuntu]
  changed: [ansible]
  
  TASK: [arkeia | download sources cent_os] ************************************* 
  skipping: [ansible]
  skipping: [managed-ubuntu]
  fatal: [managed-centos] =&gt; error while evaluating conditional: ansible_os_family == "RedHat" and p.stat.exist == false
  
  TASK: [arkeia | install Arkeia debian] **************************************** 
  fatal: [ansible] =&gt; error while evaluating conditional: ansible_os_family == "Debian" and p.stat.exist == false
  fatal: [managed-ubuntu] =&gt; error while evaluating conditional: ansible_os_family == "Debian" and p.stat.exist == false
  
  FATAL: all hosts have already failed -- aborting
  
  PLAY RECAP ******************************************************************** 
             to retry, use: --limit @/home/vagrant/arkeia.retry
  
  ansible                    : ok=4    changed=2    unreachable=1    failed=0   
  managed-centos             : ok=2    changed=0    unreachable=1    failed=0   
  managed-ubuntu             : ok=2    changed=0    unreachable=1    failed=0   
  </code></pre>
  
  <p><strong>This is my Playbook:</strong></p>
  
  <pre><code>- name: check arkeia
    stat: path=/etc/init.d/arkeia
    register: p
  
  - name: create directory
    file: state=directory path={{ install_directory }}
    when: p.stat.exists == False
  
  - name: download sources debian
    get_url: url={{ arkeia_deb_url }} dest={{ install_directory }}/{{ deb_filename }}
    when: ansible_os_family == "Debian" and p.stat.exists == false
  
  - name: download sources cent_os
    get_url: url={{ arkeia_centos_url }} dest={{ install_directory }}/{{ centos_filename }}
    when: ansible_os_family == "RedHat" and p.stat.exist == false
  
  - name: install Arkeia debian
    sudo: yes
    apt: deb={{ install_directory }}/{{ deb_filename }}
    when: ansible_os_family == "Debian" and p.stat.exist == false
  
  - name: install Arkeia centos
    sudo: yes
    yum: name={{ install_directory }}/{{ centos_filename }}
    when: ansible_os_family == "RedHat" and p.stat.exist == false
  </code></pre>
  
  <p><strong>Debug result:</strong></p>
  
  <pre><code>TASK: [arkeia | debug msg="{{ p }}"] ****************************************** 
  ok: [ansible] =&gt; {
      "msg": "{'invocation': {'module_name': u'stat', 'module_args': u'path=/etc/init.d/arkeia'}, 'stat': {'exists': False}, 'changed': False}"
  }
  ok: [managed-ubuntu] =&gt; {
      "msg": "{'invocation': {'module_name': u'stat', 'module_args': u'path=/etc/init.d/arkeia'}, 'stat': {'exists': False}, 'changed': False}"
  }
  ok: [managed-centos] =&gt; {
      "msg": "{'invocation': {'module_name': u'stat', 'module_args': u'path=/etc/init.d/arkeia'}, 'stat': {'uid': 0, 'exists': True, 'woth': False, 'mtime': 1421377097.0, 'inode': 67318822, 'isgid': False, 'size': 1224, 'wgrp': False, 'isuid': False, 'isreg': True, 'pw_name': 'root', 'gid': 0, 'ischr': False, 'wusr': True, 'xoth': True, 'rusr': True, 'nlink': 1, 'issock': False, 'rgrp': True, 'gr_name': 'root', 'path': '/etc/init.d/arkeia', 'xusr': True, 'atime': 1427813706.6750975, 'md5': '52de32627765e8f6e9ff8c5eee7ba232', 'isdir': False, 'ctime': 1427453340.1586156, 'isblk': False, 'xgrp': True, 'dev': 64769, 'roth': True, 'isfifo': False, 'mode': '0755', 'checksum': 'ddd459110d743402d9e18cfe4b3e032faaf54ba1', 'islnk': False}, 'changed': False}"
  }
  </code></pre>