Closed dmtucker closed 8 years ago
It sounds like you need to wrap this with a script or an orchestration tool. I'd use something like Ansible (simple and agentless python based tool that can update remote hosts in parallel) to: 1) Run pip on the remote instances to install python-hosts (https://docs.ansible.com/ansible/pip_module.html) 2) Copy a list of hosts in a text file to the remote host (https://docs.ansible.com/ansible/copy_module.html) 3) Copy a script to the remote host that reads from the hosts file you've uploaded and then write to local /etc/hosts (https://docs.ansible.com/ansible/copy_module.html) 4) Use command to run the python script locally and return the stdout/stderr https://docs.ansible.com/ansible/command_module.html
You could write a bash script that would do all of the above also, i.e. 'scp ....' to transfer hosts file entries and python script then run a remote command like mentioned here: https://stackoverflow.com/questions/305035/how-to-use-ssh-to-run-shell-script-on-a-remote-machine to run a 'pip install python-hosts' and then execute the uploaded script.
Other tools like saltstack, rundeck etc. can also achieve something similar.
I wouldn't want to add this to this type of functionality to this library as the types of tools/techniques for remote management already exist.
Thanks! I definitely need to learn more about Ansible.
For now I have settled on installing this library then sending a script that uses it.
I still think mimicking the json
library and dealing with strings is a good idea FWIW...
Thanks for the feedback. The issue I have with dealing with sending stdout (from the cat of /etc/hosts) is that it assumes the library will run in a terminal. I've already written a tool that wraps around this library to perform those types of tasks: https://github.com/jonhadfield/hostman and will look at extending that to propagate a Hosts instance as you describe. It's a bit of an old WIP though, so feel free to fork and butcher or send a PR, etc.
You have given me the idea of adding a parameter to the write method of Hosts though, i.e. pass a file path of where to write in case you simply want to export it.
I've added a 'path' parameter to the Hosts write method and tagged and published as 0.3.4 (including your PR). Please shout if you see any issues. Thanks.
This library looks really handy :+1:
Having to interact with files is a bit painful for my current use case, though, because I need to change the /etc/hosts on a remote host. I believe that would mean:
cat /etc/hosts
on the remote host.Hosts
.Hosts.write
str_from_reading_tempfile
.Is this the correct / most optimal flow?
I think things would be simpler if this supported strings directly:
cat /etc/hosts
on the remote host.Hosts
.new_etc_hosts = Hosts.dumps()
(like thejson
module)new_etc_hosts
.Just a thought...