EN VI

Linux - ansible + how to to run import_playbook with condition?

2024-03-15 09:00:07
Linux - ansible + how to to run import_playbook with condition

is it possible to run import_playbook with condition?

here is example from my ansible code

we want to run run_tasks_only_on_rhel8.yml from import_playbook when condition is only for rhel8 machines

- import_playbook: "{{playbook_folder}}/run_tasks_only_on_rhel8.yml"
  when: ansible_facts['distribution_major_version'] == "8"

but from ansible playbook output we see the following errors

 The conditional check 'ansible_facts['distribution_major_version'] == "8"' failed. The error was: error while evaluating conditional (ansible_facts['distribution_major_version'] == "8"): 'dict object' has no attribute 'distribution_major_version'

what need to correct here ?

Solution:

You need to check, if you disabled the facts in your playbook, as you could see below, this basic code works without issues:

- hosts: localhost
  connection: local
  tasks:
  - debug:
      var: ansible_facts['distribution_major_version']
Answer

Login


Forgot Your Password?

Create Account


Lost your password? Please enter your email address. You will receive a link to create a new password.

Reset Password

Back to login