Delegation to a host
Here is an example where we can delegate the task to a particular host. This play book is using inventory_hostname from the gather facts.
- name: Delegation to localhost
hosts: all
tasks:
- name: create a file on target server
file:
path: /tmp/i_m_on_target_server.txt
state: touch
- name: create a file with host named file by delegation to localhost
file:
state: touch
path: "/tmp/{{ inventory_hostname }}.txt"
delegate_to: localhost
connection paramer
We can use this "connection" parameter add to your task level or play level.
# Filename: connection_local.yml
# To do some task on ansible server
# local means without doing ssh command (no need of password and no need of ssh keys)
# with the local connection parameter for the play
---
- name: This is to determine how the connection parameter works with local
hosts: app
connection: local
gather_facts: false
tasks:
- name: connection local test
file:
state: touch
path: /tmp/srirama.txt
local action
Now in the following example we will see how local_action will be working. The local_action is an Ansible module which will by pass the ssh connection to do command locally.
- name: Using local acion in this playbook
hosts: localhost
vars:
test_file: /tmp/omnamahshivaya.txt
tasks:
- name: local action
local_action: "command touch {{ test_file }}"
The better option to do it in local is using connection: local parameter at play level.
Comments