Kitaab

Ansible + Nix 101

tool ops tip nix

published 2023-07-06 11:24

updated 2023-07-07 14:02

101

https://github.com/WeAreWizards/blog/blob/master/content/articles/ansible-and-nix.md https://www.middlewareinventory.com/blog/ansible-changed_when-and-failed_when-examples/ https://techsemicolon.github.io/blog/2019/07/07/ansible-everything-you-need-to-know-about-set-facts/

Since I use Nix to configure everything it's rare that I get a chance to play with other orchestration tools. Since these tools are very rarely used in combination with Nix, running them from NixOS can serve to be challenging. I'll show you some steps I took to get ansible working (non-reproducibly ugh) in Nix, and a basic rundown of how it works

Debug Printing

{{{yaml tasks:

- name: Show result
        debug:
            msg: "{{ result.stdout_lines }}"

}}}

Dumping Facts

{{{yaml tasks:

  • name: Facts setup:
  • name: Dump delegate_to: localhost run_once: true copy: content: "{{ hostvars[inventory_hostname] | to_nice_json }}" dest: /tmp/setup-dump.json }}}

Using Tags

Add a tag to the specific task you want to run {{{yaml

- name: d_ocean | db | test for existing mongodb
  ansible.builtin.command: doctl db list -o json
  changed_when: false
  failed_when: false
  register: db_check
  tags:
    - test

}}}

Then call the ansible playbook with --tags "test" included as a parameter passed to the binary