ansible中的未定义变量
下面是我的ansible任务,它将获取域名,并将输出注册为项目值,以便我可以在我的playbook中使用该变量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
- 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
和testserver\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
?成功了。但在改变剧本逻辑后,它就不起作用了。最好在原始剧本之后再添加新的编辑,否则,当你的问题完全改变时,答案似乎会起作用