1
votes

I am having this playbook

---
- hosts: all
  handlers:
  - name: user-create
    user: name=oracle state=present
  tasks:
  - name: "check user"
    command: /usr/bin/id oracle
    register: output
    notify: user-create
    when: output.rc == 1

But whenever i run this on a system where oracle user is not there, running into this error

"failed": true, "msg": "The conditional check 'output.rc == 1' failed. The error was: error while evaluating conditional (output.rc == 1): 'output' is undefined\n\nThe error appears to have been in '/user-check.yml': line 10, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n tasks:\n - name: \"check user\"\n ^ here\n"

1
when statement is evaluated before task execution, and output variable is populated after task execution. What is your goal?Konstantin Suvorov
Hello Konstantin, My goal is to check if oracle user exists, thene delete it and recreate the user. Thats why i am registering the output and then if the rc is 1, notifying the handler.Ps guide me on how to achieve the sameachak01
Your requirement is not idempotent, it will delete/create user every playbook run. This is bad pattern. Take a look at @techraf's answer. Just add task with state=absent if you want to always recreate user.Konstantin Suvorov

1 Answers

0
votes

Answering based on comment:

Actually i wanto check if the user exists, id it exists delete and recreate the user

The correct way to do it in Ansible is:

---
- hosts: all
  tasks:
    - name: ensure user 'oracle' does not exist
      user: name=oracle state=absent

    - name: ensure user 'oracle' exists
      user: name=oracle state=present

And that's all.

Ansible takes care not to try to create a user if it already exists; and not to try to delete a user if it doesn't.