Ansible training-Day3 (the principle, structure and basic use of playbook)

I. Introduction

     This article is a summary of the content of the third day of ansible training, mainly including the principle, structure and basic usage of playbook components.

2. Playbook principle

        Playbooks are one of the core components of Ansible, which are automation scripts used to define tasks and configurations.

        Ansible Playbooks are written using YAML syntax, which can describe a series of tasks and configurations to be performed on managed hosts. Playbook defines one or more scripts (play), and each script defines a set of tasks that will be executed on the target host. Tasks in a playbook are executed sequentially in the order they are defined.

        Each task contains one or more modules, which are reusable functional units of Ansible for performing various operations, such as file operations, software installation, service management, etc. Each module receives parameters, performs an operation based on those parameters, and returns a result. Different tasks and modules can be used in playbooks as needed.

3. Playbook structure

        The structure of Playbook generally includes the following contents:

        - `name` : the name or description of the playbook
        - `hosts` : define the host or host group to be managed
        - `vars` : define variables for passing parameters and configuration options in tasks
        - `tasks` : contain one or more A list of tasks for each task
        - `handlers` : define handlers for handling specific events during task execution
        - `roles` : define reusable roles, containing a set of related tasks and configurations

        Ansible Playbook can be used to automate configuration management, software deployment, system management and other tasks. It is easy to use, highly readable, and scalable, and is compatible with multiple operating systems and cloud platforms.

        To run a Playbook, use the `ansible-playbook` command, specifying the path to the Playbook file. Ansible will parse the Playbook file and execute the defined tasks and configurations on the specified hosts.

        Ansible Playbook is a powerful tool for implementing Infrastructure as Code (Infrastructure as Code), which can improve the efficiency and reliability of automated management.

4. Steps to implement Playbook

Step1: Write Ansible Playbook

        Written in YAML format, and lists tasks and hosts in the order specified in the playbook.

Step2: Configure Ansible's host list

        Specify the managed hosts to manage in the inventory, and the host groups defined in the playbook.

Step3: Verify Ansible configuration

        Executing `ansible --version` ensures that the Ansible version is installed correctly and displays the expected version number.

Step4: Run Playbook on the control machine

        Execute `ansible-playbook playbook.yml` to run the Ansible playbook. `playbook.yml` is the name of the playbook file to run.

        Ansible will then read the playbook and host inventory and run the specified tasks on the managed hosts. During a run, Ansible will output detailed information about the operation, including successful and failed tasks, variable values, and error messages.

        It is important to note that Ansible's behavior depends on how it is connected to the managed host. If the connection is not secure, sensitive information may be disclosed and pose a potential security risk to managed hosts and networks. So, make sure to connect securely and use best practices when implementing Ansible Playbooks.

Five, Playbook practical operation

        Here are the procedures for using the copy and user modules to perform playbook operations.

(1) Use the copy module to copy files

Step1: Write Ansible Playbook file in YAML format

        The command format is as follows:

```
- name: copy file
  hosts: all
  tasks:
    - name: copy /etc/ansible/ansible.cfg
      copy:
        src: /etc/ansible/ansible.cfg
        dest: /tmp/ansible.cfg
        owner: upwen
        group: upwen
        mode: '0444'
```

In the above Playbook, the following parts are included:

parameter name Parameter role
name Playbook name or description
hosts Host or host group name to manage
tasks A task list containing one or more tasks

        In this Playbook, there is a task to copy the source file `/etc/ansible/ansible.cfg` to the target file `/tmp/ansible.cfg`. This task has the following parts:

parameter name Parameter role
name The name or description of the task
copy Indicates that the `copy` module is used to copy files
src Specify the source file to copy, here is `/etc/ansible/ansible.cfg`
dest Specify the target path to copy to, here is `/tmp/ansible.cfg`
owner specify file owner
group Specifies the group to which the file belongs
mode Specify file permissions

        When Ansible runs, it iterates through the specified host inventory (`hosts`), executing each task against each host. After the task completes, Ansible will output the results, including successful tasks and failed tasks.

Step2: Configure Ansible's host list 

        The host list for configuring ansible is as follows:

Step3: Verify the ansible version

        The purpose of this step is to confirm that ansible has been installed normally. If it is confirmed that ansible has been installed normally, it can be omitted. 

Step4: Execute the playbook script

        Use the ansbile-playbook copy.yml command to execute the script, and the effect after execution is as shown in the figure:

 (2) Use the user module of playbook to create users

 Step1: Write Ansible Playbook file in YAML format

        The command format is as follows:

```yaml
- name: create user
  hosts: node1
  tasks:
    - name: create lisi user
      user:
        name: lisi
        uid: '1600'
        comment: student lisi
        shell: /sbin/nologin
        state: present
```

In the above Playbook, the following parts are included:

parameter name Parameter role
name Playbook name or description
hosts Host or host group name to manage
tasks A task list containing one or more tasks

        In this Playbook, there is a task for creating user `lisi` on the host. This task has the following parts:

parameter name Parameter role
name The name or description of the task
user Indicates that the `user` module is used to manage users
name Specify the username to create, here is `lisi`
uid Specifies the UID of the user
comment Remarks for specific users
shell Specifies the user's login shell
state Specifies the status of the user, here `present` means the user exists

        When Ansible is running, execute each task on each host, creating user `lisi`. After tasks complete, Ansible outputs the results, including successful tasks and failed tasks.

        Note, make sure you have sufficient permissions to create users when running this playbook. In addition, other user attributes such as password, home directory, etc. can be modified as needed. To learn more about Ansible's `user` module, please refer to the official Ansible documentation.

 Step2: Configure Ansible's host list 

        The host list for configuring ansible is as follows:

Step3: Verify the ansible version

        The purpose of this step is to confirm that ansible has been installed normally. If it is confirmed that ansible has been installed normally, it can be omitted. 

Step4: Execute the playbook script

        Use the ansbile-playbook user.yml command to execute the script, and the effect after execution is as shown in the figure:

 

Guess you like

Origin blog.csdn.net/as12138/article/details/131428985