How can I use the search domain in a variable? When I use "{{ ansible_dns['search'] }}"
there are brackets in the output.
Advertisement
Answer
There can be multiple search domains, and that’s why the fact is a list. In short, the first one can be accessed with "{{ ansible_dns['search'][0] }}"
and so on.
If there are multiple search domains and you’d like to use them in a configuration file, you can use the join
filter with the desired “joining” character. The example below will join the search domains with a space (as it appears in /etc/resolv.conf
):
"{{ ansible_dns['search'] | join(' ') }}"
Note: This will work with single or multiple entries in "search": []
.