4

I am writing ansible playbook, and set the below variable :

default_tz: 'ZONE="America/Los_Angeles"'

I am trying to add the above variable to a file, by using the below code On the same playbook:

- name: configure time zone
  lineinfile:
   line: "{{ default_tz }}"
   dest: "/etc/sysconfig/clock"

I am getting the below error while running the playbook:

"The task includes an option with an undefined variable. The error was: 'default_tz' is undefined\
4
  • 1
    How exactly have you set the variable (i.e. in a vars block, with set_fact module, some where in the inventory)? Can you post the actual code you have used to set the variable relative to the task that is called it? Commented Feb 6, 2019 at 15:17
  • i set the variable in vars block Commented Feb 6, 2019 at 16:10
  • 1
    Well without any further code, there is nothing obviously wrong with what you are doing. So if this is all in the same playbook, then the most likely issue is that you have more than one play in the playbook and are defining the variable in one, and calling it in another which won't work. Hard to know tho without seeing all the relevant code. Commented Feb 6, 2019 at 17:33
  • please post the whole playbook with vars mentioned. Commented Feb 12, 2019 at 9:28

1 Answer 1

3

You should declare variable in the scope of variables. Like this:

vars: default_tz: 'ZONE="America/Los_Angeles"'

https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html#defining-variables-in-a-playbook

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.