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 IntroductionThe 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. Module parameters:
Basic usage examplesUpload 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: Command run: Example of ad-hoc syntax: 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 distributionIn 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:
Dynamic Path Distribution Playbook example: **hosts**** File Example:** Command run: result: The hosts in each environment receive the corresponding configuration files:
Replace the target file contentWhen 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: Command run: result: The content of the target host /etc/app/config.txt is as follows:
copy module vs template moduleAlthough 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. SummarizeThe 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! |
Have you changed your phone to 5G? My 4G is enoug...
What is VPC Virtual Private Cloud (VPC) is a priv...
A long time ago, there was a city called Paradis...
GigsGigsCloud has launched a new VPS in the Los A...
"You only need to input the flowchart, and t...
F5 recently announced the launch of the F5 Applic...
FTP Principle and Configuration FTP is a protocol...
At the 2021 Mobile World Congress, Liu Liehong, V...
Tudcloud has launched this month's regular pr...
5G technology has been around for more than four ...
EtherNetservers is a foreign hosting company foun...
[51CTO.com original article] As a representative ...
spinservers has released a promotion for the upco...
Another favorable policy for 5G has emerged. Rece...
Huawei has always been an active promoter and pra...