Multiple remote users to work with ansible dynamic inventory

231 views Asked by At

I am using dynamic inventory and i didn't find anything about how to run a playbook with more than one user. When i configure a specific remote user in my ansible.cfg file, the ssh connection works only for the OS types that uses that specific user. I am not even sure where should it be configured? In the playbook itself i have configured to go to a different OS types. I know it works with static inventory file as well, but i have no idea how to configure it with dynamic inventory. I am using ansible galaxy role to run as ansible playbook: https://galaxy.ansible.com/geerlingguy/docker I tried configuring group_vars but it doesn't work. I need to be able to ssh to all of the instances i have using different users, using the same playbook.

This is how my dynamic inventory looks like:

plugin: aws_ec2

regions:

  - "us-east-1"

keyed_groups:

  - key: tags.Ansible

  - key: tags.Name


filters:

  tag:Ansible:

    - ubuntu

    - redhat


compose:

  ansible_host: public_ip_address

This is the playbook i run:

---

# docker.yml


- name: Use a galaxy role to install docker

  hosts: "all"

  become: true


  roles:

    - role: "geerlingguy.docker"

      tags: ["docker"]
1

There are 1 answers

0
Diana On

This is the answer to my question:

# demo.aws_ec2.yml
plugin: amazon.aws.aws_ec2
regions:
  - us-east-1
keyed_groups:
  # add hosts to tag_Name_value groups for each aws_ec2 host's tags.Name variable.
  - key: tags.Ansible
    prefix: tag_Name_
    separator: ""
filters:
  tag:Ansible:
    - ubuntu
    - redhat
    
groups:
  # add hosts to the group ubuntu or redhat
  ubuntu: "'ubuntu' in (tags|list)"
  redhat: "'redhat' in (tags|list)"
  
compose:
  ansible_host: public_ip_address