0

I want to use Ansible to run certain tasks only on hosts that I can ssh into. So I want Ansible to ssh into all the hosts I specify. If it can successfully ssh into then continue to run tasks on those hosts; hosts that it cannot ssh into, skip for the rest of the play/playbook. BTW, I am not very experianced with Ansible.

Thank you, Mike

5
  • Ansible already does this. Jan 31 at 21:34
  • Just to be clear, I do not want to use the 'ansible' user that Ansible uses. I want to use another specified user account for this test. What I am testing for is, can a user login via winbind or sssd. If yes, run tasks on that hosts, if not, ignore that host for the rest of the playbook.
    – Mike
    Jan 31 at 22:17
  • Ansible already does this. Jan 31 at 22:57
  • How do I tell Ansible what user name to use and what the password is?
    – Mike
    Jan 31 at 23:10
  • Set the remote_user in ansible.cfg, in your playbook, in a host variable, or wherever you need it. This was covered in the getting started page in the Ansible documentation. Feb 1 at 12:55
0

If you have a playbook with tasks for a specific host group (all) and run Ansible, then Ansible first tries to connect to the hosts according your Ansible connection configuration in your inventory (by gathering facts). If Ansible can reach the host via that connection type, it runs the tasks against the host. If it cannot reach the host, the server goes into a failure state (and will not try to execute the tasks on that machine.

Of course - for that hosts there are errors/failures states at the end of the play.

If you don't want to have any failure state at the end of the playbook, you could use a simple first task with wait_for_connection module, register the output and ignore any errors. And do not gather facts. All following tasks could then check the failure output.

- name: "Bootstrap"
  hosts: all
  gather_facts: false
  tasks:
    - name: "Check, if server can be reached"
      wait_for_connection:
        delay: 1
        sleep: 1
      ignore_errors: true
      register: _bootstrap_result

    - name: "Gather facts on available hosts"
      setup:
      when:
        - "_bootstrap_result is not failed"

    - name: "Run my tasks"
      include_tasks: "setup.yml"
      when:
        - "_bootstrap_result is not failed"

If you want to switch to a different connection type in case of an error on the first connection attempt with wait_for_connection, then just set concrete facts for the host on failure state.

- name: "Check, if server can be reached on ssh port 2222"
  vars:
    ansible_port: 2222
  wait_for_connection:
    delay: 1
    sleep: 1
  ignore_errors: true
  register: _bootstrap2_result
  when: "_bootstrap_result is failed"

Of course this is just an example with switching the SSH port.

0

One way can be to use module ping combined with register and ignore_errors. Then use module add_host to build a new in-memory hostgroup, and execute that task only when registered result was successful.

Start a new play, using the in-memory host group created in the previous step to execute all the tasks on the online hosts.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.