ericsysmin's DevOps Blog

How to Install Pyenv on MacOS

Steps to install Pyenv on MacOS

There are a few ways on MacOS to install Python. You can install it via Brew, or by using Pyenv. After needing to switch between different versions of Python often I’ve decided to move to Pyenv. Prior to these steps I removed all versions of Python installed directly with Brew.

1. Update Brew and install prerequisites
We will need to update brew.

In some cases when installing Python >=3.12.1 we will need ncurses. If it’s missing you can install using:

2. Install Pyenv using brew

The recommended way to install pyenv on MacOS is brew.

3. Brew doctor fix

If you want to avoid brew doctor warning about “config” scripts existing outside the system or Homebrew directories please include the following in your shell.

4. Configure your Zsh profile.

If you wish to use Pyenv in non-interactive shells, add the following:

5. Restart shell

6. Install python 3.12

I am going to show how to install python 3.12 but you can select any version of your choice.

7. Switch between your python versions

pyenv shell <version> – modifies python for the current shell session

pyenv local <version> – modifies the python used in the current directory (or subdirectories)

pyenv global <version> – modifies the python used for your user account

 

Continue reading...

How to Install Pyenv on Ubuntu 22.04

Due to the slowness of repositories or even lack thereof being updated with specific versions of Python, I’ve decided to move some of my environments over to Pyenv to allow me to dynamically install and configure Python specifically for my environment. As it turns out this will also allow VS Code to allow me to choose the version of Python that I’d like to use when testing. So, here’s a quick guide to installing Pyenv on Ubuntu 22.04

Steps to install Pyenv on Ubuntu 22.04

1. Update and Install Dependencies

We need to ensure our package cache is updated, and then install the dependencies to download, and build Python from Pyenv.

2. Install Pyenv using pyenv-installer

3. Configure user profile to use pyenv

Ensure the following is in your ~/.bash_profile (if exists), ~/.profile (for login shells), ~/.bashrc (for interactive shells), or ~/.zshrc

Optionally enable pyenv-virtualenv

4. Reload your profile

5. Install python using pyenv

6. Set your python version

pyenv shell <version>  — select just for current shell session
pyenv local <version>  — automatically select whenever you are in the current directory (or its subdirectories)
pyenv global <version>  — select globally for your user account

7. Validate your installation of python

or

 

Continue reading...
Installing ZSH on WSL: Ubuntu

Installing ZSH on WSL: Ubuntu

Having moved away from development of Ansible/Python/Terraform, and others on Windows a few years ago. I’ve heard that WSL has become much better, especially with its support of VS Code now that they have a WSL connector. So, I decided to give it a try.

However, I did run into one surprising inconvenience, BASH! I have become so accustomed to ZSH over the years on Apple and using oh-my-zsh that all my shortcuts and quick ways to doing things just weren’t there.

I did attempt to use oh-my-bash but there just aren’t the number of plugins already to support the plethora of plugins I need and use on a regular basis. So, next logical step is to install ZSH on my WSL: Ubuntu system.

Requirements

WSL with Ubuntu installed

Steps to install ZSH

  1. Install ZSH.
  2. Verify that ZSH has been installed on the system.
  3. Set ZSH as your default shell.
    NOTE: This used to require modifications to the shortcut to access WSL but in newer versions of WSL you can use the following.

    For older versions you will need to modify the shortcut to WSL and use the command:

  4. Once you close and reopen your WSL Ubuntu shell you should now be in ZSH shell.
    zsh-shell-screenshot

Install of Oh-my-zsh

  1. Ensure that your terminal session is open and use the following command as per https://ohmyz.sh/#install
  2. You should now be able to configure your oh-my-zsh installation by modifying your .zshrc file and enable any plugins you need.
  3. The final result! 🙂 

    Screenshot of oh-my-zsh on WSL: Ubuntu

 

Hopefully that helps, I know it helped me as I venture back to Windows for development of my Ansible playbooks and other tools.

Continue reading...
Import Self-Signed Cert for Chrome/Edge/Chromium on Mac

Import Self-Signed Cert for Chrome/Edge/Chromium on Mac

With the latest versions of Chrome, Edge, or Chromium, it’s no longer possible to load pages with self-signed certs and they provide a NET::ERR_CERT_INVALID error.

ERR_CERT_INVALID

When you go to the “Advanced” button it does not allow you to ignore and proceed.

To work around this you can download the certificate. Then import it into the trusted certificate store.

I used OpenSSL to download the certificate, but there are other options. Here are the commands I used.

