Aman Singh
Aman Singh

Reputation: 75

Dry run in Ansible not showing errors as in actual run

I have written a playbook to add one entry in resolv.conf and add a new user. Group I am specifying in not present in target system however I am not getting the error in dry run ( using --check) option but getting it in actual run.

[root@ansible-controller ansible-test-project]# ansible-playbook lineinfile-playbook.yaml -i inventory.txt  --check

PLAY [Add a new nameserver and webuser] ****************************************************************

TASK [Gathering Facts] *********************************************************************************
ok: [ansible-target]

TASK [Update entry into /etc/resolv.conf] **************************************************************
changed: [ansible-target]

TASK [Add user web_user] *******************************************************************************
changed: [ansible-target]

PLAY RECAP *********************************************************************************************
ansible-target             : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   




[root@ansible-controller ansible-test-project]# ansible-playbook lineinfile-playbook.yaml -i inventory.txt

PLAY [Add a new nameserver and webuser] ****************************************************************

TASK [Gathering Facts] *********************************************************************************
ok: [ansible-target]

TASK [Update entry into /etc/resolv.conf] **************************************************************
changed: [ansible-target]

TASK [Add user web_user] *******************************************************************************
fatal: [ansible-target]: FAILED! => {"changed": false, "msg": "Group developers does not exist"}

PLAY RECAP *********************************************************************************************
ansible-target             : ok=2    changed=1    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0   

[root@ansible-controller ansible-test-project]# 

My playbook

-
    name: 'Add a new nameserver and webuser'
    hosts: targets
    tasks:
        -
            name: 'Update entry into /etc/resolv.conf'
            lineinfile:
                path: /etc/resolv.conf
                line: 'nameserver 10.1.250.10'
        -
            name: 'Add user web_user'
            user:
                name: web_user
                uid: 1040
                group: developers

Upvotes: 1

Views: 333

Answers (1)

larsks
larsks

Reputation: 311635

The issue is that in check mode, the user module only checks whether or not the user exists. The solution is to add a task to your playbook that ensures the group exists as well:

    - name: add developers group
      group:
        name: developers
        gid: 1000

    - name: add user web_user
      user:
        name: web_user
        uid: 1040
        group: developers

Running this in against a host that does not have either the developers group or the web_user user results in:

TASK [add developers group] ******************************************************************************************************************************************************************
changed: [node0]

TASK [add user web_user] *********************************************************************************************************************************************************************
changed: [node0]

Upvotes: 2

Related Questions