0

I have the following ansible code:

- name: "Create required users, and user edits for {{ role_name }}"
  user:
    name: "{{ item.name }}"
    comment: "{{ item.comment }}"
    uid: "{{ item.uid }}"
    shell: "{{ item.shell }}"
    group: "{{ item.group }}"
  with_items:  
    - { name: "spring", comment: "springboot user", uid: "5017", shell: "/bin/bash", group: "spring" }
  register: addinguser
  tags: configure.yml

- name: debug var2
  debug:
    msg: "{{ addinguser.results.0.failed }}"
  register: ok

- name: "Create required directories for this role"
  file:
    path: "{{ item.dir_path }}"
    state: directory
    mode: "{{ item.dir_mode }}"
    owner: "{[ item.dir_owner ]}"
    group: "{{ item.dir_group }}"
  with_items:
    - { dir_path: '/home/spring/script', dir_mode: '0700', dir_owner: 'spring', dir_group: 'spring' }
  when: "'false' in ok"
  tags: configure.yml 

Which generated the following truncated output

task path: /tmp/awx_453706_md5af8hf/project/roles/ANS_RLS_***_001/tasks/configure.yml:57
    ok: [localhost.*.*.*l] => {
        "addinguser": {
            "changed": false,
            "msg": "All items completed",
            "results": [
                {
                    "ansible_loop_var": "item",
                    "append": false,
                    "changed": false,
                    "comment": "springboot user",
                    "failed": false,
                    "group": 5017,
                    "home": "/home/spring",
                   "invocation": {
                        "module_args": {
                            "append": false,
                            "authorization": null,
                            "comment": "springboot user",
                            "create_home": true,
                           "expires": null,
                            "force": false,

I want a directory to be created in a homedir only when the creation of the user does have the value '''failed: false'''

I can get the value i want with accessing "{{ addinguser.results.0.failed }}" but i am unable to make a comparison on that using in the '''when'' statement

Any help is very much appreciated

1
  • Try replacing when: "'false' in ok" with when: addinguser.results[0].failed == false
    – noah1400
    Commented Oct 10, 2023 at 13:54

1 Answer 1

0

Replace:

when: "'false' in ok"

With:

when: addinguser.results[0].failed == false

So your modified Block looks like this:

- name: "Create required directories for this role"
  file:
    path: "{{ item.dir_path }}"
    state: directory
    mode: "{{ item.dir_mode }}"
    owner: "{{ item.dir_owner }}"
    group: "{{ item.dir_group }}"
  with_items:
    - { dir_path: '/home/spring/script', dir_mode: '0700', dir_owner: 'spring', dir_group: 'spring' }
  when: addinguser.results[0].failed == false
  tags: configure.yml

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