Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
ansible中的未定义变量_Ansible_Ansible 2.x_Ansible Inventory_Ansible Facts - Fatal编程技术网

ansible中的未定义变量

ansible中的未定义变量,ansible,ansible-2.x,ansible-inventory,ansible-facts,Ansible,Ansible 2.x,Ansible Inventory,Ansible Facts,下面是我的ansible任务,它将获取域名,并将输出注册为项目值,以便我可以在我的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

下面是我的ansible任务,它将获取域名,并将输出注册为项目值,以便我可以在我的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
但它抛出的错误如下:

fatal: [localhost]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'number' is undefined\n\n
任何帮助都将不胜感激。

更改为

when: domain_name == item.key
编辑

domain\u name
更改为
domain\u name。首先定义stdout
和test
server\u path.stat

- 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

问题是您正在使用
和{abc:08,cde:04}
循环,并注册到
服务器路径
。在这种情况下,
server\u path
将包含一个结果数组,该数组将封装所有 调用
stat
的输出。您可以通过调试
server\u path
变量来验证这一点

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

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

上一个是
domain\u name=={{item.key}}
,OP已经编辑了它谢谢你选择鱼。。。但我仍然无法克服我发布的错误,即“dict object”没有属性“stat”\n\n。即使服务器路径存在,它仍然抛出相同的错误。@kenny您是否在任务
检查网页路径
中将
域名
更改为
域名。stdout
?成功了。但在改变剧本逻辑后,它就不起作用了。最好在原始剧本之后再添加新的编辑,否则,当你的问题完全改变时,答案似乎会起作用