Serial in playbook - missing variable

Hello Community,

I have a question related with my ansible playbook. Let me describe an issue:

I use hostvars in template like:

{% for host in groups[‘elasticsearch’] %}

  • {{ hostvars[host].ansible_default_ipv4.address }}
    {% endfor %}

all working fine, but wen I use serial option in my playbook:

  • name: Upgrade Elasticsearch
    hosts: elasticsearch

gather_facts: yes

serial: 1
tasks:

  • name: Check Elasticsearch cluster state - Need to be green before upgrade
    […]
  • import_role:
    name: u_elasticsearch

I receiving error:

TASK [u_elasticsearch : Elasticsearch template config] ***************************************************************** failed: [elk1] (item=elasticsearch.yml) => {“ansible_loop_var”: “item”, “changed”: false, “item”: “elasticsearch.yml”,
“msg”: “AnsibleUndefinedVariable: ‘ansible.vars.hostvars.HostVarsVars object’ has no attribute ‘ansible_all_ipv4_addresses’”}

I understood that this is because of missing all facts, is it any possibility to force gather all facts and perform serial …

I will be really happy for Your help

Small update, I know about:

`

  • name: gather facts from elasticsearch servers

setup:

delegate_to: “{{ item }}”

delegate_facts: True

loop: “{{ groups[‘elasticsearch’] }}”

`

Is there any more elegant option ? Maybe some parameter which I didn’t know ?

If they are more elegant or not I leave that up to you to deside.

In your playbook you could have to plays, where the first one only gather facts

   - hosts: elasticsearch
     gather_facts: yes

   - hosts: elasticsearch
     gather_facts: no
     serial: 1

An other alternative if you only need a few tasks to run in serial is to use the new throttle on the task/tasks itself

https://docs.ansible.com/ansible/latest/user_guide/playbooks_strategies.html#using-keywords-to-control-execution