Ansible delegate_to task trying to ssh
Asked Answered
T

1

8

I have a simple ansible role and couple of basic tasks within this role. All tasks are defined as local_action(or delegate_to: localhost). The host defined in the playbook is also localhost.

Now when I am running this playbook, it first tries to test ssh connection before executing the role/tasks. This is not a problem but I find it unnecessary to establish or test ssh connection before running a playbook which is explicitly targeting localhost as host. Following is how my playbook (playbook.yml) looks like:

- hosts: db-servers
  roles:
  - role: test

And the role definition (roles/test/tasks/main.yml) looks as follow:

---
  - name: Resolve and copy test configuration
    template:
      src: "roles/test/templates/temp.conf"
      dest: "roles/test/files/temp-4.2.0/temp.conf"
    delegate_to: 127.0.0.1
    become: no

  - name: Run test job
    delegate_to: 127.0.0.1
    command: roles/test/files/test-4.2.0/test.sh
    become: no

Following is my inventory file inv/test:

[db-servers]
localhost

And I am using this command to run my playbook:

ansible-playbook -i inv/test playbook.yml -vvv

Is there anyway in ansible that I could prevent this ssh connectivity check?

Teressaterete answered 11/7, 2017 at 13:20 Comment(3)
If the playbook specifies hosts: 127.0.0.1, then delegate_to: 127.0.0.1 is unnecessary.Encarnacion
Yes, but ansible will still try to ssh to 127.0.0.1 in the beginning even the task is delegate_to: 127.0.0.1. My target is to avoid this unnecessary ssh completely.Teressaterete
Yes -- for that, @techraf made the correct suggestion: connection: local.Encarnacion
D
13

Add connection: local as a task property.

- name: Run test job
  delegate_to: 127.0.0.1
  connection: local
  command: roles/test/files/test-4.2.0/test.sh
  become: no

Or define the host in the inventory and assign the connection type:

127.0.0.1 ansible_connection=local
Desulphurize answered 11/7, 2017 at 13:27 Comment(9)
I haven't tried the 1st solution but I think that Ansible will still try to verify ssh connectivity with the target host before running the task locally (since this happens at the startup before executing the tasks. Or am I wrong in my thinking?Teressaterete
yes, I haven't tried your first solution yet but have a feeling it may not work. I will give it a try now leave a comment here!Teressaterete
Sorry, but StackOverflow is not about your feelings. You are essentially saying: I'm too lazy to verify your answer, so instead I will bother you for some confirmation.Desulphurize
Yes, so your 1st solution didn't work. If I dont have ansible_connection=local in inventory and have connection: local defined on the task, Ansible still tries to ssh to the target host before executing the task locally. Would you mind updating your answer?Teressaterete
No it doesn't. I verified my answer. Either you are doing something different, or are using other Ansible version than the latest one.Desulphurize
Ansible gathers facts at first, but then it's completely unclear what you are trying to achieve. If you want to run the whole playbook on localhost, then why do you use delegation in the first place? You asked how to change the connection type for a task didn't you? The title is pretty clear.Desulphurize
I am not trying to do anything unusual. There is a simple playbook (copied in question). There are 2 simple tasks, one for copying config file and another one to execute a shell script. I have updated the question to add my inventory file and ansible command to run the playbook. With this setup can you verify that ansible is executing the tasks without verifying ssh connectivity? For me it is always trying to connect using ssh before executing the tasks.Teressaterete
ok, I see that the title is a bit misleading. Basically what I was looking for was changing the connection type for a single playbook (not task).Teressaterete
Then define connection: local on play level and remove all delegate_to. You also don't need inventory file at all. But then the question gets pretty basic... The answer is on the first page of Ansible Getting Started guide. BTW, you cannot set anything "for a playbook", because a playbook is only a file. It doesn't exist as any entity in Ansible. Ansible uses plays, tasks, etc. but playbook is just a file containing plays.Desulphurize

© 2022 - 2024 — McMap. All rights reserved.