首页 > 解决方案 > Ansible.在远程/之后本地运行任务

问题描述

我在远程 Windows 主机上运行 Ansible 任务。我想通过 Telegram 接收结果消息,但默认情况下,这些主机不包含 python,并且 Telegram 模块不起作用。我如何在本地运行它?例如...

- 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

运行后,我想在 Telegram 中获取消息:Task finished at {{ ansible_hostname }} 尝试将此代码插入剧本

- hosts: 127.0.0.1
  connection: local
  gather_facts: false
  tasks:
   telegram:
    token: 'tokentokentokentokentoken'
    chat_id: 1234567890
    msg: Task finished at {{ ansible_hostname }}

但它没有用。此外,这样我得到 ansible_hostname 作为 localhost

标签: 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
    - name: send a message to chat in playbook
      telegram:
       token: 'tokentokentokentokentoken'
       chat_id: 1234567890
       msg: Task finished at {{ ansible_hostname }} 
      delegate_to: localhost

推荐阅读