ansible-shell

Hello Guys,

I’m using ansible cli a lot for my daily work to run commands simultaneously on our cluster. Mainly I’m using the shell module but sometimes easier to use the specified module for a job. However I always forgot the required arguments, even for the ones created by me. The other thing I always forgot is the group names in our hosts file.

So I’ve created a little interactive shell for ansible. It can complete the host names, host groups, module names and its arguments.

By default it’s using the shell if there is no matching module and there are three built in commands:

  • cd - change to host/group
  • serial - change the number of forks
  • list - list the nodes in the current group

You can download from: https://github.com/dominis/ansible-shell

At this point there is no installer just drop the file somewhere to your path and run it.

[root@cobbler dominis]# ansible-shell

Welcome to the ansible-shell.
Type help or ? to list commands.

dominis@/[s:2]$ cd app
app-pool app02.bfc.kinja.com app04.bfc.kinja.com app06.bfc.kinja.com app08.bfc.kinja.com app10.bfc.kinja.com app12.bfc.kinja.com
app01.bfc.kinja.com app03.bfc.kinja.com app05.bfc.kinja.com app07.bfc.kinja.com app09.bfc.kinja.com app11.bfc.kinja.com
dominis@/[s:2]$ cd app-pool
dominis@/app-pool (12)[s:2]$ ec2
ec2 ec2_facts ec2_vol
dominis@/app-pool (12)[s:2]$ ec2
count= ec2_secret_key= group= id= instance_tags= kernel= monitor= user_data= wait=
ec2_access_key= ec2_url= group_id= image= instance_type= key_name= ramdisk= vpc_subnet_id= wait_timeout=

dominis@/app-pool (12)[s:2]$ help ec2
create an instance in ec2, return instanceid
Paremeters:
ramdisk ramdisk I(eri) to use for the instance
kernel kernel I(eki) to use for the instance
key_name key pair to use on the instance
image I(emi) (or I(ami)) to use for the instance
vpc_subnet_id the subnet ID in which to launch the instance (VPC)
user_data opaque blob of data which is made available to the ec2 instance
wait_timeout how long before wait gives up, in seconds
ec2_url url to use to connect to EC2 or your Eucalyptus cloud (by default the module will use EC2 endpoints)
ec2_access_key ec2 access key
ec2_secret_key ec2 secret key
id identifier for this instance or set of instances, so that the module will be idempotent with respect to EC2 instances.
wait wait for the instance to be in state ‘running’ before returning
count number of instances to launch
group security group to use with the instance
monitor enable detailed monitoring (CloudWatch) for instance
instance_type instance type to use for the instance
instance_tags a hash/dictionary of tags to add to the new instance; ‘{“key”:“value”}’ and ‘{“key”:“value”,“key”:“value”}’
group_id security group id to use with the instance

dominis@/app-pool (12)[s:2]$ serial 12
dominis@/app-pool (12)[s:12]$ ping
app12.bfc.kinja.com
{‘invocation’: {‘module_name’: ‘ping’, ‘module_args’: ‘’}, u’changed’: False, u’ping’: u’pong’}
app04.bfc.kinja.com
{‘invocation’: {‘module_name’: ‘ping’, ‘module_args’: ‘’}, u’changed’: False, u’ping’: u’pong’}
app07.bfc.kinja.com
{‘invocation’: {‘module_name’: ‘ping’, ‘module_args’: ‘’}, u’changed’: False, u’ping’: u’pong’}
app06.bfc.kinja.com
{‘invocation’: {‘module_name’: ‘ping’, ‘module_args’: ‘’}, u’changed’: False, u’ping’: u’pong’}
app08.bfc.kinja.com
{‘invocation’: {‘module_name’: ‘ping’, ‘module_args’: ‘’}, u’changed’: False, u’ping’: u’pong’}
app10.bfc.kinja.com
{‘invocation’: {‘module_name’: ‘ping’, ‘module_args’: ‘’}, u’changed’: False, u’ping’: u’pong’}
app11.bfc.kinja.com
{‘invocation’: {‘module_name’: ‘ping’, ‘module_args’: ‘’}, u’changed’: False, u’ping’: u’pong’}
app09.bfc.kinja.com
{‘invocation’: {‘module_name’: ‘ping’, ‘module_args’: ‘’}, u’changed’: False, u’ping’: u’pong’}
app05.bfc.kinja.com
{‘invocation’: {‘module_name’: ‘ping’, ‘module_args’: ‘’}, u’changed’: False, u’ping’: u’pong’}
app01.bfc.kinja.com
{‘invocation’: {‘module_name’: ‘ping’, ‘module_args’: ‘’}, u’changed’: False, u’ping’: u’pong’}
app03.bfc.kinja.com
{‘invocation’: {‘module_name’: ‘ping’, ‘module_args’: ‘’}, u’changed’: False, u’ping’: u’pong’}
app02.bfc.kinja.com
{‘invocation’: {‘module_name’: ‘ping’, ‘module_args’: ‘’}, u’changed’: False, u’ping’: u’pong’}
dominis@/app-pool (12)[s:12]$ date
app12.bfc.kinja.com
Fri Jun 7 17:46:40 EDT 2013
app04.bfc.kinja.com
Fri Jun 7 17:46:40 EDT 2013
app07.bfc.kinja.com
Fri Jun 7 17:46:40 EDT 2013
app06.bfc.kinja.com
Fri Jun 7 17:46:40 EDT 2013
app08.bfc.kinja.com
Fri Jun 7 17:46:40 EDT 2013
app10.bfc.kinja.com
Fri Jun 7 17:46:40 EDT 2013
app11.bfc.kinja.com
Fri Jun 7 17:46:40 EDT 2013
app09.bfc.kinja.com
Fri Jun 7 17:46:40 EDT 2013
app05.bfc.kinja.com
Fri Jun 7 17:46:40 EDT 2013
app01.bfc.kinja.com
Fri Jun 7 17:46:40 EDT 2013
app03.bfc.kinja.com
Fri Jun 7 17:46:40 EDT 2013
app02.bfc.kinja.com
Fri Jun 7 17:46:40 EDT 2013

Enjoy,
Nandi

This is quite awesome! Lovely idea, thanks.

Very cool!!!

Checking this out in more detail shortly!

(I’m thinking this may be something we could ship with ansible (possibly with a few tweaks) if that sounds interesting – maybe even as ansible “–shell”?)

Very cool.

Also makes me start thinking about writing an ansible plugin to push to oh-my-zsh as well.

Nice stuff!

Mark

Just put hands on the keyboard and it just feels really good!

Exploring the inventory and running commands/modules across various groupings of hosts is kind of intoxicating. And a little dangerous :wink:

nice! i had a 1/2 hacked version of this using a wrapper and chaining calls through stdin/out, this is much more polished.

Is there something I need to do to enable tab completion?

It is probably something obvious…

I’m running on a Mac OS 10.8.4 using homebrew.

Hey Jim,

Could you please provide some more background info about how you’re trying to use ansible-shell?

Thanks,
nandi

It is an Apple issue.

See article:
http://stackoverflow.com/a/7116997/541202

Pull request shortly.

This makes tab completion work but I don’t see the auto “list” command.

Jim

Hey Jim,

Could you please provide some more background info about how you’re trying to use ansible-shell?

How do you set it to ask for sudo or user password?

Judging from the options available in your code, --ask-pass and --ask-sudo-pass are not available as options.

Any idea when you’ll be able to add those to the tool?

Thanks,

Alex