Hi,
So i got this problem and see if ansible can help to fit in.
i got HP server that is only accessible from ILO.
Connecting to ILO is not an issue, because it’s a ssh channel.
From here, we can issue command like textcons, where it will go to the OS level for login.
i can issue textcons command using raw module (as hpilo does not support python), but how do i enter the OS login and password ?
FYI, the OS does not have IP configured, so im trying to go in and assign IP to gain access.
Thanks
To elaborate, once connected to ilo, i will perform the following:
- issue command: textcons
- then the OS login screen appear, i will key in the userid and password
- perform some shell command on the OS
You can do this with the expect module.
Unfortunately, expect is not installed yet.
Probably i could try to send multiple “enter” command
i tried sending “\n” after each command, but seems not “consumed”.
anyone knows how to send “enter” command in a single string ?
for example, this is what i tried to do:
tasks:
- name: sending all the required command
raw: “textcons\nroot\nmypassword\necho date”
You don't need expect on the remote host, only on the Ansible controller(the machine running ansible command).
the hpilo does not come with pexcept. It will through HPILO error.
tasks:
- name: login into ILO and set textcons
raw: textcons
expect:
command: textcons
reponses:
‘hostname login:’: ‘root’
‘Password:’: ‘password’
echo: yes
timeout: 20
FAILED! => {“changed”: false, “module_stderr”: “”, “module_stdout”: “/bin/sh -c ‘/usr/bin/python && sleep 0’\r”, “msg”: “MODULE FAILURE”, “rc”: 0}
What i was thinking is:
- using raw module to execute “textcons”. This is working and will go into the console OS (much like xen using xl console vmname or virsh console vmname)
- From the above, the ansible is something expecting some response back before going to next task. How do i tell ansible to continue to “login into the server using the expect”
Thanks!
I'm pretty sure raw module would be a dead end, and expect is the way to go.
You need to use expect on ssh, so your playbook should have hosts: localhost or set the delegate_to: localhost on the task.
- name: login into ILO and set textcons
expect:
command: ssh <user>@<hp ilo>
reponses:
'some ssh prompt': 'the answer'
'some prompt for when textcons need to be typed': textcons
'hostname login:': 'root'
'Password:': 'password'
delegate_to: localhost
If you show a complete output of a manual run from ssh to the end I can always help with the expect if you like.
On top of this, pexpect has been installed
[root@localhost ~]# pip install pexpect
Requirement already satisfied: pexpect in /usr/lib/python2.7/site-packages (4.5.0)
But upon running it, it keep saying:
OK, apparently i need " ptyprocess " too, but not written in the expect module information.
Hi Kai,
Thanks for the help.
so this is the rundown:
1. After executing the ssh login command, it will produce: *root@servername's
password:*
*Question: *how do escape the quote character in the responses ? can i
instead use * ?
You only need to check for the last bit as long as there are no conflict aka the string appears multiple times in the output.
Expect uses regexp and . (dot) is any character.
2. then it will login into the ILO with this output:
User:root logged-in to servername
iLO Advanced 2.55 at Aug 16 2017
Server Name: hostname
Server Power: On
</>hpiLO->
3. i will need to type either "textcons" or "vsp" to reach the OS console
4. once in the OS console, it will be pretty much like normal linux login
prompt
servername login:
Password:
So this should be close
- name: login into ILO and set textcons
expect:
command: ssh <user>@<hp ilo>
reponses:
password: <the ssh pass>
hpiLO->: textcons
login: <username>
Password: <the OS pass>
<os prompt>:
- <command 1>
- <command 2>
- <command 3>
- <command 4>
delegate_to: localhost
HI Kai,
Thanks a lot.
it did work!
now, managed to get to the server from the ILO as per expectation.
But, wondering 2 things:
- the task always returned failed, with rc:255
- it has reached the server OS (since password has been given), but is there any way to continue working on the server ? that means, once logged in, i need to perform some shell command.
You would need to use the same expect, that is what is illustrated with
<os prompt>:
- <command 1>
- <command 2>
- <command 3>
- <command 4>
Just put in you OS prompt and the you can run as many commands you would like.
To not get rc 255 you need to exit all level, exit in OS and iLO until ssh terminate cleanly.
In expect module you can only have one uniq prompt, if you have many answers/responses they must be a list under that answer/prompt.
Hi Kai,
Thanks so much for guiding through. It seems to work.
:
- <command 1>
- <command 2>
- <command 3>
- <command 4>
Just to understand on your this statement:
In expect module you can only have one uniq prompt, if you have many
answers/responses they must be a list under that answer/prompt.
Does the list of answer and responses must match sequence by sequence ?
Or it does not matter as long as prompt questions are unique ?
Thank you so much. this information shall be provided in the ansible doc too
Just to understand on your this statement:
In expect module you can only have one uniq prompt, if you have many
answers/responses they must be a list under that answer/prompt.
Does the list of answer and responses must match sequence by sequence ?
Or it does not matter as long as *prompt* questions are unique ?
Dict in Python/Ansible is unordered so the order of the prompt/question doesn't matter, and they must be uniq, if not it will fail.
But the answers is a list and the order matter, the first time expect sees the prompt is with use the first element in the list, the second time it sees the prompt is will use the second element in the list and so on.
Thank you so much. this information shall be provided in the ansible doc
too
Yes, but no one has stepped up, feel free to do so it if you want.
Hi Kai,
much appreciated!
thanks so much!