1

I would like add my hostnames with external files.

I have used in my playbook master vars_files to add my hostnames, and it doesn't seem to be working.

I need to add the hostnames in external files from my ansible's project.
I cannot used hosts files and group, because I use Ansible Tower (3.2.2) and it have already his own inventory.

Please note I can't, for some reasons, add my vars_files in my Ansible's project.

I Have already tried to add the absolute path.
EDIT: Note the error message below is only via Ansible Tower execution.
With command line, it is working fine.

---

- hosts:  "{{ hostnames }}"

# include vars from file
  vars_files:
    # this line doesn't work
    - /project/ansible/home/ansible/ansible_scripts/vars/dev.yml
      # this line doesn't work
    # - /project/ansible/home/ansible/ansible_scripts/vars/dev.yml
      # this line doesn't work
    # - ../../../ansible/home/ansible/ansible_scripts/vars/dev.yml
    # this line works fine
    # - vars/dev.yml

# play roles
  roles: 
    - check_info_servers

The error is:

ERROR! vars file project/ansible/home/ansible/ansible_scripts/vars/dev.yml was not found on the Ansible Controller.
If you are using a module and expect the file to exist on the remote, see the remote_src option
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
rab boubou
  • 11
  • 6

1 Answers1

0

To "add hostnames from external files" you might want use the add_host module.

For example:

> cat /scratch/dev.yml
my_hosts:
  - host1.example.com
  - host2.example.com
  - host3.example.com


> cat test.yaml
- hosts: localhost
  vars_files:
    - /scratch/dev.yml
  tasks:
    - add_host:
        name: "{{ item }}"
      loop: "{{ my_hosts }}"
    - debug: msg="{{ item }}"
      with_inventory_hostnames:
        - all


> ansible-playbook test.yaml | grep msg
    "msg": "host3.example.com"
    "msg": "host1.example.com"
    "msg": "host2.example.com"
Vladimir Botka
  • 58,131
  • 4
  • 32
  • 63
  • Same issue with ansible Tower, but add_hosts module works fine with command line. Maybe it's not possible to add variables from external project via ansible Tower ... – rab boubou Dec 28 '18 at 15:28