Skip task if user can't sudo

Viewed 67

I am trying to run a playbook with these tasks on a few thousand servers

  - name: Check root login config
    shell: "egrep -i '^PermitRootLogin' /etc/ssh/sshd_config|awk '{print $2}'"
    register: config_value
    async : 3
    become: yes
    poll: 1

  - name: "config value"
    debug: msg="{{ inventory_hostname }} - {{ config_value.stdout }}"

They have slightly varied configs but this should work on most of them. While running it ,ansible gets stuck somewhere in the middle on some hosts where my user doesn't have passwordless sudo or sudo privileges. I want to skip those servers where this doesn't work.Is there a way to do that ?

ansible-playbook -i hosts playbook.yml --ask-become-pass

I tried giving a wrong password too ,but it still hangs.

1 Answers

Ansible continues with the rest of the hosts if one task fails on one or more hosts. You could use that behaviour by provoking it before the actual tasks. Don't set become in the playbook, do this instead:

- name: Ping or fail host
  become: true
  ping:

- name: Check root login config
  become: true
  shell: "egrep -i '^PermitRootLogin' /etc/ssh/sshd_config|awk '{print $2}'"
    register: config_value
    async : 3
    become: yes
    poll: 1

- name: "config value"
  debug: msg="{{ inventory_hostname }} - {{ config_value.stdout }}"
Related