Search code examples
ansibleopenstackansible-inventory

How to set common ansible_user to all hosts in a group?


Does anyone know if ansible enables users share the same ansible_user settings across all hosts included in a group? This feature would be particularly useful when using some cloud computing platforms such as OpenStack, which enables users to launch multiple instances that share the same config, such as user accounts and SSH keys.


Solution

  • There are several behavioral parameters you can configure to modify the way Ansible connects to your host. Among them is the ansible_user variable. You can set it per host pr per group. You can also define a general ansible_user variable under the all hosts group, that you override at the group or host level.

    If you were writing your inventory in just one hosts.yml file you'd do it like this:

    all:
      children:
        ubuntu_linux:
          hosts:
            ubuntu_linux_1:
            ubuntu_linux_2:
        aws_linux:
          hosts:
            aws_linux_host_1:
            aws_linux_host_2:
            aws_linux_host_3:
          vars:
            ansible_user: ec2-user
      vars:
        ansible_host: ubuntu
    

    And, if you are using a directory to create your inventory, you can set it inside the ./inventory/group/vars.yml file.

    Check the "Connecting to hosts: behavioral inventory parameters" section of Ansible docs to see what other parameters you can configure.

    I hope it helps