Check out a free preview of the full Developer Productivity, v2 course
The "Convert Ansible Script to Bash" Lesson is part of the full, Developer Productivity, v2 course featured in this preview video. Here's what you'd learn in this lesson:
ThePrimeagen demonstrates how to update a bash script to be identical to an Ansible script. He shows how to simplify the script by deleting unnecessary lines, cloning a branch, installing dependencies, changing directories, and running commands. ThePrimeagen also discusses the benefits of using bash scripts for software installation and customization.
Transcript from the "Convert Ansible Script to Bash" Lesson
[00:00:00]
>> ThePrimeagen: Let's kind of redo what we did with ansible, except for we're gonna do it with our little bash script right here. So what we're gonna do here is we're gonna update our neovim scripts to be identical to our ansible script. So to do that, I'm gonna jump back to the MF ansible thing.
[00:00:18]
I'm just gonna grab this whole. This thing really quickly jump back here and I'm just gonna rep, I'm just gonna paste this all in. 'cause I want you to see the difference. I want you to feel it. I want you to know what it felt in your soul to do this.
[00:00:30]
So what I'm gonna do is I'm gonna delete all these lines. Bam. Five lines gone. I'm gonna go, get,clone branch. v0.10.1 that just clones out that branch. And I'm gonna wanna put that in home personal neovim. There we go. Delete that. Look at that. Look at how simplified that, I mean, for me, this is just like why again, I just like the bash approach.
[00:00:55]
I just get it. I can look at it. I feel this. I do it every day, right? All right, we need some sudo make installs. So I'm gonna go sudo or app install do that bring that up here and then just throw those for them again feeling so good, right?
[00:01:11]
Like that's just feeling darn good. We're gonna change into that directory now. We have to go cd Home, personal, and neovim. And then, by the way, always use things like home. You don't have to use, because if you use like the full qualified path it can be, let's see, what is it, XF?
[00:01:33]
There we go. If you change systems and you don't have the same name life sucks. So just use. Use those, it just makes life a little bit easier. All right, and then params and if I'm not mistaken, it's just, I believe that is all that is, and then we just have to do a little pseudo-make install.
[00:01:55]
Alright. And so now I am now Stalling Neo Vim in five lines instead of, 44 lines Ansible. So configuration over convention. Which one's better? One's more messy, more error prone. I'd say the other one is just more like you have to look up every detail to get things right.
[00:02:14]
Tapper.
>> Speaker 2: Was that expansion shortcut a neo vim thing or.
>> ThePrimeagen: Yeah. If you do Ctrl+X, Ctrl+F, it will expand any path based on where you're at, and it will let you kind of look at it. So if I go like this, it'll say, okay, I have these two directories.
[00:02:31]
And what's in here, I have these two and so it's kind of like already built in little file under standard. It's already there. It's a fun thing, you know? Neovim privilege, you wouldn't understand that's great. Actually, you might understand, I'm not sure [LAUGH] All right, you're learning, we're learning.
[00:02:46]
So now, if I were to actually run this thing right here, you would notice right away, hey, look at that. Come on, let me go like this RM, RF, personal neovim. Let me just destroy neovim really quickly and so now I can run this. You can see it clone right in.
[00:03:02]
It goes through. It's going to clone in just that single branch, which is fantastic. It takes a little bit. There's a lot of, people have been developing on it for a while. There's a lot of stuff in there. A lot of history, a lot of commits, a lot of features.
[00:03:17]
Hey, we didn't need all the views this time You didn't need all the what? The v's. There's no v's. Notice that you recognize all this output. So when something goes wrong, it feels really easy to fix, which is also really nice. You live and you learn as time goes on.
[00:03:30]
And I think I've just kind of ended in this really kind of basic approach to life where I just don't want to have to think about things anymore. If that makes sense, right? I don't want, I just wanted to build it the way I want. I don't want to have to go look up everyone else's software to do something that's relatively simple, like installing a bunch of things you need is actually a relatively simple process, and we effectively have a decently extensible version in 44 lines.
[00:03:55]
And it's not like I was trying that hard in writing this. This is just okay, script. I'm sure that a bunch of bash people were like, actually, they were very upset at some of the things I did. if you don't get your password wrong once, there you go.
[00:04:11]
And then you can even see this. Look at this, you can see that, okay, look at that, we're building. Okay, this looks good, we got 200% like 18 times. You can watch it happen. I like watching things happen makes me feel better on the inside. I don't feel freaked out that it's just stalled to doing who knows what.
[00:04:24]
And so there we go. This looks fantastic. And so now we have some executable code. What do we do in this one? Look at that, I didn't even do the little -b right here. I did the whole fetch and checkout business. So you can even make it more verbose than you need to make it.
[00:04:36]
And just like that, that's why I like Bash. I used Ansible for years. I'm not using it anymore. There you go. We're moving on. We're moving on with life. And that's just that. The little neovim building thing did actually end up building and everything looks fantastic. So if I now run neovim, you'll notice it's at 10.1.
[00:04:54]
Bada bing, bada boom. Five lines. Looks everything looks great. All right, someone, I did see someone ask, is all of that just? To build neovim? Well, no, it's to build anything, right? Because now I can have neovim. It's, I can have whatever I want. If we were to re execute any of those things, we can have all that.
[00:05:11]
I can hit run with just a foo so that way we skip over neovim run the foo bar, fantastic. And it just allows for all that. And so there you go, step one of where I think my environment's better than yours. Sorry, I'm sure yours is also very nice, but I like mine better.
[00:05:29]
Okay, so all we've talked about is just getting software onto your machine or setting it up into a state in which you want. But we haven't talked about dot files, the configurations, the customizations, right? The good stuff, the gold of your experience, right here, right? Well, for those are unfamiliar with the term dot files, we're talking about like your bash RC, your zish RC, your vim RC, all these are examples of dot files.
[00:05:50]
They typically are ran before the program that you're running setup is complete, and so they run at some point within the startup lifecycle. I can't tell you where, but they run at some point, right? Like your VimRC or Anit.Lua runs after some hydration of the environment has happened.
[00:06:07]
And then they'll run your script cuz obviously like the Lua engine needs to be ready to be able to run Lua. So it's just like at some point within the startup, it runs. All right, we got a question already.
>> Speaker 3: Just one thing I did like about the Ansible approach was having the SSH key ready to go for cloning private repos.
[00:06:26]
>> ThePrimeagen: So, UNO reverse, you can still use like, so one thing we could do, remember how I had that whole like, pip install Bonanzas going on here. We could have in here, Ansible. We could have inside of here, some sort of Pip3 install ansible or however you want to install it.
[00:06:43]
That way you have your vault and then you can clone down your place with your keys. You can ansible vault it all out. Type in your little password because it will prompt you right then and there for your password so you don't have to do it at the very, very top of your program hoping you didn't get it wrong until you get all the way down to the bottom where you need it.
[00:06:58]
And so you'll have that nice little ansible Ansible approach, and you can do it with multiple, and you can have different keys for different ones, right? There's like a whole bunch of reasons why it's actually better this way, and then you can just have it right then and there.
[00:07:08]
So you can still use Ansible Vault and this approach. Nothing says they're somehow mutually exclusive. You just don't have to use Ansible to install everything. In fact, you can even use Ansible and this approach, right? Nothing says you can do it all. This is kind of a do it all, do your own adventure kind of approach here.
[00:07:24]
It's just a really dumb script runner. And I just simply use it with the convention of this starts my machine ready to be production ready. That's it. And so it's a pretty simple approach. Yes?
>> Speaker 3: Your approach for running this kind of script setup on like a server, would you do that on a server versus your own personal setup?
[00:07:47]
>> ThePrimeagen: So I'm building this just for just for myself. If it was a server, you'd probably want to use something that's a little bit more production grade, right? No one's relying on, like when you have to set up a new computer, people usually have the expectation of a day.
[00:08:01]
If you're ready in five minutes, that's fantastic. If you're ready in thirty minutes, that's fantastic, right? That's good and so I don't I'm not considering this like a server experience. The only thing I said is that with Ansible, this it's supposed it's designed to work with servers. That's why it has an inventory, a list of nodes that you're actually working with in executing these commands remotely as long as you have, the proper authority on that computer.
[00:08:22]
So for me, I just went really hard, and wow, I have a server, right? And that's all that it was. And so that's why we are, that's why I wouldn't recommend this for your company, cuz that'd be crazy, right? That would probably be a I mean, maybe it would work, I don't know, but I just don't think I'd wanna live that life.
[00:08:37]
Maybe I would wanna live that life, I'd have to see the situation before. Four, I'd say yes or no to it.
>> Speaker 3: The senior engineer answer. It depends.
>> ThePrimeagen: It depends, and you're doing it wrong. And I also think the bike shed should be blue.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops