Search code examples
shellloopsansiblejbossstdout

Ansible: Use stdout_lines from registered variable which uses loop in shell module


Wondering if anyone here can help...

I'm running Ansible 2.10.7 - The below Ansible task is responsible for checking whether or not any JBOSS deployments exist from a previous Ansible run or not.

If nothing is returned by check_deployments.stdout_lines (check_deployments.stdout_lines | length == 0) then it should deploy all of the deployments defined in the loop. However, if something is returned (check_deployments.stdout_lines | length > 0) then it should undeploy whatever exists by using the check_deployments.stdout_lines content (e.g. old_deployment1, old_deployment2, old_deployment3, old_deployment4).

I am currently running this Ansible against a fresh server with no deployments, so I would expect that the undeploy section should be skipped due to check_deployments.stdout_lines being empty. However, it tries to run, but fails with the following error:

TASK [app : Undeploy all existing deployments as the versions do not match deployments defined in group_vars] ************************************************************************
fatal: []: FAILED! => {"msg": "'dict object' has no attribute 'stdout_lines'"}

Here is the output to my debug:

    "check_deployments": {
        "changed": true,
        "msg": "All items completed",
        "results": [
            {
                "ansible_loop_var": "item",
                "changed": true,
                "cmd": "./jboss-cli.sh --connect --commands='ls deployment' | grep -q deployment1",
                "delta": "0:00:02.081544",
                "end": "2021-04-28 16:12:18.448965",
                "failed": false,
                "failed_when_result": false,
                "invocation": {
                    "module_args": {
                        "_raw_params": "./jboss-cli.sh --connect --commands='ls deployment' | grep -q deployment1",
                        "_uses_shell": true,
                        "argv": null,
                        "chdir": "/opt/jboss/jboss-eap-7.1/bin",
                        "creates": null,
                        "executable": null,
                        "removes": null,
                        "stdin": null,
                        "stdin_add_newline": true,
                        "strip_empty_ends": true,
                        "warn": true
                    }
                },
                "item": "deployment1",
                "msg": "non-zero return code",
                "rc": 1,
                "start": "2021-04-28 16:12:16.367421",
                "stderr": "",
                "stderr_lines": [],
                "stdout": "",
                "stdout_lines": []
            },
            {
                "ansible_loop_var": "item",
                "changed": true,
                "cmd": "./jboss-cli.sh --connect --commands='ls deployment' | grep -q deployment2",
                "delta": "0:00:02.185931",
                "end": "2021-04-28 16:12:21.396740",
                "failed": false,
                "failed_when_result": false,
                "invocation": {
                    "module_args": {
                        "_raw_params": "./jboss-cli.sh --connect --commands='ls deployment' | grep -q deployment2",
                        "_uses_shell": true,
                        "argv": null,
                        "chdir": "/opt/jboss/jboss-eap-7.1/bin",
                        "creates": null,
                        "executable": null,
                        "removes": null,
                        "stdin": null,
                        "stdin_add_newline": true,
                        "strip_empty_ends": true,
                        "warn": true
                    }
                },
                "item": "deployment2",
                "msg": "non-zero return code",
                "rc": 1,
                "start": "2021-04-28 16:12:19.210809",
                "stderr": "",
                "stderr_lines": [],
                "stdout": "",
                "stdout_lines": []
            },
            {
                "ansible_loop_var": "item",
                "changed": true,
                "cmd": "./jboss-cli.sh --connect --commands='ls deployment' | grep -q deployment3",
                "delta": "0:00:01.938456",
                "end": "2021-04-28 16:12:24.060703",
                "failed": false,
                "failed_when_result": false,
                "invocation": {
                    "module_args": {
                        "_raw_params": "./jboss-cli.sh --connect --commands='ls deployment' | grep -q deployment3",
                        "_uses_shell": true,
                        "argv": null,
                        "chdir": "/opt/jboss/jboss-eap-7.1/bin",
                        "creates": null,
                        "executable": null,
                        "removes": null,
                        "stdin": null,
                        "stdin_add_newline": true,
                        "strip_empty_ends": true,
                        "warn": true
                    }
                },
                "item": "deployment3",
                "msg": "non-zero return code",
                "rc": 1,
                "start": "2021-04-28 16:12:22.122247",
                "stderr": "",
                "stderr_lines": [],
                "stdout": "",
                "stdout_lines": []
            },
            {
                "ansible_loop_var": "item",
                "changed": true,
                "cmd": "./jboss-cli.sh --connect --commands='ls deployment' | grep -q deployment4",
                "delta": "0:00:01.934429",
                "end": "2021-04-28 16:12:26.819414",
                "failed": false,
                "failed_when_result": false,
                "invocation": {
                    "module_args": {
                        "_raw_params": "./jboss-cli.sh --connect --commands='ls deployment' | grep -q deployment4",
                        "_uses_shell": true,
                        "argv": null,
                        "chdir": "/opt/jboss/jboss-eap-7.1/bin",
                        "creates": null,
                        "executable": null,
                        "removes": null,
                        "stdin": null,
                        "stdin_add_newline": true,
                        "strip_empty_ends": true,
                        "warn": true
                    }
                },
                "item": "deployment4",
                "msg": "non-zero return code",
                "rc": 1,
                "start": "2021-04-28 16:12:24.884985",
                "stderr": "",
                "stderr_lines": [],
                "stdout": "",
                "stdout_lines": []
            }
        ]
    }
}

