1

I have two VMs one as ansible control machine and running playbook to perform action on second vm.

Following are files on ansible control machine

/etc/ansible/ansible.cfg

inventory      = /etc/ansible/hosts

/etc/ansible/hosts

# My nodes
[my-group]
my1-node ansible_user=imran DB_SERVER=10.136.10.49 DB_USER=mydbusr

I tried following two ways to access variables but no success, but if i provide with command line like -e DB_SERVER=10.136.10.49. its working

myplaybook

---
- hosts: all
  tasks:
    - debug: 
        msg:
          - "Database server {{DB_SERVER}}"
          - "Database Username  {{  hostvars['DB_USER'] }}"

Above is producing error

Update: I also run with verbose and its saying so its pointing to correct config, as i read due to vagrant sometime configuration is not pointed correctly.

Using /etc/ansible/ansible.cfg as config file
5
  • can you say what is the error ? Commented May 29, 2018 at 10:01
  • hostvars['DB_USER'] makes no sense, hostvars contains keys with host names, not variables. For DB_SERVER there is no reason to produce an error with the configuration you posted. Commented May 29, 2018 at 10:21
  • @tux while printing they are not showing any value. if i cast to int its saying undefined variable. Commented May 29, 2018 at 10:40
  • @techraf any clue to further debug as i tried every possible option. Commented May 29, 2018 at 10:41
  • "The task includes an option with an undefined variable. The error was: 'DB_SERVER' is undefined" Commented May 29, 2018 at 10:48

1 Answer 1

0

if you want to use the hostvars magic variables array, you should use the below syntax:

---
- hosts: all
  tasks:
    - debug: 
        msg:
          - "Database server {{DB_SERVER}}"
          - "Database Username  {{  hostvars[inventory_hostname]['DB_USER'] }}"

{{DB_SERVER}} should work, ansible knows how to resolve it to the value you defined in the inventory file.

hope it helps.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.