B
B
Benedictus2021-06-30 00:35:47
Ansible
Benedictus, 2021-06-30 00:35:47

How to fix The task includes an option with an undefined variable error in Ansible?

Good afternoon.

Help a newbie to understand Ansible. There is this simple playbook found on the internet:
---
- hosts: test
tasks:

- name: Debug
debug:
msg={{ ansible_os_family }}

- set_fact: package_name=httpd
when: ansible_os_family == "Redhat"

- set_fact: package_name =apache2
when: ansible_os_family == "Debian"

- name: Install httpd package
yum: name={{ package_name }} state=latest
when: ansible_os_family == "Redhat"

- name: Debug1
debug: msg={{ package_name }}

- name: Install apache2 package
apt: name={{ package_name }} state=latest
when: ansible_os_family == "Debian"

which fails with an error:

PLAY [test] *********************************** ******************************************************* ******************************************************* ************

TASK [Gathering Facts] ********************************* ******************************************************* ******************************************************* *
ok: [192.168.0.76]
ok: [192.168.0.79]

TASK [Debug] ******************************* ******************************************************* ******************************************************* *************
ok: [192.168.0.79] => {
"msg": "RedHat"
}
ok: [192.168.0.76] => {
"msg": "redhat"
}

TASK [set_fact] ********************************************** ******************************************************* *********************************************
skipping: [192.168. 0.79]
skipping: [192.168.0.76]

TASK [set_fact] **************************************** ******************************************************* ******************************************************* ****
skipping: [192.168.0.79]
skipping: [192.168.0.76]

TASK [Install httpd package] ************************** ******************************************************* ******************************************************* **
skipping: [192.168.0.79]
skipping: [192.168.0.76]

TASK [Debug1] ********************************************** ******************************************************* ****************************************************
fatal: [ 192.168.0.79]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'package_name' is undefined\n\nThe error appears to be in '/etc/ansible/playbooks/install_apache.yml': line 19, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - name: Debug1\n ^ here\n"}
fatal: [192.168.0.76]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'package_name' is undefined\n\nThe error appears to be in '/etc/ansible/playbooks/install_apache.yml': line 19, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - name: Debug1\n ^ here\n"}

PLAY RECAP ******************************************************* ******************************************************* ***************************************************
192.168.0.76 : ok=2 changed=0 unreachable=0 failed=1 skipped=3 rescued=0 ignored=0
192.168.0.79 : ok=2 changed=0 unreachable=0 failed=1 skipped=3 rescued=0 ignored=0

I believe that there is a problem in the syntax, but I just can not figure out what needs to be fixed ...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
MaxKozlov, 2021-06-30
@Benedictus

Redhat and RedHat are two big differences :)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question