3

I want to write an Ansible playbook to install a package only if a particular is not present, so when the /tmp/hosted file present installation should not proceed, but it's failing:

---
 - hosts: all
   sudo: yes
   tasks:
   - name: Ensure NTP (for time synchronization) is installed.
     yum:  name=ntp state=present
     args:
      creates: "/tmp/hosted"

Error:

[root@ns0 ansible]# ansible-playbook creates.yml -l ansi2
[DEPRECATION WARNING]: Instead of sudo/sudo_user, use become/become_user and make sure become_method is 'sudo' (default).
This feature will be removed in
 a future release. Deprecation warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.

PLAY [all] ***********************************************************************************************************************************************

TASK [Gathering Facts] ***********************************************************************************************************************************
ok: [ansi2]

TASK [Ensure NTP (for time synchronization) is installed.] ***********************************************************************************************
fatal: [ansi2]: FAILED! => {"changed": false, "failed": true, "msg": "Unsupported parameters for (yum) module: creates. Supported parameters include: conf_file,disable_gpg_check,disablerepo,enablerepo,exclude,install_repoquery,installroot,list,name,skip_broken,state,update_cache,validate_certs"}
        to retry, use: --limit @/etc/ansible/creates.retry

PLAY RECAP ***********************************************************************************************************************************************
ansi2                      : ok=1    changed=0    unreachable=0    failed=1
2

This will work

---
- hosts: all
  become: yes
  tasks:
    - name: Check existence of /tmp/hosted
      shell: test -e /tmp/hosted
      register: hostedfile
      ignore_errors: yes

    - name: Ensure NTP (for time synchronization) is installed.
      yum:  name=ntp state=present
      when: hostedfile.rc != 0
  • can I know wat would be the output of register ` hostedfile` if the file exists,, – Mohammed Ali May 20 '17 at 14:24
  • Well it did work , thanks alot and i guess i also learned wat the code did , correct me if im wrong so the file /tmp/hosted existed so the register hostedfile value was 0 And the when directive in ntp installation checked register value it was 0 so it did not allowed ntp installation i.e it would have allowed installation when the file was missing and it would have given 1 or some other value to regisgter hostedfile – Mohammed Ali May 20 '17 at 16:14
  • You are correct. But I have updated my answer to make the shell command more elegant; testing to see if the file exists rather than using ls. The register: hostedfile also contains the stderr and stdout of the shell command, in addition to the return code. – GMaster May 21 '17 at 1:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.