Certainly! Here’s a simple example of an Ansible playbook that uses ansible_connection=local to run tasks on the local machine:
---
- name: Local Playbook Example
hosts: localhost
connection: local
tasks:
- name: Create a directory
file:
path: /tmp/example_directory
state: directory
- name: Create a file
copy:
content: "This is a local file created by Ansible."
dest: /tmp/example_file.txt
- name: Display the contents of the file
command: cat /tmp/example_file.txt
register: file_contents
- name: Show file contents
debug:
var: file_contents.stdout
Explanation:
- hosts: localhost: Targets the local machine.
- connection: local: Ensures tasks are executed locally.
- tasks: Defines a series of tasks:
- Creates a directory at
/tmp/example_directory. - Creates a file at
/tmp/example_file.txtwith specified content. - Displays the contents of the created file.
- Creates a directory at
You can run this playbook with the command:
ansible-playbook -i localhost, your_playbook.yml
Replace your_playbook.yml with the name of your playbook file. Let me know if you need further assistance!
