crNh
crNh

Reputation: 45

Ansible playbook conditionals

I'm trying to put together playbook that works for both Windows and Linux. Now I'm trying to include roles in playbook that will be taken only if Windows or Linux, but it always complains about syntax. I'd appreciate any help on this as I've tried few different approaches and it always failed.

---
- hosts: all
  gather_facts: no
  pre_tasks:
    - name: (localhost) make sure the known_hosts file is cleared
      lineinfile:
        path: ~/.ssh/known_hosts
        state: absent
        regexp: "^{{ ansible_host | replace('.', '\\.') }}.*$"
      delegate_to: 127.0.0.1

- hosts: all
  serial: 1
  pre_tasks:
    - name: (debug) print out some debug message to confirm inventory is properly set up
      debug:
        msg: "System inventory_hostname:{{ inventory_hostname }} ansible_host:{{ ansible_host }}"

- hosts: all
  tasks:
  - name: Install CA Trust Certs Windows
    include_tasks: tasks\install-certs-windows.yml
    when: ansible_os_family == 'Windows'

  - name: Install CA Trust Certs Linux
    include_tasks: tasks/install-certs-linux.yml
    when: ansible_os_family != 'Windows'

  roles:
  - { role: ansible-role-runnersbasics, tags: ["basics"] }
  - { role: ansible-role-docker, tags: ["docker"] }
  - { role: ansible-role-gitlab-runner }
    when: ansible_os_family == 'Windows'

Error:

ERROR! We were unable to read either as JSON nor YAML, these are the errors we got from each:
JSON: Expecting value: line 1 column 1 (char 0)

Syntax Error while loading YAML.
  did not find expected key

The error appears to be in 'playbook.yml': line 33, column 5, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

  - { role: ansible-role-gitlab-runner }
    when: ansible_os_family == 'Windows'
    ^ here

When removing curly braces from roles and moving when on the same level as roles

ERROR! We were unable to read either as JSON nor YAML, these are the errors we got from each:
JSON: Expecting value: line 1 column 1 (char 0)

Syntax Error while loading YAML.
  mapping values are not allowed in this context

The error appears to be in 'playbook.yml': line 30, column 43, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

  roles:
    role: ansible-role-runnersbasics, tags: ["basics"] }
                                          ^ here

Upvotes: 0

Views: 305

Answers (2)

crNh
crNh

Reputation: 45

Resolved by using method that @Khaled provided (thanks again!)

  roles:
    - role: ansible-role-runnersbasics
      tags: ["basics"]
      when: ansible_os_family == 'Windows'

Upvotes: 1

Jack
Jack

Reputation: 6158

In a word: DON'T

Alright, that's two words if you want to get picky about contractions.

Anyway.... Group your servers by OS, then work on them separately:

---
- hosts: Linux
  gather_facts: no
# Do Linux stuff

- hosts: Windoze
  serial: 1
# Do Windoze stuff

Upvotes: 1

Related Questions