vars_prompt:
- name: "hosts_prompt"
prompt: "hostname or host group need to run with Playbook"
private: no
default: dev
pre_tasks:
- setup:
filter: ansible_*
tasks:
- name: backup ip
debug:
msg: "{{ ansible_all_ipv4_addresses }}"
- name: search backup ip which starts with '192.168',assign to some other variable. If not, assign primary ip
set_fact:
backupip: "{% for ip in {{ ansible_all_ipv4_addresses }} %}
{% if ip | regex_search('192.168.*.*') %}
{{ ip }}
{% else % }
{{ ip[0] }}
{% endif %}
{% endfor %}"
This should be do the trick:
"{{ ansible_all_ipv4_addresses | select('search', '192.168.*.*') | first | default(ansible_all_ipv4_addresses[0]) }}"
However, the regular expression is not accurate as means ".*" match ANY character.
Your original error code is caused by using {{ ... }} inside of the {% for ... %}.