Can someone help me with configuring multiple users (ubuntu/ec2-user) to connect . . .

Diana:
Can someone help me with configuring multiple users (ubuntu/ec2-user) to connect to different OS types please?
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 need to be able to ssh to all of the instances i have using different users, using the same playbook.
I am not even sure where should it be configured?

Diana:
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

Diana:
I am using ansible galaxy role to run as ansible playbook: https://galaxy.ansible.com/geerlingguy/docker
This is how the playbook i run looks like:

---

# docker.yml


- name: Use a galaxy role to install docker

  hosts: "all"

  become: true


  roles:

    - role: "geerlingguy.docker"

      tags: ["docker"]

Al West:
I think you need to specify the group in your dynamic inventory:
https://docs.ansible.com/ansible/latest/collections/amazon/aws/aws_ec2_inventory.html#parameter-groups
Then you should be able to have a group_vars folder and have the user details for each group in that path.

Diana:
How can i group dynamic inventory aws instances?
@Al West

Al West:
I’ve not done it myself and don’t have access to my lab to try it out. You have to write a Jinja2 expression on the returned list to specify the group name.

Al West:
I asked ChatGPT and it seems right but I can’t validate it right now. The solutions uses tags on your EC2 Instances and the ec2_tag plugin:

Let’s say you have a number of EC2 instances in AWS that are tagged with the key env and the value dev, prod, or test. You can use the ec2_tag plugin to create a dynamic inventory of your EC2 instances and then use Jinja2 expressions to group them based on their env tag value.

Here’s an example of how you can define groups for your EC2 instances based on their env tag:

{% for host in groups['tag_env_dev'] %}
  [dev]
  {{ host }}
{% endfor %}

{% for host in groups['tag_env_prod'] %}
  [prod]
  {{ host }}
{% endfor %}

{% for host in groups['tag_env_test'] %}
  [test]
  {{ host }}
{% endfor %}

In this example, we are grouping hosts based on their env tag value. The ec2_tag plugin creates groups based on the tag name and value. So, if you have EC2 instances with the env tag value of dev, prod, or test, the ec2_tag plugin will create the groups tag_env_dev, tag_env_prod, and tag_env_test.

Note that the group names in the Jinja2 expressions (dev, prod, and test) can be any name you choose, and do not need to match the tag value. The important part is that you are using the correct group names in your Ansible playbooks to target the correct hosts.

Al West:
Actually this might be easier:
https://docs.ansible.com/ansible/latest/collections/amazon/aws/docsite/aws_ec2_guide.html#complex-example
Look at the Groups line - then you can use group_vars

Diana:
How can i configure it in my inventory file?

plugin: aws_ec2

regions:

  - "us-east-1"



{% for host in groups['tag_env_ubuntu'] %}

  [ubuntu]

  {{ host }}

{% endfor %}



{% for host in groups['tag_env_redhat'] %}

  [redhat]

  {{ host }}

{% endfor %}





compose:

  ansible_host: public_ip_address

Diana:
This is the output:

ansible-inventory --graph -i inventory_aws_ec2.yml 

[WARNING]:  * Failed to parse /home/diana/ansible/inventory_aws_ec2.yml with auto plugin: We were unable to read either as JSON nor YAML, these are the errors we got from

each: JSON: Expecting value: line 1 column 1 (char 0)  Syntax Error while loading YAML.   found character that cannot start any token  The error appears to be in

'/home/diana/ansible/inventory_aws_ec2.yml': line 5, column 2, but may be elsewhere in the file depending on the exact syntax problem.  The offending line appears to be:

{% for host in groups['tag_env_ubuntu'] %}  ^ here

[WARNING]:  * Failed to parse /home/diana/ansible/inventory_aws_ec2.yml with yaml plugin: We were unable to read either as JSON nor YAML, these are the errors we got from

each: JSON: Expecting value: line 1 column 1 (char 0)  Syntax Error while loading YAML.   found character that cannot start any token  The error appears to be in

'/home/diana/ansible/inventory_aws_ec2.yml': line 5, column 2, but may be elsewhere in the file depending on the exact syntax problem.  The offending line appears to be:

{% for host in groups['tag_env_ubuntu'] %}  ^ here

[WARNING]:  * Failed to parse /home/diana/ansible/inventory_aws_ec2.yml with ini plugin: Invalid host pattern 'plugin:' supplied, ending in ':' is not allowed, this

character is reserved to provide a port.

[WARNING]: Unable to parse /home/diana/ansible/inventory_aws_ec2.yml as an inventory source

[WARNING]: No inventory was parsed, only implicit localhost is available

@all:

  |--@ungrouped:

Diana:
Or maybe i didn’t configured that in the right location?
I am confused…

Diana:
These are my tags
image.png

Al West:
Look at the last link I posted. It should then look something like this:

# 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.Name
    prefix: tag_Name_
    separator: ""
groups:
  # add hosts to the group ubuntu or redhat
  ubuntu: "'ubuntu' in (tags|list)"
  redhat: "'redhat' in (tags|list)"

Diana:
I didn’t understand the doc…
I just made a change and it looks like this:

# 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: ""

groups:

  # add hosts to the group ubuntu or redhat

  ubuntu: "'ubuntu' in (tags|list)"

  redhat: "'redhat' in (tags|list)"

Diana:
This is the output:

ansible-inventory --graph -i inventory_aws_ec2.yml 

@all:

  |--@ungrouped:

  |--@aws_ec2:

  |  |--ip-10-0-4-186.ec2.internal

  |  |--ip-172-31-81-25.ec2.internal

  |  |--ip-172-31-14-132.ec2.internal

  |  |--ip-172-31-46-6.ec2.internal

  |  |--ip-172-31-42-4.ec2.internal

  |  |--ip-172-31-41-36.ec2.internal

  |  |--ip-172-31-40-45.ec2.internal

  |  |--ip-172-31-34-164.ec2.internal

  |  |--ip-10-0-4-66.ec2.internal

  |  |--ip-172-31-16-146.ec2.internal

  |  |--ip-172-31-62-93.ec2.internal

  |--@tag_Name_redhat:

  |  |--ip-172-31-81-25.ec2.internal

  |  |--ip-172-31-16-146.ec2.internal

  |--@tag_Name_ubuntu:

  |  |--ip-172-31-42-4.ec2.internal

  |  |--ip-172-31-41-36.ec2.internal

Diana:
How can i pass a different username to a different OS type?

Al West:
you create a group_vars folder in the same place as your inventory and then use your <groupname>.yaml file to set the parameters.

Al West:
Sorry I don’t have access to my lab at the moment to try this out for you.

Diana:
It’s ok i can check if it works my self :slightly_smiling_face:

Diana:
This is what i have: