Ansible looping over register and multiple lists

Hi
I’m trying to do the following (in windows but it’s not important):

  1. Need to deploy N number of build agents to a server (according to the inventory nb_of_agents) - iteration1

  2. Each agent should have a unique port starting from 8811 - iteration2

  3. Check if the service is already present - store in register. - REGISTER LIST

  4. Install according to the register.
    First question: Using a register list, how do i use : when: service.results.exists for specific index?

Second question: how do i go in parrallel via multiple list:

  1. with_sequence: start=1 end=‘{{ nb_of_agents}}’’ and
  2. with_sequence: start=8811 count=‘{{ nb_of_agents}}’’ and
  3. when: service.result[index].exists

So the code looks like this:

  • name: check service ‘{{ item }}’ is not already running
    win_service:
    name: Build Agent Prod ‘{{ item }}’
    register: svc
    with_sequence: start=1 end=‘{{nb_of_agents}}’

  • name: output
    debug:
    var: svc

  • name: output
    debug:
    var: svc.results.[item].exists
    with_sequence: count=‘{{nb_of_agents}}’

  • name: install Agent ‘{{ item }}’
    win_copy:
    src: \some\share\buildagent_prod_template
    dest: ‘D:\buildagent_prod_{{ item }}’
    force: no
    remote_src: yes
    when: svc.results[item].exists == false
    register: agent
    with_sequence: count=‘{{nb_of_agents}}’

  • name: modify Port
    win_lineinfile:
    path: D:\buildagent_prod_{{ item }}\conf\node.properties
    backrefs: yes
    regexp: ‘(^port)=.*’
    line: ‘$1={{ item2 }}’
    when: ‘agent.results[item]’
    with_together: count=nb_of_agents
    with_items: start=qb_prod_agent_port count=nb_of_agents

Thanks!!

Please paste the code WITHOUT any formatting. Font should be any single-space so that spacing is correct.

Hi David
Here it is:

`

  • name: check service ‘{{ item }}’ is not already running
    win_service:
    name: Build Agent Prod ‘{{ item }}’
    register: svc
    with_sequence: start=1 end=‘{{nb_of_agents}}’

  • name: output
    debug:
    var: svc

  • name: output
    debug:
    var: svc.results.[item].exists
    with_sequence: count=‘{{nb_of_agents}}’

  • name: install Agent ‘{{ item }}’
    win_copy:
    src: \some\share\buildagent_prod_template
    dest: ‘D:\buildagent_prod_{{ item }}’
    force: no
    remote_src: yes
    when: svc.results[item].exists == false
    register: agent
    with_sequence: count=‘{{nb_of_agents}}’

  • name: modify Port
    win_lineinfile:
    path: D:\buildagent_prod_{{ item }}\conf\node.properties
    backrefs: yes
    regexp: ‘(^port)=.*’
    line: ‘$1={{ item2 }}’
    when: ‘agent.results[item]’
    with_together: count=nb_of_agents
    with_items: start=qb_prod_agent_port count=nb_of_agents

`

Thanks

You can’t normally run in parallel with a loop, they are run sequentially. You can hack around it by running with async and poll 0 and then poll that status but it isn’t a perfect “run all items in list in parallel” that you are looking for.

Your debug output

`

  • name: output
    debug:
    var: svc.results.[item].exists
    with_sequence: count=‘{{nb_of_agents}}’

`

Won’t work as you are trying to get key 0, 1, 2, 3, etc of the svc.results var when it is actually a list. You want something like 'svc.results[item].exists' instead. What this does is get's the list entry of item, bare in mind that lists are a 0 based index, so the first entry is 0, 2nd is 1 and so on. You might have to do something like 'svc.results[item - 1].exists' if you want to match up the entry to your agent index starting at 1.

I haven't used with_sequence a lot so can't tell if that syntax is ok, apart from that what you have seems ok at a brief glance but having the output from a run that failed would help us narrow down your issue.

Thanks

Jordan

Thank you Jordan
Actually, I also tried without the “.” as you suggested and it didn’t work
Code:

`

  • name: output
    debug:
    var: qb_svc

  • name: output
    debug:
    var: qb_svc.results[item].exists
    with_sequence: count=‘{{nb_of_agents}}’

`

Here is my output with your suggestion:

`
task path: /ansible/scripts/playbooks/qb-agents-fw-multiple.yml:16
ok: [hasgappqba1102.DOMAIN] => {
“qb_svc”: {
“changed”: false,
“msg”: “All items completed”,
“results”: [
{
“_ansible_ignore_errors”: null,
“_ansible_item_result”: true,
“_ansible_no_log”: false,
“_ansible_parsed”: true,
“changed”: false,
“exists”: false,
“failed”: false,
“item”: “1”
},
{
“_ansible_ignore_errors”: null,
“_ansible_item_result”: true,
“_ansible_no_log”: false,
“_ansible_parsed”: true,
“changed”: false,
“exists”: false,
“failed”: false,
“item”: “2”
}
]
}
}

TASK [output] **************************************************************************************************************************************************************************************************************************************************************************************
task path: /ansible/scripts/playbooks/qb-agents-fw-multiple.yml:20
ok: [hasgappqba1102.DOMAIN] => (item=None) => {
“qb_svc.results[item].exists”: “VARIABLE IS NOT DEFINED!: ‘list object’ has no attribute u’1’”
}
ok: [hasgappqba1102.DOMAIN] => (item=None) => {
“qb_svc.results[item].exists”: “VARIABLE IS NOT DEFINED!: ‘list object’ has no attribute u’2’”
}

TASK [install Agent ‘{{ item }}’] ******************************************************************************************************************************************************************************************************************************************************************
task path: /ansible/scripts/playbooks/qb-agents-fw-multiple.yml:25
fatal: [hasgappqba1102.DOMAIN]: FAILED! => {
“msg”: “The conditional check ‘qb_svc.results[item].exists’ failed. The error was: error while evaluating conditional (qb_svc.results[item].exists): ‘list object’ has no attribute u’1’\n\nThe error appears to have been in ‘/ansible/scripts/playbooks/qb-agents-fw-multiple.yml’: line 25, 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: install Agent ‘{{ item }}’\n ^ here\nWe could be wrong, but this one looks like it might be an issue with\nmissing quotes. Always quote template expression brackets when they\nstart a value. For instance:\n\n with_items:\n - {{ foo }}\n\nShould be written as:\n\n with_items:\n - "{{ foo }}"\n”
}
to retry, use: --limit @/ansible/scripts/playbooks/qb-agents-fw-multiple.retry
`

Thank you!

Looks like item is being used as a unicode string (u’1’) and not an int, can you try qb_svc.results[item|int].exists and see if that works?

It worked!
Since i the first is 0, the last should be {{nb_of_agents -1 |int}}

`

  • name: output
    debug:
    var: qb_svc.results[item|int].exists
    with_sequence: start=0 end=‘{{nb_of_agents - 1 |int}}’

`

Thanks!!!