fuzzi
fuzzi

Reputation: 2277

ansible does not read in variables when connected to another host

I have a playbook which reads in a list of variables:

vars_files:
  - vars/myvariables.yml

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

This prints out the list of "myvariables" from a file variables.yml, which contains:

---
myvariables:
  - variable1
  - variable2

I get the following as expected.

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

However, when I connect to another host, and run the same Debug statement, it throws an error:

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 }}"

OUTPUT:

 "msg": "'myvariables' is undefined"

How do I define the variables file when connecting to another host that is not localhost?

Any help on this would be greatly appreciated.

Upvotes: 0

Views: 354

Answers (1)

Vladimir Botka
Vladimir Botka

Reputation: 67959

With "hosts: launched" you started new playbook. Put the vars_files: into the scope of this playbook (see below).

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

Review the Scoping variables.

Upvotes: 1

Related Questions