Ansible return code error: 'dict object' has no attribute 'rc'

Viewed 3373

I am using Ansible to automate the installation, configuration and deployment of an application server which uses JBOSS, therefore I need to use the in-built jboss-cli to deploy packages.

This Ansible task is literally the last stage to run, it simply needs to check if a deployment already exists, if it does, undeploy it and redeploy it (to be idempotent).

Running the below commands manually on the server and checking the return code after each command works as expected, something, somewhere in Ansible refuses to read the return codes correctly!

# BLAZE RMA DEPLOYMENT
- name: Check if Blaze RMA has been assigned to dm-server-group from a previous Ansible run
  shell: "./jboss-cli.sh --connect '/server-group=dm-server-group/deployment={{ blaze_deployment_version }}:read-resource()' | grep -q success"
  args:
    chdir: "{{ jboss_home_dir }}/bin"
  register: blaze_deployment_status
  failed_when: blaze_deployment_status.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

- name: Undeploy Blaze RMA if it has already been assigned to dm-server-group from a previous Ansible run
  command: "./jboss-cli.sh --connect 'undeploy {{ blaze_deployment_version }} --all-relevant-server-groups'"
  args:
    chdir: "{{ jboss_home_dir }}/bin"
  when: blaze_deployment_status.rc == 0
  register: blaze_undeployment_status

- name: Deploy Blaze RMA once successfully undeployed
  command: "./jboss-cli.sh --connect 'deploy {{ jboss_deployment_dir }}/{{ blaze_deployment_version }} --server-groups=dm-server-group'"
  args:
    chdir: "{{ jboss_home_dir }}/bin"
  when: blaze_undeployment_status.rc == 0 or blaze_deployment_status.rc == 1

Any advice would be appreciated.

1 Answers

Your second command contains a when clause. If it is skipped, ansible still registers the variable but there is no rc attribute in the data.

You need to take this into consideration when using the var in the next task. The following condition on last task should fix your issue.

    when: blaze_undeployment_status.rc | default('') == 0 or blaze_deployment_status.rc == 1
Related