skip to Main Content

I run Ansible task on remote Windows hosts. I want recieve result messages by Telegram, but by default these hosts not contains python, and Telegram module don’t work. How i can run it locally?
For example…

- 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

After running i want get message in Telegram: Task finished at {{ ansible_hostname }}
Try insert this code in playbook

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

But it didn’t work. Besides, this way i get ansible_hostname as localhost

2

Answers


  1. Please try as below. (not tested)

    - 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
    
    Login or Signup to reply.
  2. As per my knowledge ‘ansible_hostname’ won’t work if you disable gather_facts: False. and i still dont recommend to enable that option.. Try {{ hostvars[‘winservers’][‘inventory_hostname’] }} for your exact requirement.

    - 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'] }}
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search