<p>I also tried to make something more &quot;practical&quot; so i just search a bit around and found the task: &quot;include_role&quot; which allows loops. So i decided to loop that task with my target roles and use a when. Here the snippet:</p>
<pre><code>- name: &quot;Redhat&quot;
  hosts: all
  become: yes
  tasks:
    - name: ADD Roles
      include_role:
        name: &quot;{{role}}&quot;
        apply:
          ignore_errors: yes
      with_items:
        - role1
        - role2
      loop_control:
        loop_var: role
      when: ansible_os_family==&quot;RedHat&quot;
</code></pre>

CC BY-SA 4.0
2021-01-21T13:40:53.997Z
2021-01-21T13:40:53.997Z
7r0s7
<p>Although the comments have highlighted the fact that this playbook is fine, as long as it is designed to do what it needs to do:</p>

<blockquote>
  <p>The reason behind this is that, the first play will create ansible user with sudo access. Here, I have used root user to run the play. Then I have to run another play as ansible user.</p>
</blockquote>

<p>In the first play, you have only one role: <code>role1</code>. If in <code>role1</code> you have a task that creates the ansible user, then you can use that user to connect the next time. This is actually something many people do, including myself, to put a known baseline on a service:</p>

<pre><code>- name: Ensure Ansible user is present (RedHat)
  user:
   name: ansible
   comment: "ansible user created by bootstrap playbook"
   generate_ssh_key: yes
   groups: wheel
 tags:
 - bootstrap
 when: ansible_os_family=="RedHat"

- name: Ensure Ansible user is present (Debian)
  user:
   name: ansible
   comment: "ansible user created by bootstrap playbook"
   generate_ssh_key: yes
   groups: sudo
  tags:
  - bootstrap
  when: ansible_os_family=="Debian"

- name: update sudoers to ensure ansible user can sudo
  lineinfile:
    dest: /etc/sudoers
    state: present
    regexp: '^ansible'
    line: 'ansible ALL=(ALL) NOPASSWD: ALL'
  tags:
  - bootstrap
</code></pre>

<p>This will ensure that the user is added, and included in the right groups, with sudo rights -- it is just an example of how you could achieve this, there are other strategies of course.</p>

<blockquote>
  <p>My question is how to run these play one after another? I first want to run first one then after that second one.</p>
</blockquote>

<p>That's exactly how playbooks work.</p>

<blockquote>
  <p>If I run the playbook then it will get stuck because it will first run first play and tries to run second play where it gets stuck.</p>
</blockquote>

<p>If you do not have a task to configure the user required in the next play, then yes, it will fail (not get stuck). 
The point is that since Ansible is idempotent, you don't have to worry about skipping the play because if the user is properly configured, the first play will simply have no effect.</p>

<p>However, to answer the actual question </p>

<blockquote>
  <p>How do I skip a play in an Ansible playbook ? </p>
</blockquote>

<p>This is done using the conditional execution using <a href="https://docs.ansible.com/ansible/latest/user_guide/playbooks_conditionals.html#the-when-statement" rel="nofollow noreferrer"><code>when</code></a></p>

CC BY-SA 4.0
2019-09-18T15:55:55.460Z
Sadly, `when` can't be used at play level.
jonhattan
CC BY-SA 4.0
2018-11-11T10:41:31.573Z
Thank you for your time. I ended up using two yml files and running one after another. Anyway, it is okay for me to run this way.
Prakash
CC BY-SA 4.0
2018-11-09T09:53:42.590Z
2018-11-09T09:53:42.590Z
Bruce Becker
<p>For those looking for a way to skip a play in Ansible (e.g. troubleshooting the second or third play during development), my workaround is to temporarily change the <code>hosts</code> setting to an unused group name like <code>skip-for-now</code>. Then when the Playbook runs, that Play will be skipped:</p>
<pre><code>PLAY [Lock down SSH] ***************************************************************************************************
skipping: no hosts matched
</code></pre>

CC BY-SA 4.0
2024-09-13T00:53:03.347Z
2024-09-13T00:53:03.347Z
Matthew
<p>I have 2 plays in my sites.yml file:</p>

<hr>

<pre><code>- name: First play
  hosts: all
  remote_user: root
  roles:
       - role1


- name: Second Play
  hosts: all
  remote_user: ansible
  become: yes
  become_method: sudo
  connection: ssh
  gather_facts: yes
  roles:
     - role2
     - role3
</code></pre>

