access to register stdout_lines attributes

1,555 views
Skip to first unread message

deewon

unread,
May 15, 2018, 9:15:57 AM5/15/18
to Ansible Project
Hi,

I have a task that executes a shell command and spews out a few lines. Specifically, it probes a remote DB and and returns some information .  Please see below:

- name: Test role delegation to apps
  hosts
: appboxes
  tasks
:
   
- name: get cluster status from dbboxes
      shell
: mysqlsh --redirect-primary test123:test123@box1:3306 --cluster -e "cluster.status().defaultReplicaSet.primary" --interactive --json
     
register: res
      delegate_to
: "{{ groups.mysqldbs | first }}"
      run_once
: True


   
- name: check result
      debug
:
       
var=res.stdout_lines
      run_once
: True


#    - name: check result
#      debug:
#        var: "{{ item.value }}"
#     with_items: "{{ res.stdout_lines }}"
#      run_one: True





root@controller:/etc/ansible/playbooks# ansible-playbook test_delegate.yml


PLAY
[Test role delegation to apps] *********************************************************************************************************************************************************************************************************


TASK
[Gathering Facts] **********************************************************************************************************************************************************************************************************************
ok
: [appbox1]
ok
: [appbox2]


TASK
[get cluster status from dbboxes] ******************************************************************************************************************************************************************************************************
changed
: [appbox1 -> dbbox1]


TASK
[check result] *************************************************************************************************************************************************************************************************************************
ok
: [appbox1] => {
   
"res.stdout_lines": [
       
"{",
       
"    \"info\": \"Creating a session to 'test123@box1:3306'\"",
       
"}",
       
"{",
       
"    \"info\": \"Your MySQL connection id is 246 Server version: 5.7.21-log MySQL Commercial Server (Advanced) No default schema selected; type \\\\use <schema> to set one.\"",
       
"}",
       
"{",
       
"    \"info\": \"Reconnecting to PRIMARY instance of the InnoDB cluster (mysql://box1:3306)...\"",
       
"}",
       
"{",
       
"    \"info\": \"Creating a Classic session to 'test123@box1:3306'\"",
       
"}",
       
"{",
       
"    \"info\": \"Closing old connection...\"",
       
"}",
       
"{",
       
"    \"info\": \"Your MySQL connection id is 239 Server version: 5.7.21-log MySQL Commercial Server (Advanced) No default schema selected; type \\\\use <schema> to set one.\"",
       
"}",
       
"{",
       
"    \"value\": \"box1:3306\"",
       
"}"
   
]
}


PLAY RECAP
**********************************************************************************************************************************************************************************************************************************
appbox1                    
: ok=3    changed=1    unreachable=0    failed=0
appbox2                    
: ok=1    changed=0    unreachable=0    failed=0


I've been  scratching my head trying to figure out how to return only the "value" attribute and its corresponding result. 

I tried using a loop (commented out in the playbook) but it just spews out a load of errors saying the attribute doesn't exist i.e.

TASK [check result] *************************************************************************************************************************************************************************************************************************
fatal
: [appbox1]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'ansible.utils.unsafe_proxy.AnsibleUnsafeText object' has no attribute 'value'\n\nThe error appears to have been in '/etc/ansible/playbooks/test_delegate.yml': line 15, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n    - name: check result\n      ^ here\n\nexception type: <class 'ansible.errors.AnsibleUndefinedVariable'>\nexception: 'ansible.utils.unsafe_proxy.AnsibleUnsafeText object' has no attribute 'value'"}
fatal
: [appbox2]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'ansible.utils.unsafe_proxy.AnsibleUnsafeText object' has no attribute 'value'\n\nThe error appears to have been in '/etc/ansible/playbooks/test_delegate.yml': line 15, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n    - name: check result\n      ^ here\n\nexception type: <class 'ansible.errors.AnsibleUndefinedVariable'>\nexception: 'ansible.utils.unsafe_proxy.AnsibleUnsafeText object' has no attribute 'value'"}
        to
retry, use: --limit @/etc/ansible/playbooks/test_delegate.retry


PLAY RECAP
**********************************************************************************************************************************************************************************************************************************
appbox1                    
: ok=3    changed=1    unreachable=0    failed=1
appbox2                    
: ok=1    changed=0    unreachable=0    failed=1

Any ideas will be appreciated
Dayo



deewon

unread,
May 18, 2018, 5:05:40 AM5/18/18
to Ansible Project
Got to the bottom of this eventually...

-debug: var= "{{ res.stdout_lines | last }}"

It's the last item in the list 
Reply all
Reply to author
Forward
0 new messages