title: Process item in Ansible loop only if condition is met and restarting SSH service in Ansible Playbook
body: <p>I have few simple tasks in Ansible playbook:</p>
  
  <pre><code>tasks:
  
    - name: Reset firewall
      action: shell ufw --force reset
  
    - name: Manage firewall ports
      ufw: rule=allow port={{ item }} proto=tcp
      with_items:
        - "{{ ssh_port }}"
        - "{{ 80 if myvar == 'yes' else '' }}"
        - "{{ 8080 if myvar == 'no' else '' }}"
  
    - name: Enable firewall
      ufw: state=enabled
      notify: restart ssh
  
  handlers:
  
    - name: restart ssh
      service:
        name=sshd
        state=restarted
        enabled=yes
  </code></pre>
  
  <p>and 2 simple questions regarding this tasks:</p>
  
  <ul>
  <li>is it possible to completely ignore <code>item</code> in above loop if condition is not met?</li>
  <li>how to "properly" restart <code>sshd</code> service with Ansible after managing ports with <code>ufw</code> (since in my case I understandably get an Ansible's connection error when trying to restart <code>sshd</code>)</li>
  </ul>