Ansible - Loop

Card Puncher Data Processing

About

loop in Ansible are done with look up plugin 1) 2) that returns variables for each selectionned item that you can use in your template.

Management

Nested

loop in Ansible are never nested but you can combine list

- name: give users access to multiple databases
  mysql_user:
    name: "{{ item[0] }}"
    priv: "{{ item[1] }}.*:ALL"
    append_privs: yes
    password: "foo"
  loop: "{{ ['alice', 'bob'] |product(['clientdb', 'employeedb', 'providerdb'])|list }}"

With Items

- name: Install mapr-core
  vars:
    packages_Suse: ['mapr-compat-suse', 'mapr-core', 'mapr-core-internal']
    packages_RedHat: ['mapr-core', 'mapr-core-internal']
    packages_Debian: ['mapr-core', 'mapr-core-internal']
  package: name={{ item }} state=present
  with_items: "{{ vars['packages_' + ansible_os_family] }}"
  register: core_result

For Files

See the file system page loop section where you can find the with_fileglob and with_filetree lookup.





Discover More
Card Puncher Data Processing
Ansible - Flow (Conditional)

This page is conditional operators in Ansible. Variable initialization See Loop variable value can be made conditional. Example: variables by type of operating system with include_vars...
Card Puncher Data Processing
Ansible - Getting Started

This is a getting started guide that shows how to install and where are the training/example. lightbulb ...
Card Puncher Data Processing
Ansible - List Variable

List variable or from two_sources Flatten a list (same thing the flatten lookup does):...



Share this page:
Follow us:
Task Runner