Here is my Ansible task:

 - name: Check if the deployments defined in group_vars are already installed
  ansible.builtin.shell: "./jboss-cli.sh --connect --commands='ls deployment' | grep -q {{ item }}"
  args:
    chdir: "{{ jboss_home_dir }}/bin"
  register: check_deployments
  failed_when: check_deployments.rc == 2
  tags: # We always need to check this as the output determines whether or not we need to undeploy an existing deployment.
    - skip_ansible_lint
  loop:
 - "{{ new_deployment1 }}"
 - "{{ new_deployment2 }}"
 - "{{ new_deployment3 }}"
 - "{{ new_deployment4 }}"

 - name: Show list of deployments
  ansible.builtin.debug:
    var: check_deployments.stdout_lines

 - name: Undeploy all existing deployments as the versions do not match deployments defined in group_vars
  ansible.builtin.command: "./jboss-cli.sh --connect 'undeploy {{ item }} --all-relevant-server-groups'"
  args:
    chdir: "{{ jboss_home_dir }}/bin"
  when: check_deployments.stdout_lines | length > 0
  register: check_undeployment_status
  loop:
 - "{{ check_deployments.stdout_lines }}"

 - name: Deploy all deployments defined in group_vars
  ansible.builtin.command: "./jboss-cli.sh --connect 'deploy {{ jboss_deployment_dir }}/{{ item }} --server-groups=dm-server-group'"
  args:
    chdir: "{{ jboss_home_dir }}/bin"
  when: check_deployments.stdout_lines | length == 0 or check_undeployment_status.rc | default('') == 0
  loop:
 - "{{ new_deployment1 }}"
 - "{{ new_deployment2 }}"
 - "{{ new_deployment3 }}"
 - "{{ new_deployment4 }}"

Solution

  • Something is wrong in your playbook together with the given output. When I build a similar playbook without all the dependencies:

    - name: "Check if the deployments defined in group_vars are already installed"
      shell: "{{ item }}"
      register: check_deployments
      loop:
        - "pwd"
        - "ls -al"
        - "pwd"
    
    - name: "Show list of deployments"
      debug:
        var: check_deployments.stdout_lines
    

    I'm getting the information

    TASK [Show list of deployments] *************************************************
    ok: [localhost] => 
      check_deployments.stdout_lines: VARIABLE IS NOT DEFINED!
    

    This is true, because you loop over a Ansible module and register ALL output into ONE variable. In that case, the variable contains a list of results. So - your loop with check_deployments.stdout_lines is wrong. You need to loop over all check_deployments.results which contains the correct "registered" variable per module run, which you want to check

    - name: "Undeploy..."
      debug:
        msg: "stdout: {{ item.cmd }}"
      when: "item.stdout_lines | length > 1"
      loop: "{{ check_deployments.results }}"
    

    This will only output one entry (ls -al). All other will be skipped.