How can I use the ip in a playbook when inventory has ansible_host

30 views
Skip to first unread message

Scott Fella

unread,
Apr 20, 2020, 9:11:02 PM4/20/20
to Ansible Project
Currently trying to see if there is a way I can grab the ip defined in the ansible_host and use that in a playbook.  I know there is inventory_hostaname, but these for example are not in dns.

hosts inventory example:

[aireos]
wlc2504-01 ansible_host=10.10.201.24
wlc2504-02 ansible_host=10.10.201.25
wlc5520-01 ansible_host=10.11.201.24
wlc5520-02 ansible_host=10.11.201.25

Here is a sample of a playbook which works if the device is registered in dns but fails of course when not.  Is there any way I can extract the ip address so I can loop through all devices in the inventory group?

---
- name: Check if SNMPv2 is working on remote host
hosts: aireos
connection: local
gather_facts: no

tasks:
- name: Test SNMPv2
local_action: command snmpget -v 2c -c testSNMPxxx {{inventory_hostname}} 1.3.6.1.2.1.1.5.0
register: response
- name: Print the complete response
debug:
var: response

Thanks,
-Scott

Dick Visser

unread,
Apr 21, 2020, 12:48:23 AM4/21/20
to ansible...@googlegroups.com
Try 'ansible_host'

--
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 ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/302b741d-c377-41a7-ab93-837f143446c9%40googlegroups.com.
--
Sent from a mobile device - please excuse the brevity, spelling and punctuation.

Dick Visser

unread,
Apr 21, 2020, 12:49:41 AM4/21/20
to ansible...@googlegroups.com

Scott Fella

unread,
Apr 21, 2020, 12:55:08 AM4/21/20
to Ansible Project
Thank you very much!  I can't believe that I didn't scroll down that page far enough, basically was looking at the magic variables. 

-Scott
Try 'ansible_host'

To unsubscribe from this group and stop receiving emails from it, send an email to ansible...@googlegroups.com.
--
Sent from a mobile device - please excuse the brevity, spelling and punctuation.

Stefan Hornburg (Racke)

unread,
Apr 21, 2020, 1:02:09 AM4/21/20
to ansible...@googlegroups.com
On 4/21/20 6:55 AM, Scott Fella wrote:
> Thank you very much!  I can't believe that I didn't scroll down that page far enough, basically was looking at the magic
> variables. 
>
> -Scott

Hello Scott,

in fact ansible_host is a special variable, but any variable you stick on the host line will be available as
variable later.

[aireos]
wlc2504-01 ansible_host=10.10.201.24 myvar=foo

Can be used as {{ myvar }}.

Regards
Racke

>
> On Monday, 20 April 2020 21:49:41 UTC-7, Dick Visser wrote:
>
> From: 
> https://docs.ansible.com/ansible/latest/reference_appendices/special_variables.html
> <https://docs.ansible.com/ansible/latest/reference_appendices/special_variables.html>
>
>
> On Tue, 21 Apr 2020 at 06:47, Dick Visser <dick....@geant.org <javascript:>> wrote:
>
> Try 'ansible_host'
>
> ansible...@googlegroups.com <javascript:>.
> <https://groups.google.com/d/msgid/ansible-project/302b741d-c377-41a7-ab93-837f143446c9%40googlegroups.com?utm_medium=email&utm_source=footer>.
>
> --
> Sent from a mobile device - please excuse the brevity, spelling and punctuation.
>
> --
> Sent from a mobile device - please excuse the brevity, spelling and punctuation.
>
> --
> 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
> ansible-proje...@googlegroups.com <mailto:ansible-proje...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/bba8ccda-df65-4cde-bbc8-2e22d3fc4394%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/bba8ccda-df65-4cde-bbc8-2e22d3fc4394%40googlegroups.com?utm_medium=email&utm_source=footer>.


--
Ecommerce and Linux consulting + Perl and web application programming.
Debian and Sympa administration. Provisioning with Ansible.

signature.asc

Scott Fella

unread,
Apr 21, 2020, 1:09:40 AM4/21/20
to Ansible Project
Thanks for that info also. I will play around with that in the inventory or in the host_vars. That gives me more options I can use during a play.

-Scott

Reply all
Reply to author
Forward
0 new messages