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

[juniper]

localhost


[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] cat group_vars/juniper 

---

username: admin

password: This is not a good password. 

...

# vim: ft=ansible:






[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 
 


I should like to suggest you review the documentation on best practices 
<http://docs.ansible.com/ansible/playbooks_best_practices.html>. 

You might also consider reviewing the documentation on inventory 
<http://docs.ansible.com/ansible/intro_inventory.html> and variables 
<http://docs.ansible.com/ansible/playbooks_variables.html#>.

Finally, please give the community guidelines 
<http://docs.ansible.com/ansible/community.html> a thorough going over. 

I believe you'll find all the answers you're looking for in the resources 
provided.

Best of luck!

Alex


On Wednesday, October 19, 2016 at 8:07:24 AM UTC-7, RK wrote:
>
> 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
>
>
-- 

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/f2dd1754-0055-42d6-acea-54b29e617445%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to