Need help with nested variables to use in a "when:" directive inside of a role playbook

32 views
Skip to first unread message

Dayton Jones

unread,
Feb 2, 2016, 3:48:21 PM2/2/16
to Ansible Project
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."
            }
        ]
    }
}

Igor Cicimov

unread,
Feb 4, 2016, 2:21:42 AM2/4/16
to Ansible Project
Try this:


    - name: Ensure user directory exists
      file:
        path=/opt/{{ item.item }}
        state=directory
        owner={{ item.item }}
        group={{ item.item }}
        mode=0755
      with_items: passinfo.results

not tested though.
Reply all
Reply to author
Forward
0 new messages