Formatting shell output into structured data? - shell

Are there any means of formatting the output of shell commands to a structured data format like JSON or XML to be processed by another application?
Use case: Bunch of CentOS servers on a network. I'd like to programatically login to them via SSH, run commands to obtain system stats and eventually run basic maintenance commands. Instead of parsing all the text output myself I'm wondering if there is anything out there will help me return the data in a structured format? Even if only some shell commands were supported that would be a head start.

Sounds like the task for SNMP.

It is possible to use puppet fairly lightly. You can configure it to run it's checks only on what you want to check for.
Your entire puppet config could consist of:
exec { "yum install foo":
unless => "some-check for software",
}
That would run yum install foo but only if some-check for software failed.
That said there are lots of benefits if you're managing more that a couple of servers to getting as much of your config and build into puppet manifests (or cfengine, bcfg2, or similar) as possible.

Check out Nagios (http://www.nagios.org/) for remote system monitoring. What you are looking for may already exist out there.

Related

How to upload a file to a server, that's not in the inventory?

Sometimes we need to upload logs of an application, that's distributed among multiple local Unix machines, to the vendor's server. The machines are all part of the same inventory, and can perform the archiving of the logs, and uploading the archives directly.
The server runs Unix and accepts only SCP and SFTP, so synchronize module (which uses rsync) will not work.
There exists a net_put-module, but that seems intended for uploads to special network appliances -- trying to use it, I get cryptic errors about ansible_network_os...
I can, of course, use the command module, but is not there something specifically targeted for SCP- and/or SFTP-servers?
No, there is no module for scp or sftp, and I don't really see that it would provide a lot of value. sftp and scp are straightforward to use with command, and the underlying commands don't really support the things you might want a module to do, like skipping an upload if the file on the remote wouldn't change.

Ansible querying data

Ansible is primarily designed for running tasks on remote machines. I want to use it in kind of the reverse - pull data from a bunch of machines and store it locally. I love the setup module but right now I'm interested in pulling a list of users - which the setup module doesn't show me.
Also, I want to add this info into a mariadb table.
I can do this project with the shell module but, my question is, is there a better way?
Unless someone can tell me a better practice, I'll use the shell module for two parts: First, I'll simply cat /etc/passwd and register the output from that. Then, do a local_action shell script that invokes a mysql command to update the table.
I'd love to both pull the data and insert/update the data into my table using standard modules. But there don't seem to be modules that will do either.
You could use local facts. It is described here https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html#local-facts-facts-d
If a remotely managed system has an /etc/ansible/facts.d directory, any files in this directory ending in .fact, can be JSON, INI, or executable files returning JSON, and these can supply local facts in Ansible.
then you get the local facts in json format with this line:
ansible <hostname> -m setup -a "filter=ansible_local"
For system user facts you could write a shell-script which returns the system user in json format and put this script under /etc/ansible/facts.d . Each time you gather facts from this machine, you have this facts too.

Ansible - wait for user to key in value for interactive scripts on remote machine

I would like the ansible to wait for my input in the command line for interactive script running in remote machine. "Expect" will not suite my requirement as the interactive questions keep changing.
eg
xxx.pl
This must be the user which is running service. [root:root]': y ----> i should be allowed to change in realtime
handling utilities? [/usr/bin]: y ---> same with this
This is not possible with Ansible.
Ansible packs all task scripts/parameters before sending it for execution to remote host and there is no way (as of Ansible 2.4) to get any feedback during task execution – only final result of task.
For anyone (like myself) curious about this and landing on this page per google. Looks as if it's now possible through the prompts feature! (I don't think it's even so much as a built-in module. It seems like it's just built-in. Can't find it under Ansible.Builtin Plugin Index. It's just under Playbook Advanced Features.

Simple way of copying a file to a remote box via SCP using a Rake Task?

I'm used to Python Fabric in the past and I'm trying to do something similar with Ruby.
Basically I have created a Rake script which will run as a particular user which has SSH keys setup for passwordless access to the boxes in question.
I've managed to use https://github.com/seattlerb/rake-remote_task in order to run a command remotely, and expected the "put" method to "just work". However it seems to be an Rsync wrapper which does not take advantage of the keyless authentication.
It also seems to expect the file to be generated by a template which is not what I want, I want to SCP an actual .tgz binary file.
Am I missing something in the Ruby/Rake ecosystem. I expected this to be easy, but I feel like I'm going to need to go back to searching for gems?

Ruby - How to start an ssh session and dump user into it - no Net::SSH

So I've got dozens of servers I connect to and want a simple Ruby script that provides me with a list of those servers. Picking one will start up SSH with the proper connection details and let me start using it. That's it!
But, I don't want/need Ruby to keep running. If I did I could use Net::SSH and capture all output and send it back to the user, but this is an extra layer I don't need. I simply want to use Ruby as a "script starter" and then close itself.
Any ideas? I've thought about forking processes but I don't know how I'd attach the terminal to the new ssh one.
I have a simple bash script that does this already, but I want to add more functionality like being able to add to the list, remove servers, etc. all from the command line. I'm sure I could do this with bash as well but I'm much more comfortable with Ruby.
Maybe exec will do the trick
http://en.wikipedia.org/wiki/Exec_(operating_system)
http://ruby-doc.org/core/classes/Kernel.html#M005968

Resources