Ansible.在远程后本地运行任务/

Ansible.在远程后本地运行任务/,ansible,Ansible,我在远程Windows主机上运行Ansible任务。我希望通过电报接收结果消息,但默认情况下,这些主机不包含python,电报模块不工作。我如何在本地运行它? 例如 - hosts: winservers vars: scope: win script: Rulez.PS1 folder: C:\TEMP gather_facts: false vars_files: - /etc/ansible/win/group_vars/{{ scope }}.sec

我在远程Windows主机上运行Ansible任务。我希望通过电报接收结果消息,但默认情况下,这些主机不包含python,电报模块不工作。我如何在本地运行它? 例如

- hosts: winservers
  vars:
   scope: win
   script: Rulez.PS1
   folder: C:\TEMP
  gather_facts: false
  vars_files:
   - /etc/ansible/win/group_vars/{{ scope }}.sec
  tasks:
    - name: Сheck for path {{ folder }} availability. Create if not present.
      win_file:
       path: "{{ folder }}"
       state: directory
运行后,我希望在电报中获取消息:任务在{{ansible_hostname}完成 尝试在playbook中插入此代码

- hosts: 127.0.0.1
  connection: local
  gather_facts: false
  tasks:
   telegram:
    token: 'tokentokentokentokentoken'
    chat_id: 1234567890
    msg: Task finished at {{ ansible_hostname }}
但它不起作用。此外,通过这种方式,我可以将ansible_主机名设置为localhost

请尝试以下操作。(未测试)


据我所知,“ansible\u主机名”在禁用“收集事实:False”时不起作用。我仍然不建议启用该选项。。请尝试{{hostvars['winservers']['inventory\u hostname']}以满足您的确切需求

- hosts: winservers
  vars:
   scope: win
   script: Rulez.PS1
   folder: C:\TEMP
  gather_facts: false
  vars_files:
   - /etc/ansible/win/group_vars/{{ scope }}.sec
  tasks:
    - name: Сheck for path {{ folder }} availability. Create if not present.
      win_file:
       path: "{{ folder }}"
       state: directory

- hosts: 127.0.0.1
  connection: local
  gather_facts: false
  tasks:
    telegram:
        token: 'tokentokentokentokentoken'
        chat_id: 1234567890
        msg: Task finished at {{ hostvars['winservers']['inventory_hostname'] }}

我可以通过调用RestMethod-Uri通过powershell运行https://api.telegram.org/bot$($MyBotToken)/sendMessage?聊天室id=$($chatID)&text=$($Message)&parse_mode=html“正如您作为-hosts:127.0.0.1给出的那样,ansible_主机名将为您提供本地主机。
- hosts: winservers
  vars:
   scope: win
   script: Rulez.PS1
   folder: C:\TEMP
  gather_facts: false
  vars_files:
   - /etc/ansible/win/group_vars/{{ scope }}.sec
  tasks:
    - name: Сheck for path {{ folder }} availability. Create if not present.
      win_file:
       path: "{{ folder }}"
       state: directory

- hosts: 127.0.0.1
  connection: local
  gather_facts: false
  tasks:
    telegram:
        token: 'tokentokentokentokentoken'
        chat_id: 1234567890
        msg: Task finished at {{ hostvars['winservers']['inventory_hostname'] }}