0

I've been searching for quite some time and tried many variants and similar answers without success. Hopefully this is something simple I am missing.

Ansible 2.9.6

I am creating many playbooks that all share a large set of custom Roles for my clients. I want to keep all logic out of the playbooks, and place that logic in the roles themselves to allow maximum re-usability. Basically, I just want to add boilerplate playbooks for simple "Role" runs via tags, with some vars overrides here and there.

My problem is that I can't seem to make some roles idempotent by using conditions - the conditionals don't work. The error I get is:

fatal: [localhost]: FAILED! => {"msg": "The conditional check 'homebrew_base.rc != 0' failed.
The error was: error while evaluating conditional (homebrew_bash.rc != 0): 'homebrew_bash' is undefined

The error appears to be in '/Users/eric/code/client1/provisioning/roles/bash/tasks/main.yaml': line 12, column 3, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:


- name: Change shell (chsh) for macOS to homebrew managed version
  ^ here
"}

Below is the boilerplate code for my playbooks:

# ./playbook.yaml

---
- name: Provisioning
  hosts: localhost
  connection: local

  pre_tasks:
    - include_vars: "{{ item }}"
    with_fileglob:
      - "{{ playbook_dir }}/vars/global.yaml"
  tags: always

tasks:

  - name: Use homebrew bash binary
    include_role:
      name: bash
    tags: bash

The above is truncated quite a bit, but only thing missing are additional var files and a whole bunch of include_roles.

Below is my role file in the entirely though. They are largely untested because of the error I keep getting.

# ./roles/bash/tasks/main.yaml

---
- name: Check /etc/shells contains "/usr/local/bin/bash"
  command: grep -Fxq "/usr/local/bin/bash" /etc/shells
  register: homebrew_bash
  ignore_errors: True
  changed_when:
    - homebrew_bash.rc != 0

- name: Check that homebrew installed /usr/local/bin/bash
  stat:
    path: /usr/local/bin/bash
  register: homebrew_bash_binary

- name: Change shell (chsh) for macOS to homebrew managed versoin
  tags: bash, chsh
  shell: chsh -s /usr/local/bin/bash
  become: yes
  when:
    - homebrew_bash.rc != 0
    - homebrew_bash_binary.stat.exists = True

Ps: I do plan on abstracting those hardcoded paths into roles/bash/defaults/. But I need it working first before that.

Ps2: If there is a better way to use a contains filter (instead of the grep hack), I'm all ears.

I've tried:

  • making separate tasks/chsh.yaml, and using the include: command to call that task within the role. When I do this, I get an odd error telling me the variable is undefined - in the tasks/chsh.yaml - even though I am checking for the variable in tasks/main.yaml! That doesn't seem right.
  • using quotes in various places in the conditions
  • commenting out each condition: both give the same error, just differenet names.

Again, I am trying to keep this logic in the roles only - not in the playbook.

Thanks!

eduncan911
  • 17,165
  • 13
  • 68
  • 104

1 Answers1

0

Figured it out. I was missing the "tags" on the conditionals!

# ./roles/bash/tasks/main.yaml

---
- name: Check /etc/shells contains "/usr/local/bin/bash"
  tags: bash, chsh
  command: grep -Fxq "/usr/local/bin/bash" /etc/shells
  register: homebrew_bash
  ignore_errors: True
  changed_when:
    - homebrew_bash.rc != 0

- name: Check that homebrew installed /usr/local/bin/bash
  tags: bash, chsh
  stat:
    path: /usr/local/bin/bash
  register: homebrew_bash_binary

- name: Change shell (chsh) for macOS to homebrew managed versoin
  tags: bash, chsh
  shell: chsh -s /usr/local/bin/bash
  become: yes
  when:
    - homebrew_bash.rc != 0
    - homebrew_bash_binary.stat.exists = True
eduncan911
  • 17,165
  • 13
  • 68
  • 104