Can you give an example playbook?

QuestionsQuestions8 SkillsProAnsible Groups InventoryAug, 16 2025
0118

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:

  1. hosts: localhost: Targets the local machine.
  2. connection: local: Ensures tasks are executed locally.
  3. tasks: Defines a series of tasks:
    • Creates a directory at /tmp/example_directory.
    • Creates a file at /tmp/example_file.txt with specified content.
    • Displays the contents of the created file.

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!

0 Comments

no data
Be the first to share your comment!