home ~ about ~ photos ~ family ~ arcana ~ org-mode ~ network tutorial ~ MAAS from the CLI

Configuring MAAS with the CLI


MAAS: Install ~ Configure ~ DHCP ~ Commission ~ Deploy ~ jq ~ SSH ~ More jq


Configuring MAAS via the CLI only

Now that MAAS is up and running, it's time to configure it. Normally, this would be accomplished through the MAAS configuration journey, but since I'm using the CLI for this, I want to avoid the UI as much as possible.

Logging in

The first step in the config journey is logging in. In the CLI, that's a two-stepper:

You can make sure you got a valid API key by displaying the contents of api-key-file:

Note that string isn't an actual API key, just characters I made up.
Anyway, I can now login to MAAS -- but first, let's try maas --help -- there's an important distinction that gets skipped over, causing some grief:

This is the help you get (a) if you're not logged in, or (b) if you don't type a logged-in username after maas - which is a little annoying to me, but it's just how we do it. What you see above isn't even half of what the MAAS CLI will do, but it's all you get as an unrecognized user.

So now, let's login and try that help again:

Getting real help

Now, having done that, I can get a much better idea what the CLI will do:

Wowee! Look at all the commands! The very first time I tried to use the MAAS API (before I actually hired on at Canonical), I was using various commands I could find in the documentation that actually returned things I wanted, and then digging it out of the JSON output. Not fun. Then someone reminded me about jq (we'll come to that in a minute), and things got a little easier. Eventually, I asked one of the developers what the deal was, and learned that I just missed a the section called "Get help" in the CLI introduction. After that, a whole new world opened up.

But we came here to configure MAAS, not tell stories, so let me see what I can do with this beast.

Setting dns

The very first blank line you encounter in the MAAS UI is the DNS server IP address. In the UI, I just type "8.8.8.8" (Google's DNS server) and forget about it. But the CLI has no box, so how do I get there? Well, there is a subcommand called <code>dnsresource~, let's see what that does.

Okay, let's be naive and try that:

Well, that isn't going to help me, I don't have any idea what the "dnsresource id" would be. Hmmm. Oh, wait. This CLI follows the "collection-instance" rule, that is, listing DNS resources would be part of a collection, so would be pluralized. So, for example, I can read <code>dnsresources~ (plural) and maybe find out something:

Well. Maybe I should try the CLI documentation. Looking at the "Quick questions" under common tasks, I see there's one about setting a DNS forwarder. Let me check that out. Hmmm, yeah, that might be what I want. There's a easy way to tell: I can check the DNS setting in the MAAS dashboard in the UI, run the CLI command I found, and see if that sets it.

Okay, actually, I cheated first. I went and asked my friend Lee, who knows how this works, and I kept asking until I annoyed him. But it's useful to illustrate that there was a way to get this from the doc and experimentation, which I should have done. * Sorry, Lee. Owe you lunch. Maybe that's two lunches. *

Hey, in that field, it even says "DNS forwarder" -- so let me try it:

And it sets it! Woo-hoo, it worked! It wasn't obvious whether I needed to type the IP address with quotes, but I did, and it paid off. I guess I could try it without quotes to see what happens:

And that works, too, so I'll change it back real fast before weird things start to happen, since I have no idea what "9.9.9.9" is.

note to self: always RTFM. before annoying my friend Lee.

Importing images

The next thing would be to import images. When I look at the dashboard, it's already been done, but TBH it was already synched when I logged in and opened the dashboard, so it must be automatic for at least one default image. Dunno. But I can bring in some other image (like Ubuntu 16.04 LTS) just to see how that works, and I can figure out how to confirm that the 18.04 (default) image is actually here.

Well, this time actually consulting the manual, it says I can confirm 18.04 by entering the following command:

The JSON resulting from this command is rather lengthy:

Okay, that's a lot of information, but it looks like I have a bunch of 18.04 images downloaded and synched. Let me try to get a little fancy with a grep and see if I can make that list shorter:

This produces a quick list of the images I've successfully downloaded:

That definitely confirms 18.04. But what are those three or four on top? Looking at the massive JSON output, I can see that they have names like "open-firmware," "uefi," and "pxe." Okay, so those are images that can PXE-boot machines, basically. But how could I sort this information out in a neat way?

Enter jq

If you're going to use the MAAS CLI -- or anything with JSON-based output -- you'll want to consider learning the command line tool jq. It's quite handy for parsing the JSON output of the MAAS CLI. So, for example, if I want a (sorta) formatted table of names and architectures, I can run my last command through jq like this:

This gives me a clean image list that looks something like this:

Okay, I cheated just a little there, too: I cleaned up the tabs a little, which wouldn't be quite as well lined-up unless you have a really wide tab setting (which is possible). Even what I did isn't enough to straighten it out totally, but the output is still readable, anyway.

So you can see that I basically have (a) the images I need to boot machines, and (b) an 18.04 image (set) to deploy. That's a good start, but let me see if I can pull down another image with the CLI. I know I can select images with the boot-source-selections command, so let me try that with "Trusty" (Xenial Xerus, aka 16.04):

The results look like this:

And that appeared to work, which is good, because that was a long command to type correctly! Sure enough, if I look back at my "Images" tab in the UI, 14.04 LTS is now "Selected for download." Okay, this CLI seems to be working fairly well, let me just keep going and see if I can get it to download, now.

In reality, downloading them -- ahem, I mean "importing" them -- is a fairly simple command:

Now when I switch back to the Images screen, I can see that 14.04 LTS is downloading, which is fantastic. I'll just let that download for a while and then move on to enabling DHCP.

Updated 2022-08-10 Wed 14:40.
Copyright (C) 2020-2022 by stormrider. All rights reserved.