[duchess@cody:tmp] cat /etc/ansible/hosts
[juniper]
localhost
[duchess@cody:tmp] cat group_vars/juniper
---
username: admin
password: This is not a good password.
...
[duchess@cody:tmp] cat playbook.yml
---
- name: Execute a show command on a junos device.
hosts: juniper
gather_facts: no
connection: local
tasks:
- name: Define the provider.
set_fact:
hostinfo:
host: "{{ inventory_hostname }}"
password: "{{ password }}"
username: "{{ username }}"
- name: Output the hostname.
debug:
var: hostvars[inventory_hostname]
# I don't have a junos device handy so I can't test this, but. . .
#- name: show command
# junos_command:
# provider: "{{ hostinfo }}"
# commands:
# - show version
# register: version
#- name: Output the version.
# debug:
# var: version
- name: Output the username and password from group_vars/juniper.
debug:
msg: "The username is {{ username }}. The password is '{{ password }}'."
...
[duchess@cody:tmp] ansible-playbook playbook.yml
PLAY [Execute a show command on a junos device.] *******************************
TASK [Define the provider.] ****************************************************
ok: [localhost]
TASK [Output the hostname.] ****************************************************
ok: [localhost] => {
"hostvars[inventory_hostname]": {
"ansible_check_mode": false,
"ansible_version": {
"full": "2.1.2.0",
"major": 2,
"minor": 1,
"revision": 2,
"string": "2.1.2.0"
},
"group_names": [
"juniper"
],
"groups": {
"all": [
"localhost"
],
"juniper": [
"localhost"
],
"ungrouped": []
},
"hostinfo": {
"host": "localhost",
"password": "This is not a good password.",
"username": "admin"
},
"inventory_dir": "/etc/ansible",
"inventory_file": "/etc/ansible/hosts",
"inventory_hostname": "localhost",
"inventory_hostname_short": "localhost",
"omit": "__omit_place_holder__fe3ea0370d6ef7ffad66e4a8a81c05821adcfe79",
"password": "This is not a good password.",
"playbook_dir": "/private/tmp",
"username": "admin"
}
}
TASK [Output the username and password from group_vars/juniper.] ***************
ok: [localhost] => {
"msg": "The username is admin. The password is 'This is not a good password.'."
}
PLAY RECAP *********************************************************************
localhost : ok=3 changed=0 unreachable=0 failed=0