I have a python script which is able to retrieve the IP adress of my remote node, and as a pre-task of my ansible playbook I would like to run this script and set the IP adress. Is there a command in Ansible which would allow me to do this ?
Asked
Active
Viewed 1,723 times
0
-
`delegate_to: localhost` along with `set_fact` should do the trick. For more details, more information about the script is needed. – Gerald Schneider Feb 14 '23 at 07:19
2 Answers
0
With delegate_to it is possible to control where an action is run.
Straight from the Ansible documentation:
---
- hosts: webservers
serial: 5
tasks:
- name: Take out of load balancer pool
ansible.builtin.command: /usr/bin/take_out_of_pool {{ inventory_hostname }}
delegate_to: 127.0.0.1
This will run the command on the ansible host. There is really not much more to it!
Link to Ansible documentation on the topic.

proxx
- 121
- 6
0
yes you can
- name: Run local script
hosts: localhost
connection: local
gather_facts: false
tasks:
- name: Execute script
command: /path/to/your/script.py
register: script_output
- name: Print script output
debug:
var: script_output.stdout
or as proxx mentioned, u can use delegate_to
- name: Run local script on remote host
hosts: your_remote_node
gather_facts: false
tasks:
- name: Execute script on control machine
command: /path/to/your/script.py
register: script_output
delegate_to: localhost
- name: Print script output
debug:
var: script_output.stdout

Salim Aljayousi
- 341
- 1
- 3