Monday, March 05, 2018

Note on Ansible Shell

Symptom :
   Always get below when we use variable in ansible playbook. It turns out we use |  to work around it.

Error Code:
tasks:
   - name: display databases in the GI via srvctl
     shell:   "{{ srvctl_locl }}" config database
     register: dblist
   - debug: msg="{{ dblist.stdout_lines }}"


Error Stack:
The offending line appears to be:

   - name: display databases in the GI via srvctl
     shell: '{{srvctl_locl }}' config database
                               ^ here
We could be wrong, but this one looks like it might be an issue with
missing quotes.  Always quote template expression brackets when they
start a value. For instance:

    with_items:
      - {{ foo }}

Should be written as:

    with_items:
      - "{{ foo }}"

exception type: <class 'yaml.parser.ParserError'>
exception: while parsing a block mapping
  in "<unicode string>", line 16, column 6
did not find expected key
  in "<unicode string>", line 17, column 32

 
Solution:

tasks:
   - name: display databases in the GI via srvctl
     shell: |
         "{{ srvctl_locl }}" config database
     register: dblist
   - debug: msg="{{ dblist.stdout_lines }}"



Other ansible Tip:

- name: use loop control to display a specific attribute  of item ie stdout_lines ,std_err, start etc
     command: sleep 1
     with_items: "{{dbstatus.results }}"
     loop_control:
          label: "{{ item.stdout_lines }}" 

No comments: