首页 > 解决方案 > 变量已定义但仍出现未定义错误

问题描述

我正在尝试编写一个在运行该剧本的机器上完成其一些任务的剧本。我知道我可以为此使用 local_action ,但我只是在测试剧本现在是否有效。最终我需要使用delegate_to。我已经定义了变量并且我正在使用 delegate_to: 'variable name' 但我收到了这个错误。: " fatal [target node]: FAILED! => { msg": "'variablename' 未定义。下面是我的剧本:

name: Create certs
gather_facts: true
vars:
  master: "{{ nameofhost }}"

tasks:
 - name: Run command
   shell: Command to run
   delegate_to: "{{ master }}"

标签: ansible

解决方案


您需要将您的游戏定位到库存的目标主机

name: Create certs
gather_facts: true
hosts: target_hosts
vars:
  master: "{{ nameofhost }}"
tasks:
 - name: Run command
   shell: Command to run
   delegate_to: "{{ master }}"

``` 

Your inventory files may look like that:

    [target_hosts]
    master ansible_host=your_master_dns_or_ip


And then ansible can target that inventory group and then reduce the task scope to master host. Or you can just use the localhost as target.

推荐阅读