task to get username/password from group_vars

I have a pretty easy playbook to connect to a Juniper switch and provide me the show version output. All works when I place the username and password into the playbook no problem. What I would prefer to do is set the username and password in group_vars/all and have the username and password read from there. Any tips? I can’t seem to get this going. I think I may need to pass along a hostvars but can’t seem to figure it out.

playbook:

  • hosts: juniper
    gather_facts: no
    connection: local

tasks:

  • name: DEFINE PROVIDER
    set_fact:
    hostinfo:
    host: “{{ inventory_hostname }}”
    password: xxxxx
    username: admin
  • debug: var=hostvars[inventory_hostname]
  • name: show command
    junos_command:
    provider: “{{ hostinfo }}”
    commands:
  • show version
    register: version
  • debug: msg=“{{ version.stdout }}”

group_vars/all
username=admin

password=xxxxxx

`

[duchess@cody:tmp] cat /etc/ansible/hosts

[juniper]

localhost

`

Make sure you have localhost in the juniper group in your inventory.

`

[duchess@cody:tmp] cat group_vars/juniper

I almost forgot. . .

It’s generally unsafe to store credentials in cleartext on your hard drive, so the documentation on the Ansible Vault may also be of some use to you.