jonhadfield / python-hosts

a hosts file manager library written in python
MIT License
125 stars 33 forks source link

Work with strings instead of files. #4

Closed dmtucker closed 8 years ago

dmtucker commented 8 years ago

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:

  1. cat /etc/hosts on the remote host.
  2. Write stdout to a local tempfile.
  3. Give the path of the tempfile to Hosts.
  4. Do my changes.
  5. Hosts.write
  6. Read the tempfile.
  7. Tell the host to overwrite its /etc/hosts with str_from_reading_tempfile.
  8. Delete the tempfile.

Is this the correct / most optimal flow?


I think things would be simpler if this supported strings directly:

  1. cat /etc/hosts on the remote host.
  2. Feed stdout to Hosts.
  3. Do my changes.
  4. new_etc_hosts = Hosts.dumps() (like the json module)
  5. Tell the host to overwrite its /etc/hosts with new_etc_hosts.

Just a thought...

jonhadfield commented 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.

dmtucker commented 8 years ago

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...

jonhadfield commented 8 years ago

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.

jonhadfield commented 8 years ago

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.