Developer Productivity

Creating Ansible Tasks to Install Zsh



Developer Productivity

Check out a free preview of the full Developer Productivity course

The "Creating Ansible Tasks to Install Zsh" Lesson is part of the full, Developer Productivity course featured in this preview video. Here's what you'd learn in this lesson:

ThePrimeagen demonstrates creating Ansible tasks to handle the install of Zsh, change the default shell, install ohmyzsh, and install the Zsh plugin for auto suggestions. The differences between Ansible and a bash script and a brief description of sed are also covered in this segment.


Transcript from the "Creating Ansible Tasks to Install Zsh" Lesson

>> So what I think we should do is let's create the zsh install, but this time instead of me doing it by hand one at a time, let's just use Ansible. So I'm gonna jump back here, and I'm gonna exit this. We're gonna exit this one too. I'm gonna go to, let's see, where do we wanna go?

Let's go to Ansible really quickly, and I'm going to just open up Vim right here and I'm gonna delete this cuz I definitely have not practiced this once ever. This is brand new, first time I've ever done this. And so we're gonna open up, we're gonna call it fem.yml, Frontend Masters YAML.

I'll go back here, and let's just cut and paste this whole thing. There we go. And we have that, we don't need pre_tasks. We don't really need vars. We can have tasks. There we go, looking good. I don't remember the shape of the task, so I'm just gonna copy and paste this one right here.

This feels about right, let's, oopsies, let's paste this one in. There we go. We have a name, looks like this, install zsh. And then let's go apt, oop. For whatever reason, my indenting, I have yet to figure out why it doesn't work right. It just drives me nuts.

And then we don't need any tags. So we're gonna go apt install name=zsh. There we go. I now have something that will just simply install zsh. So let me just close this down and let's just rerun Docker. Oops, actually I have to build Dockers, don't worry. I believe I actually have, I thought I had a, what is it, a build script.

Yeah, there we go. And so now we can go ansible-playbook, and we can pass in fem.local right, fem.yml. This will take that and it'll run everything. Okay, I need to install zsh for you. And so it's gonna go out, it's gonna download it, it's gonna make everything happen, and it's gonna tell you kind of what happened.

So now I can actually execute zsh. Now this isn't my full environment, but I now have something that specifies what to do for me. So when I have something new, I don't have to remember every single step along the way. So let's exit zsh, let's exit Docker. There we go.

So we're back here. Let's reopen up this, let's grab that. Let's add it, make sure that's the only thing. There we go. And there we go. So we have this task but we need more tasks. Remember, I also have to remember to change my shell so that when I start up a new terminal, I'm using the shell that I expect to use.

So I can go in here and go Change shell, I'll just give it a nice name so I don't have to forget. Shell: chsh -s 'which zsh'. So it's just like the previous one, we did this command before but now I've just simply specified it right here and we can do it again.

Now you go, install ohmyzsh (please don't tell people I use this). There we go. Now we have that and of course, we can do the ohmyzsh installation, which I will never remember off the top my head. So I actually have to just simply copy and paste this. And so let's copy, let's paste this.

There's probably a better way to do this. It's gonna give me a warning, hey, use curl instead of just using shell but we're just gonna do that for now. Then we got to remember we need to install the zsh plugin for auto suggestions, right? So let's do that right now, we'll go here, and there is a way to be able to do Git.

So I don't need to use the shell, I can actually use a specific Git plugin. So let's just paste those in here. And I can go Ansible, you can look all these things up really easy. So I'll go like this, ansible git clone. Go in here, top one.

It right away always has examples, jump to the example of that repo, distribution and you can even say version. So, very, very nice, awesome. Let's jump in here. Let's just paste that in there. All right, there we go. So I have Ansible Git and then I can just specify repo.

So let's just specify repo. Let's take, let's say D word. There we go. Let's paste that in there, fantastic. What was the question?
>> What's the difference between this and just some ugly bash scripts?
>> The difference between this and an ugly bash script is that, well, we're gonna get there where Git's a lot better.

I kinda wanted to save that here for a moment, what really makes this significantly better, but you are right. This is a lot like an ugly bash script but it's gonna get a lot better. So I would like to kind of show that as we go. Don't worry, I have some things planned but yes, this is like a bash script but using YAML to kind of specify each task.

Now I used to do the bash script way, I actually had about a 300-line install script, and I would just like to let you know that I never successfully had it run first try. I know, I usually do everything first try, but this time I just couldn't quite get it to do it first try, and it's really, really hard.

Whereas this I found to be very easy, especially when it comes to using Git, using some of the finer features, being able to do all that extra stuff cuz even when you do, say, this get operation, I could also do something with submodules in here. So I could specify how they want to use submodules.

I can say, hey, use my ssh key, instead of just using an HTTPS clone. There's a lot more to it but I'm just trying to keep it simple for now. Right now this just looks like a really simple bash scripts gone complex, right? So we'll get there, don't you worry.

And we have one last task we need to do right now. So let's paste this thing in here and we need to update our zshrc, right? Now, I'm gonna cheat, so don't do this at home, kids. But I'm just simply gonna use sed to kind of replace that one line inside my plugins.

So I remember the line that says plugins equals get, but I'm actually gonna make it call git zsh-autosuggestions. I'm pretty sure that even Ansible will say, hey, don't use sed. You should install this from a clean location. Now normally I have dotfiles that already have this in here, but for this little example, I'm just simply gonna replace git with this so everything else works, and then of course pass in the zshrc, zshrc, there we go.

Everything looks good. So let's quit the script. Let's rebuild the dockers. So there we go. We got that going here, dockers run. So this is why I created a clean environment to test things over and over again. Is that I always have something I can test, and to make sure it actually works on a new system.

Even when I was doing this presentation, my zsh script wasn't necessarily correct. I didn't have to do some of these things that I'm doing now. But I was able to just kind of redo this over and over again going yes, I've done this correct, or no, I haven't done this correct.

So notice it gets done installing, everything looks fine. I do this, I press O, look at that. It is now autocompleting, everything is set up the way I wanted it. Now obviously, I wouldn't use sed but this is just an example. Yeah, I think there's a question.
>> What is sed again?

Why would we use it?
>> Sed, I'm actually gonna go over sed later in this talk. So effectively, sed, it's a streaming version of ed. If you don't know what ed is, ed is one of the oldest editors known to man. It is a very painful editor to use.

It is the precursor to Vi, and Vi is the precursor to Vim, and Vim's the precursor to Neovim. So there's a lot of iterations that have actually gone on, and I've didn't even mention the editor Stevie, which is actually the true successor to ed. But effectively all it is, it's an ability to read lines and then do something to them and you can mutate the file or pipe everything out to standard out.

And so it's a very useful tool, especially if you're having to do some log searching, manipulation, changing things, anything in large files that you don't have, like a repo round. A lot of people will, hey, I need to jump in here. There's a couple occurrences of something that are in this folder I need to change.

Well, I'm gonna just launch IntelliJ and do a Find and Replace, you can easily do it with sed. It's a very nice tool for whenever you're doing any sort of ad hoc manipulations or you have output from a program. You can either take in, change it and make it into something else.

Sed is a really great tool and I'll kinda go over how to better understand sed, but that'll be a little bit later. So don't worry, if you don't understand sed, it will become very clear in a little bit. I just wanted to kinda get this example up to show you how easy was that for me to install zsh.

Now in one year from now when I get a new computer, I just run, my Ansible script, and I have my command line all ready to rock. It's exactly the way I want it, awesome. It has auto suggestions on it, feels great.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now