This will add your certificate to the System Keychain and trust it as an SSL certificate.

If you get an the error:

try replacing

to

Continue reading...
Ansible Collections: Role Tests with Molecule

Ansible Collections: Role Tests with Molecule

As many of you know or are finding out, Ansible is moving to Collections. But what does that mean? Well, it’s been a long time waiting but Collections provide a way to namespace modules, roles, and playbooks that can all be combined in a single package for you to consume. It also allows businesses, partners, and contributors to update modules without adhering to the Ansible core release cycle. So, if AWS updates their API, then the modules that go with those will be instantly accessible, or at least faster than we used to wait for core releases to get those modules. But what does this mean for roles?

Move to FQCN (Fully Qualified Collection Name)

As many of us our finding out we are needing to move our roles to the collection design. However, now we need to figure out how to test them with the new design and using the Collection Namespaces aka FQCN (Fully Qualified Collection Name) So what we used to write

Will now end up something like this

We also are going to have a new folder structure using ansible_collections/namespace/collection_name

Luckily the molecule team and all of its contributors ensured that collections are recognized and supported. And I will cover how we can test this with GitHub Actions (which happen to also be the preferred way at this moment to test your collections on the official Ansible-Collections Github https://github.com/ansible-collections.

Single Command Testing

Example with multiple role tests in one collection molecule test.

As you see, in this format I had to make sure RedHat specific roles don’t get run on non-RedHat systems. But then this tests every role together and doesn’t easily allow me to scale to even more operating systems.  It will get long, and crazy with lots of when statements, and each time you add a role, you’ll have to edit this one and ensure the environment configuration is correct for all of the roles. It was at this point I realized maybe I should move away from Travis-CI, and also was encouraged to by the Ansible team. Gundalow and others recommended moving to GitHub Actions which are the preferred method now in the community. So I explored that option.

I also at this time decided to move away from the monolithic “default” scenario and instead divide scenarios based on roles. This is what I moved to.

Now I was able to individually ensure each environment was correct. That includes each roles’ dependencies, etc, and that it doesn’t affect the others. It prevented any form of cross-contamination on testing, and what’s expected in requirements. This fixed one of my issues I had with testing, however, it did add a bit of complexity, but each one is almost identical so I could easily copy and paste one of these to help build tests for the new role.

Matrix Testing with GitHub Actions

When I started testing how I would do this using Github Actions I explored using Matrix workflows. That looked something like this (which was really awesome because I couldn’t do it in Travis-CI…I don’t know if I can go back to Travis-CI because apparently for me GitHub tests are much faster.)

However, here’s the issue with this…each commit no matter where it’s made will trigger the matrix to execute meaning 4 x 6 tests! Even though I modified 1 role. Also, EPEL doesn’t work on Ubuntu or Debian. So then I’d have to use a lot of these exclude  statements:

Of course, that’s not scalable. So, I gave it a bit of thought. Why don’t we treat each role for what it is? It’s a separate role. Editing Role1 shouldn’t affect Role2 or even need to test Role2 in this situation. So I decided to create multiple workflows. Using GitHub Workflows I created the following structure.

Each workflow is specific to each role, and each one looks similar. This is the template I used.

You can change the operating systems if you want, this is just one of the examples I had. I solved the issue with the cross-contamination I had earlier, as well as made the tests easier to verify and check, as well as independent test state icons for the README.md. But I still had an issue. If I make a change to Role1, Role2 still builds…not desired and wastes build time against GitHub Actions.

Luckily in GitHub Actions, we can do include, or exclude paths on the trigger. So I replaced this section on: ["push", "pull_request"] with

So now the role only executes when the items specific to that role are edited. Saving me possibly money, and build time so other jobs in my GitHub can execute.

Now my completed .github/workflow/chrony.yml  looks like this:

Since these changes have been made now I am able to ensure that all of my roles are independently tested each time they are edited without treating everything as one giant repo and having tests run for 10+ minutes each as all of my roles execute. Now they are all tested in parallel, and against their own supported operating systems.

To see a copy of the repository used for this you can see https://github.com/ericsysmin/ansible-collection-system which you are free to clone, modify, change, use a reference. I did make changes to the Dockerfile because I do not host my own docker images, and don’t plan to. I highly suggested taking a look at my molecule/role_name/Dockerfile.j2  files to get an idea on what I did to get services to work. My changes to Dockerfile.j2 are based on Multi-distribution Ansible testing with Molecule on Travis-CI, and check out molecule/role_name/molecule.yml  to see how I pass through the parameters.

If you have questions please feel free to comment.

 

Continue reading...