To create an Ansible playbook, follow these steps:
-
Open a Terminal: Access your terminal or command line interface.
-
Navigate to Your Project Directory: Change to the directory where you want to create your playbook. For example:
cd ~/project -
Create a New YAML File: Use a text editor to create a new file with a
.ymlextension. For example, to create a playbook namedmy_playbook.yml, use:nano my_playbook.yml -
Define the Playbook Structure: Add the necessary YAML structure to your playbook. Here’s a simple example:
--- - 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 -
Save and Exit: If using
nano, save your changes by pressingCtrl + X, thenY, and finallyEnter. -
Run the Playbook: Execute your playbook using the
ansible-playbookcommand: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.
