ansible中的未定义变量

时间:2018-03-19 22:39:00

标签: ansible ansible-2.x ansible-inventory ansible-facts

下面是我的ansible任务,它将获取域名,并将输出注册到项目值,以便我可以在我的剧本中使用该变量。

- 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

但它抛出的错误如下:

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

任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:1)

问题是您正在使用with_dict: {abc: 08, cde: 04}进行循环并注册到server_path。在这种情况下,server_path将包含一个将包装所有内容的结果数组 调用stat的输出。您可以通过调试server_path变量验证这一点。

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

您需要通过数组索引访问结果。示例:server_path.results[0].stat.isdir

答案 1 :(得分:0)

更改为

when: domain_name == item.key

修改

domain_name更改为domain_name.stdout并首先测试server_path.stat is defined

- name: Checking for webpage path
  stat: path=/etc/apps/dc{{item.value}}/webpage.html
  when: domain_name.stdout == item.key
  with_dict: {abc: 08, cde: 04}
  register: server_path

- debug: msg="server path exists"
  when: server_path.stat is defined and server_path.stat.isdir is defined and server_path.stat.isdir