0

It's neither a secure idea nor one I'd recommend elsewhere, but I have a situation when occasionally it takes a while for my Ansible ad-hoc command to respond. I'd love to pipe or args or whatever is needed to push the required text into the prompt so I can walk away and know it will finish.

Ex:

$ ansible all -m shell -a "reboot" --ask-pass
Password:
blah blah blah it worked

I'd love to send an argument or << or something to get the password in. Is that possible?

8
  • possible duplicate of Pre-authorise sudo? (So it can be run later) Aug 19, 2014 at 20:34
  • Good link @G-Man. Feels like a different goal though -- I can remove the sudo to clarify. It's just about the sending of text back to a pending command prompt, no more.
    – mbb
    Aug 19, 2014 at 20:42
  • 1
    If the goal is a shutdown or reboot after hours of unattended operation, then I think at least one or two of the answers there should work for you. If it's just about providing input to a long-delayed tty read, then why not just type the input in advance? You don't need to wait until some process issues a prompt and starts reading. Aug 19, 2014 at 20:47
  • 4
    @mjb: What about Expect?
    – cYrus
    Aug 19, 2014 at 20:49
  • 1
    @g-man I'm talking about the latter - delayed tty. I type out the prompt reply and hit enter, though sometimes it doesn't seem to catch the input. I don't want to take that chance if there's a better solution.
    – mbb
    Aug 19, 2014 at 21:24

2 Answers 2

1

You can use Expect for such tasks.

Here's a starting point:

spawn ansible all -m shell -a "reboot" --ask-pass
expect "Password:"
send "secret\r"
expect eof # from this point it depends on the behavior of Ansible...

Save this snippet to a file, say reboot-ansimble.sh, then run it with:

expect reboot-ansimble.sh

PS: If Expect is not installed you should be able to fetch it from default repositories:

sudo apt-get install expect
0

May I suggest switching to key-based authentication? https://www.digitalocean.com/community/tutorials/how-to-configure-ssh-key-based-authentication-on-a-linux-server

Key based auth allows you to log into a server without a password, and is far more secure than passwords. I have a role in Ansible Galaxy for managing users and keys here: https://galaxy.ansible.com/list#/roles/4840

3
  • this is not answering the question, since you are showing only partial solution for the provided example.
    – Jakuje
    Aug 23, 2015 at 16:29
  • While it doesn't answer the specific question, I think it solves the problem much better.
    – MillerGeek
    Aug 23, 2015 at 16:47
  • yes. Problem in this one example. But not general problem, if you want to wait for something else. And this is the problem.
    – Jakuje
    Aug 23, 2015 at 16:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .