Molecule

Snippets

Source

Packages

Check if a package is installed

- name: Check package is installed.
  ansible.builtin.package:
    name: "PACKAGENAME"
    state: present
  register: __verify_package
  failed_when: >
    __verify_package is changed or
    __verify_package is failed

User/Groups

Verify Group exists

- name: Check group exists (Default)
  ansible.builtin.group:
    name: "GROUPNAME"
  check_mode: true
  register: __verify_group
  failed_when: >
    __verify_group is changed or
    __verify_group is failed

- name: Check group exists (full options)
  ansible.builtin.group:
    name: "GROUPNAME"
    gid: "4711"
    state: "present"
    system: "true"
  check_mode: true
  register: __verify_group
  failed_when: >
    __verify_group is changed or
    __verify_group is failed

Verify group is absent

- name: Check group is absent
  ansible.builtin.group:
    name: "ABSENT_GROUP"
    state: "absent"
  check_mode: true
  register: __verify_group
  failed_when: >
    __verify_group is changed or
    __verify_group is failed

Files/Folders

Verify file exists and contains text

- name: Verify file exists and contains a line.
  ansible.builtin.lineinfile:
    name: '/PATH/TO/FILE'
    line: 'SEARCHTEXT'
    state: present
    mode: "0700"
    owner: "root"
    group: "root"
  check_mode: true
  register: __verify_file
  failed_when: >
    __verify_file is changed or
    __verify_file is failed

Verify file exists

- name: Verify file exists.
  ansible.builtin.file:
    name: '/PATH/TO/FILE'
    owner: "root"
    group: "root"
    mode: "0700"
  register: __verify_file
  failed_when: >
    __verify_file is changed or
    __verify_file is failed
# Not checking user, group and mode.
- name: Verify file exists.
  ansible.builtin.stat:
    path: "/path/to/file"
  register: __verify_file
  failed_when: >
    __verify_file is failed or
    not __verify_file.stat.exists

Verify directory exists

- name: Verify directory exists.
  ansible.builtin.file:
    name: '/PATH/TO/DIRECTORY'
    state: directory
    owner: "root"
    group: "root"
    mode: "0700"
  register: __verify_directory
  failed_when: >
    __verify_directory is changed or
    __verify_directory is failed

Verify Binaries

- name: Check binary exists.
  ansible.builtin.command:
    cmd: '/PATH/TO/BINARY'
  register: __verify_binary
  failed_when: not _verify_binary.stat.exists

Services

- name: Check service status.
  ansible.builtin.service:
    name: "SERVICENAME"
    state: started
    enabled: true
  check_mode: true
  register: __verify_service
  failed_when: >
    __verify_service is changed or
    __verify_service is failed

Command execution

Check command executes wo. error

- name: Check command status.
  ansible.builtin.command: "/PATH/TO/BINARY version"
  register: __verify_command
  failed_when: __verify_command.rc != 0

Cron

Verify Cron status

- name: Check cron.
  cron:
    name: "CRONNAME"
    minute: "5"
    hour: "*"
    day: "*"
    weekday: "*"
    month: "*"
    job: "/PATH/TO/CRON/FILE"
  register: __verify_cron
  failed_when: >
    __verify_cron is changed or
    __verify_cron is failed