r/ansible • u/oz1sej • Oct 28 '26
playbooks, roles and collections Why not just call a bash script?
One thing I still don't think I've understood with ansible is: Why all this new syntax for installing stuff, why not just write an install script in bash and then call it with ansible? Sorry if this is a stupid question...
42
Upvotes
31
u/Horace-Harkness Oct 28 '26
Abstraction. To install a package with Ansible is like 3 lines of yaml. To do it in shell on RHEL and Debian and Arch with full error handling would be a lot of shell code.
20
u/anarchyusa Oct 28 '26 edited Oct 29 '26
With pre-built collections, the script is already written. This is what’s known as “imperative” opps, i.e. DO THE THING. Ansible tasks are (or should be because it’s not necessarily automatic) written in a “declarative” fashion, i.e. DO WHATEVER IS NECESSARY TO MAKE THE STATE THUS”. E.g. not “start service X”, rather, “the state of service X should be started”. In the former, you have to write the state detection and state management (on, off, delayed, etc). In the latter, you just say what you want the state to be.
5
u/planeturban Oct 28 '26
Sure you can do that. But how should you handle upgrades? Ansibles package/dnf/deb module gives you that automatically.
125
u/SagaciousZed Oct 28 '26
It's primarily one of safety and idempotency. Running a ansible playbook should result in no work being done on subsequent runs, such the system state matches what is decided in the playbook. If your shell script does all that you now have extra code to maintain to make sure to always works. Bullet proof shell scripts are also difficult to write. Would you rather just maintaining a yaml file with your list of dependencies or manage multiple complex shellscrips?
14
u/mehx9 Oct 28 '26
Whatever you do, just don’t write bash in ansible.
Like others have said: abstraction.
If you program in Python also it’s dead easy to write extensions and call them from yaml.
5
u/SnooJokes4504 Oct 28 '26
The two biggest wins for Ansible over a bash script is the ability to split out config on a per host/group basis in a straightforward way, and a centralised inventory. That said, if there was a particularly complicated task (eg anything that uses anything more elaborate than a list/array) then I'd always almost exclusively use python or bash.
5
u/N7Valor Oct 28 '26
How do you make it "idempotent" with Bash? Basically the idea that if nothing needs to be changed on a system, then change nothing. If say you try to download a file, sometimes it might throw an error if it already exists. Typically you would need to write a lot of wrapping "if/then" statements.
When you use Ansible native modules, they are more or less idempotent by default. So you can run hundreds of tasks and, assuming it's written correctly, nothing will change on the system and nothing should throw an error.
Ansible Lockdown is a good example of something that performs hundreds of tasks (to apply STIG hardening to RHEL as an example) and should ideally change nothing.
5
u/Proper-Attempt4337 Oct 28 '26
One thing I've found myself doing in the past year is taking a number of legacy bash scripts in my homelab and converting them or folding them into Ansible Playbooks with dynamic inventory. I maintain both the playbooks and standalone bash variants, but for any task where I have a number of targets Ansible is usually the more efficient and reliable way to operate at scale. Even if the scale is only 2 or 3 machines.
4
u/flodabo Oct 28 '26
Becaue with bash you need lot of things yourself that ansible provides. e.g. Bash is not made to do things in an idempotent way by default.
The high level goals of Ansible are described here https://docs.ansible.com/projects/ansible/latest/getting_started/introduction.html
You can of course run plain shell, raw python, whatever else stuff wrapped in ansible using command, shell or similar modules. However you then have to make sure you write proper error handling, make it idempotent, etc. That are things that calling an ansible module mostly solves for you already.
4
u/amarao_san Oct 28 '26
From a decade of use of Ansible.
The great thing about Ansible is that it articulates side effects. > in bash is casual and without proper error handling. file/content is very well articulated.
Ansible falls short in the decision part, but side effects are articulated with excellence.
4
u/iq-0 Oct 28 '26
That’s not why you use Ansible. Ansible is a procedure tool. A well written Ansible playbook should read like a succinctly written procedure that you could perform by hand (minus the templating, but even that should ideally also be human interpretable)
Yes, you can write loops and complex logic, do all kinds of convoluted meta-programming and such. But for our operations teams we try to limit our main playbooks to just simple (but strict) procedures. The technical complexity is abstracted away in roles and a few custom python modules.
3
u/Ill-Ad5760 Oct 28 '26 edited Oct 28 '26
Ich hab früher ( ansible 1.6 - 2.x) immer ein Shebang vorn in die 1. Zeile geschrieben und damit das playbook ausführbar gemacht. Dann konnte ich es wie ein Shellscript aufrufen.
4
u/TrueInferno Oct 28 '26
It's actually a really, really good question because it gets to the heart of how Ansible works and why it's good. Don't feel bad at all!
The thing is? At it's lowest level- to my knowledge- a module is just a Python script (or on Windows machines PowerShell I think) that does certain things based on the input it gets from you and the facts from the host it's working on. Each task consists of Ansible creating a new SSH connection, running the module script, and returning the result.
In that sense, it's not a replacement for writing scripts and running them on machines, but rather an orchestration to run lots of scripts. However, a lot of the tasks that Ansible was made to do are so standardized, it provides those scripts for you as pre-made modules. Not only that, but they are maintained and updated and checked for errors. Then you have all the community collection modules, the manufacturer provided/maintained ones, not to mention all the roles available on Ansible Galaxy and elsewhere!
That means you can usually take a step back and focus on what you want done rather than how you want done- things are now declarative vs imperative. Some modules take it a step further- for example, rather than calling ansible.builtin.dnf or ansible.builtin.apt, you can call ansible.builtin.package which takes the input you give it and looks at the facts from the host to determine which update module to actually use- if it's running on a RHEL machine it'll use dnf, while on Debian etc. it'd use apt.
Not all modules have something like that- for example, there's no overarching module for managing both podman and docker, because a system can have both- but you can use the when keyword to have specific steps only run when applicable, among many other helpful tools.
Going back to the whole "maintained and updated" thing- take that previous statement about the when keyword. A script or playbook you write can be as simple as checking whether it is in the podman server group, since you know and maintain this environment, but a provided module/role can't count on that being there, so it might actually have a very complex check involving looking at package facts.
All of that helps make it so that with Ansible, your job isn't to tell it how to do it, you just tell it what you want done and when and it gets done, without you worrying about all the details about how.
2
u/Live_Surround5198 Oct 28 '26
Ansible was released 14 years ago. The fact that it seems “new” to you and your multiple responses that are getting downvoted suggests you aren’t really familiar with the concepts of automation, idempotent code, portable code, and repeatable code in the modern context.
One can write an Ansible playbook to install a specific package across 3 different Linux os types with a half dozen simple lines of .yaml. The corresponding Bash to do exactly the same with full idempotence would be hundreds of lines.
1
u/emptyDir Oct 28 '26
I genuinely think the best way to answer this question would be to just try writing the bash script. I don't think it would take you too long to find is limitations, and you'd get to experience first hand some of the problems that ansible was designed to solve.
Don't listen to me, though. I apparently only learn things the hard way.
1
u/RewardAgitated5520 Oct 28 '26
On top of that different OS versions have different bash versions. Now imagine a bash4 script trying to do some stuff on a host with bash3 only... In general, idempotency is the most important benefit of configuration management - ensure it works and does only the changes it needs.
1
u/serverhorror Oct 28 '26
Because these scripts are already embedded in Ansible.
You don't have to write all the code to make it idempotent.
1
u/Burgergold Oct 28 '26
Existing module managing idempotency, check mode and native inventory
Its also easy to run remotely on linux and even windows
1
u/ipub Oct 28 '26
Bash: Do these steps. Install Nginx, edit the config, start the service. Ansible: Make sure Nginx is installed, the config is correct, and the service is running. Ansible checks the current state and only makes changes when needed. That's idempotency.
1
u/kY2iB3yH0mN8wI2h Oct 29 '26
Mohahaha
1
u/Forward_Hawk 29d ago
ansible good when you handle multiple server and of course idempotentcy is important
1
u/Zesty_IT 26d ago
little late to the party but my two cents using both quite a bit.
Bash is the fast solution to do something, you can call a script with the shell module but that is a quick fix and not idempotent.
Ansible is the tool of choice because if you write a role/playbook correctly, you are describing how you want a group of machines (or one machine) to look. It won't do any additional work or break anything if you set it up properly.