pyinfra-dev / pyinfra

pyinfra turns Python code into shell commands and runs them on your servers. Execute ad-hoc commands and write declarative operations. Target SSH servers, local machine and Docker containers. Fast and scales from one server to thousands.
https://pyinfra.com
MIT License
3.91k stars 383 forks source link

Ability to build a server from an image #740

Closed Taisgeal closed 2 years ago

Taisgeal commented 2 years ago

Is your feature request related to a problem? Please describe

I work in a hybrid environment (part local data centre, part cloud). I can build a new server from an image in either environment using ansible, by specifying a few parameters (image name, location, IP addresses etc.)

Describe the solution you'd like

I would like to be able to do this from pyinfra. In fact, this is the only thing stopping me from ditching ansible altogether.

bauen1 commented 2 years ago

I'm already doing something like that with pyinfra:

  1. I'm not using pyinfra directly, but a python script that mirrors most things the CLI does to obtain the inventory etc...
    • This is because pyinfra wants to connect to all involved hosts at the start, which won't work if the host does not exist yet for obvious reasons.
  2. Uses the inventory to template simple-cdd configuration files (basically the debian preseed file, a postinst script and a list of packages to install)
  3. Invokes simple-cdd to build the installation image
  4. Uploads the image to my server

After that I can use the URL to upload the image to my VPS provider and kick off the installation.

Taisgeal commented 2 years ago

That sounds like a very sensible way to do it.

I was thinking of taking this approach :

  1. Manually build a server with -file systems set up using LVM -networking set up using a prereserved set of IP addresses and a predefined "golden image" hostname -software installed -user accounts created ( i only have three of these) -custom changes made to files such as resolv.conf
  2. Using this "golden image" , clone it and just change the networking and hostname to whatever I want.

I hope to use pyinfra with Step 2. Step 1 is manual, but as my golden image only changes every few years, I can bite the bullet.

What do you think of that approach ?

bauen1 commented 2 years ago

That's also a doable approach, a couple of issues you will have to consider:

However I believe there are other projects supporting this approach, e.g. systemd first boot, maybe cloud-init ?

Taisgeal commented 2 years ago

That makes a lot of sense. Thank you for taking the time to write down your thoughts.