Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible - Access tags at run time

Tags:

ansible

jinja2

How can I access the tags and skip-tags passed via command line to an ansible playbook at run time?

I am trying to achieve a with_items loop that can skip or include items based on tag/skip-tag using a when clause. This previous SO question touches on the same topic but takes a different approach. I would evaluate the existence of a tag per iteration.

For example:

-  name: Build docker images
    docker_image:
        name: "{{item.name}}"
        path: "{{build_folder}}/dockerfiles/{{item.name}}"
        dockerfile: "{{item.name}}.Dockerfile"
        state: build
        tag: "{{private_docker_registry}}/{{item.name}}"
    when: "{{ansible_host_vars['tags'][image1]}}" is defined
    with_items: 
        - image1
        - image2
        - image3
like image 625
stacksonstacks Avatar asked Mar 13 '23 10:03

stacksonstacks


1 Answers

Since 2.5, ansible added some magic variables to access at runtime. Some of them is ansible_run_tags. It seems to be what you needed.

Ref: Ansible 2.5 change logs

like image 91
Tuanitim Avatar answered Apr 27 '23 21:04

Tuanitim