0

I'm using ansible 2.9 to run some tasks on localhost, and then tasks on a Windows host (using powershell as default shell), and then final tasks on localhost again.

Right now I'm running then separately in the play as such :

- hosts: localhost
  become: yes
  any_errors_fatal: true
  gather_facts: no
  force_handlers: true
  pre_tasks:
.......
- hosts: wintel
  become: yes
  become_user: AD_user
  gather_facts: no
  force_handlers: true

  roles:
     - role: mywindowsrole

- hosts: localhost
  become: yes
  gather_facts: no
  force_handlers: true

  post_tasks:
...........

I have a requirement now, to run some tasks on localhost (linux), when running the role for the remote host (wintel). I included them as below within the main.yml of mywindowsrole :

- name: Include linux tasks
  include: linuxtasks.yml
  delegate_to: localhost
  become: true
  become_method: sudo
  become_user: root

However when running the play it doesnt look like its delegating the tasks to the localhost, instead it was running them itself (wintel.test.com):

TASK [mywindowsrole: Include linux tasks] ***************************************************************************************************************************************
included: /mydir/roles/mywindowsrole/tasks/main.yml for wintel.test.com


 fatal: [wintel.test.com]: FAILED! => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "module_stderr": "Exception calling \"Create\" with \"1\" argument(s): \"At line:4 char:21\r\n+ def _ansiballz_main():\r\n+                     ~\r\nAn expression was expected after '('.\r\nAt line:13 char:27\r\n+     except (AttributeError, OSError):\r\n+                           ~\r\nMissing argument in parameter list.\r\nAt line:15 char:7\r\n+     if scriptdir is not None:\r\n+       ~\r\nMissing '(' after 'if' in if statement.\r\nAt line:22 char:7\r\n+     if sys.version_info < (3,):\r\n+       ~\r\nMissing '(' after 'if' in if statement.\r\nAt line:22 char:30\r\n+     if sys.version_info < (3,):\r\n+                              ~\r\nMissing expression after ','.\r\nAt line:22 char:25\r\n+     if sys.version_info < (3,):\r\n+                         ~\r\nThe '<' operator is reserved for future use.\r\nAt line:27 char:34\r\n+     def invoke_module(modlib_path, temp_path, json_params):\r\n+                                  ~\r\nMissing argument in parameter list.\r\nAt line:28 char:40\r\n+         z = zipfile.ZipFile(modlib_path, mode='a')\r\n+                                        ~\r\nMissing argument in parameter list.\r\nAt line:31 char:33\r\n+         zinfo = zipfile.ZipInfo()\r\n+                                 ~\r\nAn expression was expected after '('.\r\nAt line:34 char:25\r\n+         z.writestr(zinfo, sitecustomize)\r\n+                         ~\r\nMissing argument in parameter list.\r\nNot all parse errors were reported.  Correct the reported errors and try again.\"\r\nAt line:6 char:1\r\n+ $exec_wrapper = [ScriptBlock]::Create($split_parts[0])\r\n+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\r\n    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException\r\n    + FullyQualifiedErrorId : ParseException\r\n \r\nThe expression after '&' in a pipeline element produced an object that was not valid. It must result in a command \r\nname, a script block, or a CommandInfo object.\r\nAt line:7 char:2\r\n+ &$exec_wrapper\r\n+  ~~~~~~~~~~~~~\r\n    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException\r\n    + FullyQualifiedErrorId : BadExpression\r\n ", "module_stdout": "", "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error", "rc": 1}

Why doesnt the tasks get delegated to localhost (the linux server that is the ansible controller)?

Here is a snippet of my inventory file:

[wintel]
wintel.test.com

JaneD
  • 149
  • 1
  • 2
  • 15
  • 2
    `delegate_to` on `include(_tasks)` has no effect. You need to delegate each task inside the include. Here is a [possible workarround](https://serverfault.com/questions/755297/is-it-possible-to-include-a-tasks-file-with-a-different-set-of-hosts) – Zeitounator Jun 17 '20 at 09:46
  • Thats interesting, because I have used `delegate_to` with `include`, but against a linux host instead of windows. But you are right that I could delegate it successfully per task. – JaneD Jun 17 '20 at 11:04

0 Answers0