I have a requirement to create directories only if the specified user(s) 
exist on the remote host... given my role defintion below, what is the 
proper syntax to use with "when" (or other method) to only create the 
directory only if that user exists on the host and skip the task if not 
present?  

../vars/main.yml:

> ---
> my_user_list:
>   - user1
>   - user2
>


../tasks/main.yml:

>     - name: Check for existence of users
>       getent: database=passwd key={{item}} fail_key=False
>       with_items: my_user_list
>       register: passinfo
>     - name: Ensure user directory exists (user1)
>       file:
>         path=/opt/user1
>         state=directory
>         owner=user1
>         group=user1
>         mode=0755
>       when: ??? filter to only run if user1 exists ???
>       ignore_errors: yes
>     - name: Ensure user directory exists (user2)
>       file:
>         path=/opt/user2
>         state=directory
>         owner=user2
>         group=user2
>         mode=0755
>       when: ??? filter to only run if user2 exists ???
>       ignore_errors: yes
>


here is the "output" of the passinfo variable:

    "passinfo": {
        "changed": false, 
        "msg": "All items completed",
        "results": [
            {
                "_ansible_no_log": false,
                "ansible_facts": {
                    "getent_passwd": {
                        "user2": [
                            "x",
                            "1002",
                            "1002",
                            "",
                            "/home/user2",
                            "/bin/bash"
                        ]
                    }
                },
                "changed": false,
                "invocation": {
                    "module_args": {
                        "_ansible_check_mode": false,
                        "_ansible_debug": false,
                        "_ansible_diff": false,
                        "_ansible_no_log": false,
                        "_ansible_verbosity": 0,
                        "database": "passwd",
                        "fail_key": false,
                        "key": "user2",
                        "split": null
                    },
                    "module_name": "getent"
                },
                "item": "user2"
            },
            {
                "_ansible_no_log": false,
                "ansible_facts": {
                    "getent_passwd": {
                        "user1": null
                    }
                },
                "changed": false,
                "invocation": {
                    "module_args": {
                        "_ansible_check_mode": false,
                        "_ansible_debug": false,
                        "_ansible_diff": false,
                        "_ansible_no_log": false,
                        "_ansible_verbosity": 0,
                        "database": "passwd",
                        "fail_key": false,
                        "key": "user1",
                        "split": null
                    },
                    "module_name": "getent"
                },
                "item": "user1",
                "msg": "One or more supplied key could not be found in the 
database."
            }
        ]
    }
}


-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/3fbf5e4a-b229-435e-a180-e0218187f887%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to