Ansible-Playbook passing lists for specific tasks

2k views Asked by At

Much like this question regarding passing an array mine is a bit different

The OP, was trying to give the play multiple hosts to run on.

Fails clearly. My goal is to "loop" through my play on each account within my .boto configuration file. Sure I could leverage a simple bash loop. But was thinking to do something more with Ansible, by passing specific "acct" vars into it. *Updated with task that fails.

- name: Ansible Roles in AWS .v01
  hosts: 127.0.0.1
  gather_facts: no
  connection: local
  vars:
    role_state: present   
    dict1: { "dev", "mgmt", "uat", "sbx" }
  # - debug: var=
  tasks:
  - name: hhc-ADMIN-Role-Create
    with_items: dict1
    tags:
      - admin
    iam:
      iam_type: role
      region: us-east-1
      profile: "{{ item }}"
      name: hhc-{{ dict1 }}-ADMIN
      state: "{{role_state}}"
      trust_policy_filepath: ./Policies/Trust/Role-Trust-Policy.json
2

There are 2 answers

0
shdobxr On BEST ANSWER

One method to utilize a "list" would be the following:

---
- name: Ansible Roles in AWS .v01
  hosts: 127.0.0.1
  gather_facts: no
  connection: local
  vars:
    role_state: present   
    list:
      - { profile: "dev", role:  "ADMIN" }
      - { profile: "dev", role:  "MGMT" }
  tasks:
  - name: hhc-ADMIN-Role-Create
    with_items: "{{ list }}"
    tags:
      - admin
    iam:
      iam_type: role
      region: us-east-1
      profile: "{{ item.profile }}"
      name: hhc-{{ item.profile }}-{{ item.role }}
      state: "{{role_state}}"
      trust_policy_filepath: ./Policies/Trust/Role-Trust-Policy.json

This will create a role of ADMIN and MGMT in Dev. There has got to be a more elegant way to do something like

list:
  - { profile: "dev", role: [ "ADMIN", "MGMT"] }
4
helloV On

One idea is to set AWS_PROFILE environment variable in the loop.

Pseudo code:

  tasks:
    - ec2: ... ....
      environment:
        AWS_PROFILE: "{{item}}"
      with_items: cli_var