How to keep track of where you are in an Ansible loop?

I need to write some lines in a YAML file for a variable number of times:

- name: Retry a task until a certain condition is met
  lineinfile:
    path: /root/file
    insertafter: '^listeners:'
    line: 'iteration #iteration_number++'
  retries: {{ a_variable }}
  delay: 10

I want to write execution number of each iteration to the file also. Similar to the following for loop:

for i in {1..a_variable}
  do
    echo "i"
  done

How to keep track of where you are in a loop?

Q: “Similar to the following for loop.

Based on the already given idea, a minimal example playbook

---
- hosts: localhost
  become: false
  gather_facts: false

  tasks:

  - name: Echo index into file
    lineinfile:
      path: index.file
      create: true
      line: '{{ item }}'
    loop: "{{ range(1, i + 1, 1) }}"
    loop_control:
      extended: true
      label: "{{ ansible_loop.index }}"
    vars:
      i: 3

will result into an output of

TASK [Echo index into file] ****
changed: [localhost] => (item=1)
changed: [localhost] => (item=2)
changed: [localhost] => (item=3)

and a file content of

cat index.file
1
2
3

Even if this is technically possible one should keep in mind The Zen of Ansible.

If you’re trying to “write code” in your plays and roles, you’re setting yourself up for failure. Ansible’s YAML-based playbooks were never meant to be for programming.

Documentation

  • Jinja2 Template Designer – List of Global Functions – range()
  • Adding controls to loop

First, retries are not loops as they don’t iterate over anything. They just poll until the condition is met. Instead, you need to put your lineinfile execution in a loop that would iterate over a range from 0 to your value. Something like this (I can’t validate right now, just showing the idea):

 - name: Retry a task until a certain condition is met
   lineinfile:
     path: /root/file
     insertafter: '^listeners:'
     line: 'iteration #iteration_number++'
   loop: "{{ lookup('range', 0, a_variable }}"

Then, you can use the default item variable or set it to another one using the optional loop_var directive of the loop_control parameter.

But that’s true for simple cases. In the real world, one iterates over some list of objects, so Ansible provides a special variable just for this case. It’s covered in
Tracking progress through a loop with index_var:

To keep track of where you are in a loop, use the index_var directive with loop_control. This directive specifies a variable name to contain the current loop index.

    - name: Count our fruit
      ansible.builtin.debug:
        msg: "{{ item }} with index {{ my_idx }}"
      loop:
        - apple
        - banana
        - pear
      loop_control:
        index_var: my_idx

You might also consider templating the file with Jinja2 instead.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật