How can i specify different username for different host in command line

46 views
Skip to first unread message

Cola CoCo

unread,
Sep 28, 2022, 2:36:28 AM9/28/22
to Ansible Project
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).

Luca 'remix_tj' Lorenzetto

unread,
Sep 28, 2022, 2:53:39 AM9/28/22
to ansible...@googlegroups.com
Hello,

man ansible

=> --user=user1

Luca
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/1535cb21-bbd8-4a99-9f43-cc080ae53ae2n%40googlegroups.com.



--
"E' assurdo impiegare gli uomini di intelligenza eccellente per fare
calcoli che potrebbero essere affidati a chiunque se si usassero delle
macchine"
Gottfried Wilhelm von Leibnitz, Filosofo e Matematico (1646-1716)

"Internet è la più grande biblioteca del mondo.
Ma il problema è che i libri sono tutti sparsi sul pavimento"
John Allen Paulos, Matematico (1945-vivente)

Luca 'remix_tj' Lorenzetto, http://www.remixtj.net , <lorenze...@gmail.com>

Cola CoCo

unread,
Sep 28, 2022, 3:14:01 AM9/28/22
to Ansible Project
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"

Luca 'remix_tj' Lorenzetto

unread,
Sep 28, 2022, 3:17:41 AM9/28/22
to ansible...@googlegroups.com
Hello,

On Wed, Sep 28, 2022 at 9:14 AM Cola CoCo <wangj...@gmail.com> wrote:
>
> 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"
>

No way to do it without using an inventory.

Luca

Rowe, Walter P. (Fed)

unread,
Sep 28, 2022, 7:12:49 AM9/28/22
to ansible...@googlegroups.com
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
--
Walter Rowe, Division Chief
Infrastructure Services, OISM
Mobile: 202.355.4123
> Luca 'remix_tj' Lorenzetto, https://gcc02.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.remixtj.net%2F&amp;data=05%7C01%7Cwalter.rowe%40nist.gov%7Cc0b6e35a414841bf861608daa12181c9%7C2ab5d82fd8fa4797a93e054655c61dec%7C1%7C0%7C637999462556957119%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&amp;sdata=qI3%2FeQRdWLB5nQYwOX4ja3PvlAC2pyuANCC7uhnEwlg%3D&amp;reserved=0 , <lorenze...@gmail.com>
>
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
> To view this discussion on the web visit https://gcc02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgroups.google.com%2Fd%2Fmsgid%2Fansible-project%2FCAKuX69oOf1jU4oy5_dTciw2_%252BFbEuKx5SDkZayh9tCF1vhifUw%2540mail.gmail.com&amp;data=05%7C01%7Cwalter.rowe%40nist.gov%7Cc0b6e35a414841bf861608daa12181c9%7C2ab5d82fd8fa4797a93e054655c61dec%7C1%7C0%7C637999462556957119%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&amp;sdata=2tL2eZjpTa86zBmsXabC7UimmSiPRTwjI8IIVqYvh1A%3D&amp;reserved=0.

Brian Coca

unread,
Sep 28, 2022, 9:34:37 AM9/28/22
to ansible...@googlegroups.com
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.

--
----------
Brian Coca

Brian Coca

unread,
Sep 28, 2022, 9:36:30 AM9/28/22
to ansible...@googlegroups.com

Brian Coca

unread,
Sep 28, 2022, 9:47:00 AM9/28/22
to ansible...@googlegroups.com
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.

--
----------
Brian Coca

Reply all
Reply to author
Forward
0 new messages