How to skip all roles and run all tasks without tagging

Viewed 229

When I'm writing a playbook I need to run all tasks repeatedly. I could tag all tasks (and/or roles) to selectively run these using --tags. But what if I just want to run all tasks skipping all roles? Is there no shortcut for that that does not require tagging everything?

Given a playbook:

---
- name: example playbook
  become: true
  hosts:
    - worker

  roles:
    - dev-sec.ssh-hardening
    - iptables

  tasks:
    - name: install pip3
      apt: name=python3-pip state=present

    - name: install kas via pip
      pip:
        name: kas
        state: present

something like

ansible-playbook -i hosts.yml my-example-playbook.yml --skip-all-roles

to skip all roles and only run the tasks of the playbook my-example-playbook.yml without tagging tasks.

2 Answers

A simple workaround:

---
- name: example playbook
  become: true
  hosts:
    - worker

  roles:
    - role: some_role
      when: not skip_roles | default(false) | bool

  tasks:
    - name: a playbook task
      debug:
        msg: a simple task

If you don't want to have all role tasks appearing as skipped (i.e. completely skip the role) you can try the following

---
- name: example playbook
  become: true
  hosts:
    - worker

  tasks:
    - name: run some_role
      include_role:
        name: some_role
      when: not skip_roles | default(false) | bool

    - name: a playbook task
      debug:
        msg: a simple task

With either solution you can then run your playbook as:

# Run normally with all roles
ansible-playbook -i my_inventory my_playbook.yml

# Run skipping roles
ansible-playbook -i my_inventory -e skip_roles=true my_playbook.yml

Two other ways — the second one would work without any change, but presuppose a specific playbook structure:

  1. Instead of tagging a bunch of tasks, mind that you can tag a block:
    - hosts: worker
      become: true
    
      roles:
        - dev-sec.ssh-hardening
        - iptables
    
      tasks:
        - block:
          - name: install pip3
            apt: 
              name: python3-pip 
              state: present
    
          - name: install kas via pip
            pip:
              name: kas
              state: present
    
          tags: only-tasks
    
    And, run it with
    ansible-playbook play.yml --tags only-tasks
    
  2. Without changing anything, presuposing your roles are always before your tasks and that you know the name of the first task, you can use the --start-at-task flag, so in your case:
    ansible-playbook play.yml --start-at-task "install pip3"
    
Related