How to filter ans extract variable in static inventory

32 views
Skip to first unread message

blue fox

unread,
Nov 23, 2020, 6:36:48 AM11/23/20
to Ansible Project
Hello,

How to filter ans extract variable in static inventory.

Example:

[webserver]

10.10.10.10

20.20.20.20


--------------------------

i would to extract each host alone and put him in variable for use in playbook.

Thank you

Jerin J

unread,
Nov 23, 2020, 6:41:23 AM11/23/20
to ansible...@googlegroups.com
You can simply use {{inventory_hostname}} to get the host name of the 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/0ba5cde2-d244-499a-9711-57b7749cf1c0n%40googlegroups.com.

Stefan Hornburg (Racke)

unread,
Nov 23, 2020, 6:43:18 AM11/23/20
to ansible...@googlegroups.com
First entry:

groups['webserver'][0]

Second entry:

groups['webserver'][1]

Use in a task

debug:
msg: {{ groups['webserver'][0] }}

Regards
Racke

>
> Thank you
>
> --
> 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>.
> <https://groups.google.com/d/msgid/ansible-project/0ba5cde2-d244-499a-9711-57b7749cf1c0n%40googlegroups.com?utm_medium=email&utm_source=footer>.


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

OpenPGP_signature

Jerin J

unread,
Nov 23, 2020, 6:51:32 AM11/23/20
to ansible...@googlegroups.com
{{hostvars [inventory_hostname]["environment"]}}

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/65f28581-924a-4d9e-016a-90c7cd0eb9c2%40linuxia.de.

Jerin J

unread,
Nov 23, 2020, 6:54:54 AM11/23/20
to ansible...@googlegroups.com
Typo 
{{hostvars [inventory_hostname]["environment"][0]}}

blue fox

unread,
Nov 23, 2020, 7:03:23 AM11/23/20
to Ansible Project
Thank you for your answer,
exactly, il like to make a loop for the role , each time he copie each host in file

example:

turn 1 for loop: copie   10.10.10.10.  in file and send it to distant server
turn 2 for loop: copie   20.20.20.20.  in file and send it to distant server

Thank you
Reply all
Reply to author
Forward
0 new messages