当连接到另一个主机时,ansible不会读取变量

时间:2019-01-25 17:07:47

标签: ansible

我有一本剧本,其中读入变量列表:

vars_files:
  - vars/myvariables.yml

 tasks:
  - name: Debug Variable List
    debug:
      msg: "An item: {{item}}"
    with_list: "{{ myvariables }}"

这会从文件variables.yml中打印出“ myvariables”列表,其中包含:

---
myvariables:
  - variable1
  - variable2

我得到了预期的结果。

"msg": "An item: variable1"
"msg": "An item: variable2"

但是,当我连接到另一台主机并运行相同的Debug语句时,它将引发错误:

vars_files:
  - vars/myvariables.yml

 tasks:
  - name: Configure instance(s)
    hosts: launched
    become: True
    remote_user: ubuntu
    port: 22
    gather_facts: False
    tasks:
      - name: Wait for SSH to come up
        delegate_to: ***
        remote_user: ubuntu
        connection: ssh
        register: item
      - name: Debug Variable List
        debug:
          msg: "An item: {{item}}"
        with_list: "{{ myvariables }}"

输出:

 "msg": "'myvariables' is undefined"

连接到不是localhost的另一台主机时,如何定义变量文件?

在此方面的任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

使用“ 主机:已启动”,您可以启动新的剧本。将 vars_files:放入此剧本的范围内(见下文)。

   - name: Configure instance(s)
     hosts: launched
     become: True
     remote_user: ubuntu
     port: 22
     gather_facts: False
     vars_files:
       - vars/myvariables.yml
     tasks:

查看Scoping variables