undefined variable in ansible

kenny picture kenny · Mar 19, 2018 · Viewed 11k times · Source

Below is the my ansible tasks which will fetch domain name and which will register the output to item value so that i can use the variable across my playbook.

- name: Fetching the domain name
  shell: dnsdomainname | cut -d "." -f 1
  register: domain_name

- debug: 
     msg: "DC detected {{domain_name}}"
  when: domain_name.stdout == item.key
  with_dict: {abc: 01, cde: 05}
  register: number == item.value

But it was throwing the error as below:

fatal: [localhost]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'number' is undefined\n\n

Any help will be appreciated .

Answer

yamenk picture yamenk · Mar 20, 2018

The problem is that the you are looping using with_dict: {abc: 08, cde: 04} and registering into server_path. In this case server_path will contain a results array that will wrap all the output of the calls to stat. You can verify this by debugging the server_path variable.

- debug: msg="{{server_path}}"

You need to access the result via an array index. Example: server_path.results[0].stat.isdir