0

I am using the ansible lineinfile module to replace few lines in file. The two variables "apache_status" & " collector_status" are passed as extra vars when running the playbook. However, when i dont pass collector_status , the second task is getting failed by throwing undefined variable error. I want ansible to skip the task when variable defined in task is not added in extra vars. Any help is appreciated.

Command: ansible-playbook playbook_name --extra-vars "apache_status=TRUE"

- name: Enable/Disable Apache  service
  lineinfile: 
    path: /apache/properities
    regexp: '^active='
    line: active={{ apache_status }}
- name: Enable/Disable collector 
  lineinfile:
    path: /apache/properities
    regexp: '^collector='
    line: collector={{ collector_status }}
"msg": "The task includes an option with an undefined variable. The error was: 'collector_status' is undefined"
learning fun
  • 519
  • 1
  • 5
  • 12

1 Answers1

0

Try this:

- name: Enable/Disable Apache  service
  lineinfile: 
    path: /apache/properities
    regexp: '^active='
    line: active={{ apache_status }}
- name: Enable/Disable collector 
  lineinfile:
    path: /apache/properities
    regexp: '^collector='
    line: collector={{ collector_status }}
  when: collector_status is defined
Matt P
  • 2,452
  • 1
  • 12
  • 15