Double your O&M efficiency! What you need to know about the Ansible Copy module

Double your O&M efficiency! What you need to know about the Ansible Copy module

In automated operation and maintenance, file distribution is a high-frequency demand scenario, such as uploading configuration files, distributing scripts, etc. Ansible provides a powerful copy module to help us quickly complete the file distribution task. This article will combine actual cases to explain the usage of the copy module in detail to help you master it in production.

Copy Module Introduction

The copy module is used to upload files or directories from the Ansible control node to the target host. It supports functions such as file permission settings and content replacement, and is a powerful tool for file management.

 ansible <pattern> -m copy \ -a "src=<source_path> dest=<destination_path> [other options]"

Module parameters:

  • <pattern>: specifies the host or host group to be operated, such as all, webservers.
  • -m copy: Specifies to use the copy module.
  • src: source file path, which must be a file on the control node.
  • dest: Destination path, specifies the location where the file is stored on the target host.
  • owner: The file owner.
  • group: The group to which the file belongs.
  • mode: file permissions.
  • content: directly specify the file content.

Basic usage examples

Upload files to a remote host

In the /etc/ansible/playbooks/ directory of the control node, there is a config.txt file that needs to be distributed to the /etc/app/ directory of all target hosts.

Playbook syntax example:

 - name:使用copy模块上传文件hosts:test tasks: -name:上传配置文件到目标主机ansible.builtin.copy: src:/etc/ansible/playbooks/config.txt dest:/home/config.txt owner:root group:root mode:'0644'

Command run:

 ansible-playbook -i hosts copy_example.yml

Example of ad-hoc syntax:

 ansible test -m copy \ -a "src=/etc/ansible/playbooks/config.txt dest=/home/config.txt owner=root group=root mode=0664"

result:

The file config.txt is successfully uploaded to the /etc/app/ directory of the target host, with the permissions set to 0644, and the owner and group set to root.

Multi-environment file distribution

In a production environment, different configuration files need to be distributed to development, test, and production environments. The configuration files are stored in different directories on the control node according to the environment name:

  • /etc/ansible/configs/dev/config.txt
  • /etc/ansible/configs/test/config.txt
  • /etc/ansible/configs/prod/config.txt

Dynamic Path Distribution

Playbook example:

 - name:多环境配置文件分发hosts:all vars: env:"{{ inventory_hostname | regex_replace('(.*)-(.*)', '\\2') }}"# 提取环境名tasks: -name:根据环境分发配置文件ansible.builtin.copy: src:"/etc/ansible/configs/{{ env }}/config.txt" dest:"/etc/app/config.txt" owner:root group:root mode:'0644'

**hosts**** File Example:**

 [dev] dev-server-01 ansible_host=192.168.1.101 [test] test-server-01 ansible_host=192.168.1.102 [prod] prod-server-01 ansible_host=192.168.1.103

Command run:

 ansible-playbook -i hosts multi_env_copy.yml

result:

The hosts in each environment receive the corresponding configuration files:

  • dev-server-01: Received dev/config.txt.
  • test-server-01: Received test/config.txt.
  • prod-server-01: Received prod/config.txt.

Replace the target file content

When uploading a file, you need to dynamically replace the placeholders in it, for example, with a host name or IP address.

Using the content parameter

Playbook example:

 - name:动态生成配置文件hosts:all tasks: -name:根据模板生成配置文件ansible.builtin.copy: dest:/etc/app/config.txt content:|主机名:{{ inventory_hostname }} IP地址:{{ ansible_default_ipv4.address }} owner:root group:root mode:'0644'

Command run:

 ansible-playbook -i hosts dynamic_content.yml

result:

The content of the target host /etc/app/config.txt is as follows:

主机名:dev-server-01 IP地址:192.168.1.101

copy module vs template module

Although the copy module is simple and efficient, it needs to be explicitly defined through the content parameter when processing dynamic content. The template module is designed for dynamic content and is more flexible when combined with Jinja2 templates.

Summarize

The copy module is a basic tool for Ansible file distribution. Whether it is single file upload, multi-environment management, or dynamic content generation, it can quickly meet the needs. In actual operation and maintenance, choosing the appropriate method according to the scenario and using it with other modules can greatly improve the efficiency of automation. Try the copy module in your work!

<<:  Recommend an operation and maintenance tool: a tool for collecting AD domain environment information

>>:  Interviewer: What are the underlying data types of Redis? Why is Redis so fast? Why did Redis introduce multithreading? What is the implementation mechanism of Redis multithreading?

Recommend

Practical VPC Network Planning on the Cloud

What is VPC Virtual Private Cloud (VPC) is a priv...

GigsGigsCloud: $26/year KVM-1GB/15G SSD/2TB/Los Angeles Data Center

GigsGigsCloud has launched a new VPS in the Los A...

Five pictures to solve FTP

FTP Principle and Configuration FTP is a protocol...

Global 5G patent rankings: Huawei ranks first, ZTE and OPPO perform well

5G technology has been around for more than four ...

Huawei: Realizing a truly bright future for the Internet of Things

Huawei has always been an active promoter and pra...