I executed a command like: ansible all -i "user1@ip1:22,user2@ip2:port2" -m shell -a "echo 1" --private-key /root/.ssh/id_rsa
.
But it could not work.
I want to know whether there is a way to let me specify different user for different host in command line(not using a inventory file).
Hello,
man ansible
=> --user=user1
Luca
Hello,
-user can just specify one user, but what I need is to specify multi users in one command line, like “user1@ip1, user2@ip2”
在2022年9月28日星期三 UTC+8 14:53:39lorenze...@gmail.com 写道:
Hello,
I agree. This has to be done in the inventory. No way to do this on the command line. Inventories can be powerful and help you not have to specify so many things on the command line. You can have group vars and host vars in an inventory.
Walter
the 'command line inventory' is still an inventory, by default handled
by the `host_list` plugin, you can also use the 'advanced_host_list`
plugin or develop your own that can handle any notation you throw at
it.
docs for each:
https://docs.ansible.com/ansible/latest/collections/ansible/builtin/host_list_inventory.html
https://docs.ansible.com/ansible/latest/collections/ansible/builtin/advanced_host_list_inventory.html#ansible-collections-ansible-builtin-advanced-host-list-inventory
the code for both and more:
https://github.com/ansible/ansible/tree/devel/lib/ansible/plugins/inventory
tested several combinations:
bcoca@erebos:~/work/ansible(editor_config)$ ansible-inventory -i
'ip:23, ip2:24' --list
{
"_meta": {
"hostvars": {
"ip": {
"ansible_port": 23
},
"ip2": {
"ansible_port": 24
}
}
},
"all": {
"children": [
"ungrouped"
]
},
"ungrouped": {
"hosts": [
"ip",
"ip2"
]
}
}
bcoca@erebos:~/work/ansible(editor_config)$ ansible-inventory -i
'user@ip:23, user@ip2:24' --list
{
"_meta": {
"hostvars": {}
},
"all": {
"children": [
"ungrouped"
]
},
"ungrouped": {
"hosts": [
"user@ip2:24",
"user@ip:23"
]
}
}
bcoca@erebos:~/work/ansible(editor_config)$ ansible-inventory -i
'user@ip1, user2@ip2' --list
{
"_meta": {
"hostvars": {}
},
"all": {
"children": [
"ungrouped"
]
},
"ungrouped": {
"hosts": [
"user2@ip2",
"user@ip1"
]
}
}
the issue is that user@ip breaks the 'address parsing' that would
separate ip:port, while user@ip is 'valid' as a hostname and when
passed to ssh 'just works'tm
So you would just need to modify the plugin to extract user@host
(puting the 'user' value into ansible_user) before calling the
parse_address function.