1

I am trying to convert an existing ansible playbook (for extracting the webpage content of multiple webpage URL's in parallel fashion) to re-usable roles. I need the role to accept variables in a loop and produce the output for all the items in a single task which my current playbook is able to do. But the current role is only able to produce the output of the last item in the loop

I have tried registering the webpage content inside and outside the roles but of no use. And also looping the response results with_items same as of the role is producing results for non-200 values

FYI I got the expected output by including the loop inside the role but it's defeating the purpose of maintaining a role for GET call because I will not need a loop every time for the GET call. So I am expecting to loop the role in the testplaybook.yml.

Test-Role: main.yml

  uri: 
    url: "{{ URL_Variable }}"
    method: GET
    status_code: 200
    return_content: yes
  register: response
  ignore_errors: true

testplaybook.yml:

- hosts: localhost
  gather_facts: true
  tasks:  
    - name: Include roles
      include_role:
        name: Test-Role
      vars:
        URL_Variable: "http://{{ item }}:{{ hostvars[groups['group1'][0]]['port'] }}/{{ hostvars[groups['group1'][0]]['app'] }}/"
      with_items: "{{ groups['group1'] }}"

    - name: "Display content"
      debug:
        var: response.results

Expected Output:

response.results:

ok: [127.0.0.1] => (item=[0, 'item1']) => {
    "ansible_loop_var": "item",
    "item": [
        0,
        "item1"
    ],
    "response": {
        "accept_ranges": "bytes",
        "changed": false,
        "connection": "close",
        "content": "content1",
        "content_length": "719",
        "content_type": "text/html",
        "cookies": {},
        "failed": false,
        "msg": "OK (719 bytes)",
        "redirected": false,
        "server": "42",
        "status": 200,
        "url": "http://item1:port/app/"
    }
}
ok: [127.0.0.1] => (item=[1, 'item2']) => {
    "ansible_loop_var": "item",
    "item": [
        1,
        "item2"
    ],
    "response": {
        "accept_ranges": "bytes",
        "changed": false,
        "connection": "close",
        "content": "content2",
        "content_length": "719",
        "content_type": "text/html",
        "cookies": {},
        "failed": false,
        "msg": "OK (719 bytes)",
        "redirected": false,
        "server": "42",
        "status": 200,
        "url": "http://item2:port/app/"
    }
}
TR007
  • 47
  • 1
  • 8

1 Answers1

0

try this Test-Role: main.yml file:

- uri: 
    url: "{{ URL_Variable }}"
    method: GET
    status_code: 200
    return_content: yes
  register: response
  ignore_errors: true

- name: Add response to responses array
  set_fact:
    responses_results: "{{ responses_results | default([]) + [{'URL': URL_Variable, 'response': response.content}] }}"

this works with include_tasks, i assume it would work with include_role as well, the variable responses_results should persist across roles assuming its in the same play. if not works, try to switch your code to a single role instead, with an include_tasks.

hope it helps

ilias-sp
  • 6,135
  • 4
  • 28
  • 41
  • Hello, Thanks for the response. But I got this error "fatal: [127.0.0.1]: FAILED! => {"msg": "Unexpected templating type error occurred on ({{ response_results | default([]) + [{'URL': URL_Variable, 'response': response.content}] }}): can only concatenate str (not \"list\") to str"} for the second iteration. FYI I got the expected output by including the loop inside the role but it's defeating the purpose of maintaining a role for GET call because I will not need a loop every time for the GET call. So I am expecting to loop the role in the testplaybook.yml. – TR007 Jun 06 '19 at 23:05
  • hmmm. not sure why this error occurs, i didnt try the code, just adapted a similar tasks file that i am using. if you had rdy to copy-paste PBs, i could them on my host.. or you can use the above if you find it helpful. cheers – ilias-sp Jun 07 '19 at 18:17