<p>The reason behind this is that, the first play will create ansible user with sudo access. Here, I have used root user to run the play. Then I have to run another play as ansible user.</p>

<p>My question is how to run these play one after another? I first want to run first one then after that second one.</p>

<p>If I run the playbook then it will get stuck because it will first run first play and tries to run second play where it gets stuck.</p>

<p>I am running playbook with the following command:</p>

<pre><code>ansible-playbook -i inventory.ini -l web sites.yml --ask-pass -c paramiko
</code></pre>

<p>This is for 1st part.</p>

<ansible>
How to skip play in ansible-playbook?
Prakash
# How to skip play in ansible-playbook?

(Asked by: username_0 on 2018-08-23)

I have 2 plays in my sites.yml file:

- name: First play
  hosts: all
  remote_user: root
  roles:
       - role1


- name: Second Play
  hosts: all
  remote_user: ansible
  become: yes
  become_method: sudo
  connection: ssh
  gather_facts: yes
  roles:
     - role2
     - role3

The reason behind this is that, the first play will create ansible user with sudo access. Here, I have used root user to run the play. Then I have to run another play as ansible user.
My question is how to run these play one after another? I first want to run first one then after that second one.
If I run the playbook then it will get stuck because it will first run first play and tries to run second play where it gets stuck.
I am running playbook with the following command:
ansible-playbook -i inventory.ini -l web sites.yml --ask-pass -c paramiko

This is for 1st part.

**Tags:** ansible


---

## 3 answers

### Answer by username_1 on 2021-01-21. Score: 1

I also tried to make something more "practical" so i just search a bit around and found the task: "include_role" which allows loops. So i decided to loop that task with my target roles and use a when. Here the snippet:
- name: "Redhat"
  hosts: all
  become: yes
  tasks:
    - name: ADD Roles
      include_role:
        name: "{{role}}"
        apply:
          ignore_errors: yes
      with_items:
        - role1
        - role2
      loop_control:
        loop_var: role
      when: ansible_os_family=="RedHat"


---

### Answer by username_2 on 2018-11-09. Score: 1

Although the comments have highlighted the fact that this playbook is fine, as long as it is designed to do what it needs to do:

The reason behind this is that, the first play will create ansible user with sudo access. Here, I have used root user to run the play. Then I have to run another play as ansible user.

In the first play, you have only one role: role1. If in role1 you have a task that creates the ansible user, then you can use that user to connect the next time. This is actually something many people do, including myself, to put a known baseline on a service:
- name: Ensure Ansible user is present (RedHat)
  user:
   name: ansible
   comment: "ansible user created by bootstrap playbook"
   generate_ssh_key: yes
   groups: wheel
 tags:
 - bootstrap
 when: ansible_os_family=="RedHat"

- name: Ensure Ansible user is present (Debian)
  user:
   name: ansible
   comment: "ansible user created by bootstrap playbook"
   generate_ssh_key: yes
   groups: sudo
  tags:
  - bootstrap
  when: ansible_os_family=="Debian"

- name: update sudoers to ensure ansible user can sudo
  lineinfile:
    dest: /etc/sudoers
    state: present
    regexp: '^ansible'
    line: 'ansible ALL=(ALL) NOPASSWD: ALL'
  tags:
  - bootstrap

This will ensure that the user is added, and included in the right groups, with sudo rights -- it is just an example of how you could achieve this, there are other strategies of course.

My question is how to run these play one after another? I first want to run first one then after that second one.

That's exactly how playbooks work.

If I run the playbook then it will get stuck because it will first run first play and tries to run second play where it gets stuck.

If you do not have a task to configure the user required in the next play, then yes, it will fail (not get stuck). 
The point is that since Ansible is idempotent, you don't have to worry about skipping the play because if the user is properly configured, the first play will simply have no effect.
However, to answer the actual question 

How do I skip a play in an Ansible playbook ? 

This is done using the conditional execution using when

> **Comments:**
> **username_0**: Thank you for your time. I ended up using two yml files and running one after another. Anyway, it is okay for me to run this way.  
> **username_3**: Sadly, `when` can't be used at play level.  

---

### Answer by username_4 on 2024-09-13. Score: 1

For those looking for a way to skip a play in Ansible (e.g. troubleshooting the second or third play during development), my workaround is to temporarily change the hosts setting to an unused group name like skip-for-now. Then when the Playbook runs, that Play will be skipped:
PLAY [Lock down SSH] ***************************************************************************************************
skipping: no hosts matched


---

