kdwarn

Codeberg Mastodon Feeds

Home > Programming > Blog

Made Up of Wires

Subscribe Feeds

Full Posts [switch to table of contents]

tagged: linux [clear]

Note: visiting individual posts will show related follow-up and previous posts, if any exist.

Creating a Python Virtual Environment in Debian via Ansible

July 24, 2021

coding, python, ansible, linux | permalink

Debian is my preferred Linux distribution, particularly for servers. Although it is the base from which so many other distros are derived, it seems that it gets short shrift by a lot of packages. It took me a little while to figure out how to create a Python virtual environment on it with Ansible, so here is a quick example in the hopes that it may help others who hit this particular hurdle.

Two system installations are necessary:

- name: Install system packages
  apt:
    name:
      - python3-pip
      - python3-venv
      # others you need
    state: present

I prefer to use built-ins wherever possible, so I use venv rather than packages like virtualenv or pyenv for virtual environments. Here is the task that will create one, using the pip module. It will also install any dependencies listed in a requirements.txt file:

- name: Set up virtual environment and install requirements.
  pip:
    virtualenv_command: /usr/bin/python3 -m venv
    virtualenv: "/project/path/ve"  # the directory to install the ve
    requirements: "/project/path/requirements.txt"

You would think that would be all it takes, but for some reason, Ansible still appears to want to use Python 2 with Debian, so you need to tell it again that you want to use Python 3, this time at the inventory level. Declare the executable explicitly in the inventory file with the ansible_python_interpreter variable, e.g.:

all:
  hosts:
    my_host:
      ansible_host: # domain or ip
      ansible_private_key_file: # /path/to/key
      ansible_python_interpreter: /usr/bin/python3

Voilà, you should have a Python 3 virtual environment using the built-in venv module.

Vagrant, Libvirt, and Ansible

April 3, 2021

coding, ansible, virtualization, linux | permalink

I recently started to learn how to use Ansible, from Jeff Geerling's book Ansible for DevOps, which so far I highly recommend. A bit of a pain point has been using virtual machines created by vagrant as if they were remote servers — without using Ansible as a provisioner and just running Ansible commands like normal. I think I now have a good workflow that I thought might help others in this situation.

First, a couple notes on my setup: my OS is Debian Testing (which I just upgraded to last week from Buster, which was surprisingly simple and painless to do) and I'm using libvirt as the provider, but this should probably be applicable to other configurations. I'm not going to cover installation of these or vagrant or Ansible, though perhaps someday I'll edit that in.

  1. Create the virtual machine with vagrant. If you haven't yet added the box you want to use, add it with vagrant add box [user]/[box]. Let's say it's CentOS7, since that's what Geerling tends to use: vagrant add box generic/centos7. (I haven't been using the boxes he made since they use VirtualBox rather than libvirt.) Then initialize it: vagrant init generic/centos7. This creates the configuration file, Vagrantfile, in the current directory. If you'll be using some kind of web server on the virtual machine, open that file up and uncomment the line config.vm.network "forwarded_port", guest: 80, host: 8080 (putting in whatever host port you'll use in your browser to connect to it).

  2. Start up the virtual machine: vagrant up.

  3. Get the ip address of the vm: vagrant ssh-config. Either use it directly or put it in an Ansible "inventory" file.

  4. Add the ssh key of the vm to your SSH authentication agent. For libvirt vms, this is located at .vagrant/machines/default/libvirt/private_key from the vm directory (where the Vagrantfile is located), so run ssh-add .vagrant/machines/default/libvirt/private_key. I imagine that if you use a different provider, it would just be a matter of substituting its name for the "libvirt" directory.

That should do it — you should now be able to use ssh or ansible or ansible-playbook to connect to the virtual machine.

Note that if you set up multiple servers on the virtual machine and then run an Ansible command on them, you'll need to confirm the ssh fingerprint the first time. Since Ansible runs in parallel mode by default, you'll repeatedly get asked to confirm it, and it doesn't always seem to work. So I'll use one fork in this case by passing in -f 1 to the command the first time.