Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to make sure a given user always exist in a system, so only create when it is not exist

my current tasks is:

- name: Create default user
action: user name={{ user }} groups={{ group }}  state=present

However, it raise the error when a user already exists, so how to avoid the error when the user account already exist?

share|improve this question
Please post the exact error as output by the system. – dawud Jul 28 '13 at 18:06

Modules, and therefore, playbooks like the one you show, have to be idempotent to be of any use.

Repeating the same action several times both with a playbook and a onliner does not result in any errors, as expected:

$ ansible -u dawud -m user -a "name=sysadm group=1000 state=present" | success >> {
    "append": false,
    "changed": false,
    "comment": "System Administrator,,,",
    "group": 1000,
    "home": "/home/sysadm",
    "name": "sysadm",
    "shell": "/bin/bash-static",
    "state": "present",
    "uid": 1000

$ ansible-playbook ansible/sample.yml -u dawud -K
sudo password:

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

GATHERING FACTS *********************
ok: []

TASK: [create admin user] *********************
ok: []

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

The playbook I have used:

$ cat ansible/sample.yml
- hosts:
  sudo: yes


    - name: create admin user
      action: user name=sysadm group=1000 state=present
share|improve this answer

Or you can just cheat and add

ignore_errors: yes  

below the action: user line

share|improve this answer
This is a poor choice, and should only be used as THE VERY LAST resort, and ONLY if you know exactly why you're doing this. – Guto Andreollo Jun 14 at 14:12

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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