Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'dict object' has no attribute 'stdout' in Ansible Playbook

My playbook:

- name: JBoss KeyStore and Truststore passwords will be stored in the          password vault
    #shell: less "{{ vault }}"
     shell: cat "{{ vault }}"
    register: vault_contents
    tags:
      - BW.6.1.1.10
    with_items:
      - "{{ vault }}"
  - debug:
       msg: "JBoss config filedoes not contains the word vault"
    when: vault_contents.stdout.find('$VAULT') == -1

I'm trying to read multiple files through ansible using Jinga2 Template and parse the output as stdout and search for a keyword and report it.

It fails with the below error:

TASK [testing_roles : debug]   **************************************************************************.   *****************************************************************
fatal: [d84e4fe137f4]: FAILED! => {"failed": true, "msg": "The conditional check 'vault_contents.stdout.find('$VAULT') == -1' failed. 
The error was: error while evaluating conditional (vault_contents.stdout.find('$VAULT') == -1): 'dict object' has no attribute 'stdout'\n\nThe error appears to have been in '/Ansible/Ansible/Relearn/testing_roles/roles/testing_roles/tasks/main.yml': line 49, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n      - \"{{ vault }}\"\n  - debug:\n    ^ here\n"}
to retry, use: --limit @/Ansible/Ansible/Relearn/testing_roles/playbook.retry

When i append it with a single file entry it works as expected but when it is changes to a series of files it fails.

Is this the right approach to scan multiple files in Ansible or should be be using some other module or method.

Any help is greatly appreciated.

In the vars file it has the below contents:

vault:
  - /jboss-as-7.1.1.Final/standalone/configuration/standalone-full-ha.xml

Thank you

like image 286
anish anil Avatar asked Dec 24 '18 15:12

anish anil


People also ask

What is stdout in Ansible?

Change the default callback for standard output When you run an Ansible playbook from the command line, you get messages printed to standard output (stdout). Ansible can only have a single callback responsible for handling stdout.

What is Changed_when in Ansible?

Ansible lets you define when a particular task has “changed” a remote node using the changed_when conditional. This lets you determine, based on return codes or output, whether a change should be reported in Ansible statistics and whether a handler should be triggered or not.


1 Answers

An inspection of - debug: var=vault_contents will show you that when used with a looping construct such as with_items:, the register variable has a list called results containing the outcomes for each iteration of the loop. This is also documented in the fine manual.

So, what you want is likely:

- debug:
    msg: "JBoss config {{ item.item }} does not contain the word vault"
  when: item.stdout.find('$VAULT') == -1
  with_items: '{{ vault_contents.results }}'
like image 52
mdaniel Avatar answered Sep 28 '22 14:09

mdaniel