Skip to content

Instantly share code, notes, and snippets.

@natefoo
Last active May 28, 2021 11:23
Show Gist options
  • Save natefoo/6fd55573478a38b50e359505495ed0cf to your computer and use it in GitHub Desktop.
Save natefoo/6fd55573478a38b50e359505495ed0cf to your computer and use it in GitHub Desktop.
Ansible tasks for selecting an OpenStack image from a list using a regular expression
---
- name: Get Image
hosts: all
gather_facts: no
environment:
OS_CLOUD: "{{ cloud_id }}"
OS_IDENTITY_API_VERSION: '3'
connection: local
tasks:
- name: Collect image facts
os_image_facts:
delegate_to: localhost
- name: Display image facts
debug:
msg: "{% if item.name.startswith('JS-API-Featured-CentOS7-') %}{% set image = item.id %}{% endif %}"
with_items: "{{ openstack_image }}"
no_log: true
- name: Debug image
debug:
var: image
---
- name: Get Image
hosts: all
gather_facts: no
environment:
OS_CLOUD: "{{ cloud_id }}"
OS_IDENTITY_API_VERSION: '3'
connection: local
tasks:
- name: Collect image facts
os_image_facts:
delegate_to: localhost
- name: Find current JS-API-Featured-CentOS7 image
set_fact:
image: "{{ item }}"
with_items: "{{ openstack_image }}"
when: item.name.startswith('JS-API-Featured-CentOS7-')
loop_control:
label: item.name
- name: Debug image
debug:
var: image
---
- name: Get Image
hosts: all
gather_facts: no
environment:
OS_CLOUD: "{{ cloud_id }}"
OS_IDENTITY_API_VERSION: '3'
connection: local
tasks:
- name: Collect image facts
os_image_facts:
delegate_to: localhost
- name: Find current JS-API-Featured-CentOS7 image
set_fact:
image: "{{ ( openstack_image | selectattr('name', 'match', '(?i)^JS-API-Featured-CentOS7-[A-Z]{3}-\\d{1,2}-\\d{4}$') | list | last ).id }}"
run_once: true
- name: Debug image
debug:
var: image
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment