Declarative Installation (Recommended)

For a permanent system-wide installation, add Ansible to your NixOS configuration:

1. Edit your NixOS configuration:

``bash

sudo nano /etc/nixos/configuration.nix

`

2. Add Ansible to the system packages:

`nix

environment.systemPackages = with pkgs; [

ansible

ansible-lint

];

`

3. Rebuild your system:

`bash

sudo nixos-rebuild switch

`

Imperative Installation (Quick)

For a quick user-level installation:

Introduction

NixOS is a popular choice for server and desktop environments, and combining it with Ansible enables powerful automation capabilities. This guide walks you through installing Ansible on NixOS, from updating your system to verifying the installation and running your first Ansible command.

Prerequisites

Before you begin, ensure that you have:

  • Access to a NixOS system with root or sudo privileges.
  • An active internet connection to download necessary packages.
  • Python 3.12 or later installed (included by default in NixOS).

Step-by-Step Installation

1. Connect to Your Server Initiate an SSH connection from your terminal:

`bash

ssh [email protected]

`

2. Switch to Root User For installing system-wide software:

`bash

sudo su

`

3. Update System Packages Before installing any new software, update your system:

`bash

nix-channel --update && nixos-rebuild switch

`

Confirm any prompts to ensure your system has the latest updates.

4. Check Available Ansible Packages Verify which Ansible packages are available:

`bash

nix-env -qaA nixpkgs.ansible

`

5. Install Ansible Using the nix package manager, install Ansible:

`bash

nix-env -iA nixpkgs.ansible

`

This command installs Ansible along with its dependencies. Confirm the installation when prompted.

6. Verify the Installation Once installation is complete, check the installed version:

`bash

ansible --version

`

This command displays the version of Ansible and configuration details, confirming a successful installation.

Configuration and First Steps

1. Configure Ansible Ansible configurations can be adjusted in the ansible.cfg file located in /etc/ansible/. Customize settings like default inventory file, privilege escalation settings, and more.

2. Edit the Inventory File Ansible uses an inventory file to track managed servers:

`ini

[local]

localhost ansible_connection=local

[webservers]

web1.example.com

web2.example.com

`

3. Test Ansible Connectivity Ensure that Ansible can communicate with your hosts:

`bash

ansible all -m ping

`

Expected output:

`bash

localhost | SUCCESS => {

"ansible_facts": {

"discovered_interpreter_python": "/usr/bin/python3"

},

"changed": false,

"ping": "pong"

}

``

Conclusion

With Ansible installed on NixOS, you are now ready to automate your infrastructure. Whether managing configurations, deploying applications, or automating daily tasks, Ans