How to create a playbook?

To create an Ansible playbook, follow these steps:

  1. Open a Terminal: Access your terminal or command line interface.

  2. Navigate to Your Project Directory: Change to the directory where you want to create your playbook. For example:

    cd ~/project
    
  3. Create a New YAML File: Use a text editor to create a new file with a .yml extension. For example, to create a playbook named my_playbook.yml, use:

    nano my_playbook.yml
    
  4. Define the Playbook Structure: Add the necessary YAML structure to your playbook. Here’s a simple example: ```yaml

    • name: My First Playbook hosts: localhost tasks:
      • name: Create a directory file: path: /home/labex/project/test_directory state: directory mode: "0755"
      • name: Create a file copy: content: "Hello from Ansible!" dest: /home/labex/project/test_directory/hello.txt
    
    
  5. Save and Exit: If using nano, save your changes by pressing Ctrl + X, then Y, and finally Enter.

  6. Run the Playbook: Execute your playbook using the ansible-playbook command:

    ansible-playbook my_playbook.yml
    

This will run the tasks defined in your playbook on the specified hosts. You can modify the playbook as needed to include more tasks or target different hosts.

0 Comments

no data
Be the first to share your comment!