Home  >  Article  >  Operation and Maintenance  >  How to configure automated deployment tools (such as Ansible) on Linux

How to configure automated deployment tools (such as Ansible) on Linux

王林
王林Original
2023-07-07 17:37:401535browse

How to configure automated deployment tools (such as Ansible) on Linux

Introduction:
In the process of software development and operation and maintenance, we often encounter the need to deploy applications to multiple servers. Condition. Manual deployment is undoubtedly inefficient and error-prone, so configuring an automated deployment tool is essential. This article will introduce how to configure Ansible, a commonly used automated deployment tool, on Linux to achieve fast and reliable application deployment.

1. Install Ansible

  1. Open the terminal and use the following command to install Ansible:

    sudo apt-get update
    sudo apt-get install ansible
  2. After the installation is complete, you can The following command verifies whether the installation is successful:

    ansible --version

2. Configure Ansible

  1. ##Open the terminal and use the following command to edit the Ansible configuration file

    ansible.cfg

    sudo nano /etc/ansible/ansible.cfg

  2. You can set some common configuration items in the configuration file, such as setting the default host inventory file path, remote user, private key file, etc. The following is a sample configuration file:

    [defaults]
    inventory = /etc/ansible/hosts
    remote_user = your_remote_user
    private_key_file = /path/to/your/private/key

3. Configure the host inventory file

  1. Create a new host inventory file, for example

    hosts, and use the following command to edit the file:

    sudo nano /etc/ansible/hosts

  2. In the host list file, you can define different host groups and hosts, as well as host-related configuration information. The following is a sample host inventory file:

    [web]
    webserver1 ansible_host=192.168.0.1
    webserver2 ansible_host=192.168.0.2
    
    [database]
    dbserver1 ansible_host=192.168.0.3
    dbserver2 ansible_host=192.168.0.4

4. Write Ansible Playbook

  1. Create a new Ansible Playbook file, for example

    deploy.yml, and edit the file using the following command:

    sudo nano deploy.yml

  2. In the Playbook file, you can define a series of tasks (tasks) for performing operations on the remote host . The following is a sample Playbook file:

    - name: Deploy application
      hosts: web
      tasks:
        - name: Install dependencies
          apt:
            name: "{{ item }}"
            state: present
          with_items:
            - nginx
            - python3
    
        - name: Copy application files
          copy:
            src: /path/to/your/application/files
            dest: /opt/application
            owner: your_remote_user
            group: your_remote_group

5. Run Ansible Playbook

  1. In the terminal, use the following command to run Ansible Playbook:

    ansible-playbook /path/to/your/deploy.yml

  2. Ansible will automatically connect to the target host and perform the corresponding operations according to the tasks defined in the Playbook file.
Conclusion:

By configuring and using Ansible, we can easily automate the deployment of applications on Linux. Ansible provides rich functions and flexible configuration options, making application deployment more efficient and reliable, and bringing convenience to our software development and operation and maintenance work. I hope this article can help readers quickly get started configuring and using Ansible.

The above is the detailed content of How to configure automated deployment tools (such as Ansible) on Linux. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn