Ansible – Using Consul Dynamic Inventory and Setting Specific SSH User

ansibleconsul

My setup:

Every physical server in the network has consul-client installed and I manage these servers using consul_dynamic_inventory script which I've edited to allow me the use of specific grouping options based on os major version for example and other stuff.

Example:

(base) ➜  ansible git:(master) ✗ ansible centos6 -m ping -e ansible_ssh_user=root
Sunday 08 March 2020  16:55:10 +0200 (0:00:00.101)       0:00:00.102 **********
agent-c6-06.node.company.consul | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}
agent-c6-01.node.company.consul | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}
agent-c6-05.node.company.consul | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}

The above example is possible because I configured /etc/ansible/ansible.cfg "inventory" directive to point to that consul_dynamic_inventory script so I don't have to explicitly add the "-i inventory_file" to the command.

My problem:

I would like to add the "ansible_ssh_user=root" to the defaults so I don't have to provide that switch whenever I run ansible.

Usually, in order to add it to all hosts, in the inventory file I'd add:

[all:vars]
ansible_ssh_user=root

But since my inventory is actually a script, I'm not sure where to add it, as adding it to the script obviously breaks its run.

I've tried adding it under the following parts of ansible.cfg file:

[defaults]
[inventory]
[ssh_connection]

But to no avail.

My Question:

Where would be the right place to add it?

Best Answer

Okay, found it.

I was trying to add it as "ansible_ssh_user" in a few places in the ansible.cfg file but it turns out that it should be added as: "remote_user" under the [defaults] part of the ansible.cfg file.