Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to automate repeatable daily tasks by using Ansible

2025-04-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you how to use Ansible to automatically perform repeatable daily tasks, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's learn about it!

Improve productivity and avoid errors by automating repeatable daily tasks using Ansible.

If you hate performing repetitive tasks, then I have a suggestion for you to learn Ansible!

Ansible is a tool that can help you accomplish daily tasks more easily and faster, so that you can use your time more effectively, such as learning important new technologies. It is a good tool for system administrators because it can help you standardize and collaborate in daily activities, including:

Install, configure, and provision servers and applications

Update and upgrade the system regularly

Monitor, mitigate and troubleshoot problems.

Often, many of these basic daily tasks require manual steps, which can lead to inconsistencies and configuration drift, depending on individual skills. This may be acceptable in small-scale implementations because you manage a server and know what you are doing. But what happens when you manage hundreds or thousands of servers?

If you are not careful, these manual, repeatable tasks may cause delays and problems due to human errors that may affect the reputation of you and your organization.

This is the value of automation. Ansible is the perfect tool for automating these repeatable daily tasks.

Some of the reasons for automation are:

You want a consistent and stable environment.

You want to promote standardization.

You want to reduce downtime and reduce serious accidents so that you can enjoy life.

You want a beer instead of troubleshooting!

This article provides examples of day-to-day tasks that system administrators can use to automate Ansible. I put the scripts and roles in this article in the system administrator task repository on GitHub so that you can use them.

The structure of these plays is like this (my comments are preceded by = >).

[root@homebase 6_sysadmin_tasks] # tree-L 2. ├── ansible.cfg = = > the configuration file responsible for controlling the behavior of Ansible ├── ansible.log ├── inventory │ ├── group_vars │ ├── hosts = = > the manifest file │ └── host_vars ├── LICENSE ├── playbooks = > contains the directory │ ├── c that contains the scripts we will use in this article _ logs.yml │ ├── c_stats.yml │ ├── c_uptime.yml │ ├── inventory │ ├── r_cron.yml │ r_install.yml │ └── r_script.yml ├── README.md ├── roles = = > directory │ ├── check_logs │ ├── check_stats │ check that contains the roles we will use in this article _ uptime │ ├── install_cron │ ├── install_tool │ └── run_scr └── templates = > directory containing jinja templates ├── cron_output.txt.j2 ├── sar.txt.j2 └── scr_output.txt.j2

The list looks something like this:

[root@homebase 6_sysadmin_tasks] # cat inventory/ hosts [rhel8] master ansible_ssh_host=192.168.1.12workernode1 ansible_ssh_host=192.168.1.15 [rhel8:vars] ansible_user=ansible = = > Please update it with your ansible username

Here are five daily system management tasks that you can automate with Ansible.

1. Check the uptime of the server

You need to make sure that your server is running properly all the time. Organizations have enterprise monitoring tools to monitor the uptime of servers and applications, but automatic monitoring tools often fail and you need to log in to verify the status of a server. It takes a lot of time to manually verify the uptime of each server. The more servers you have, the longer it will take you. But with automation, this verification can be done in a few minutes.

Use check_uptime characters and c_uptime.yml screenplays:

[root@homebase 6_sysadmin_tasks] # ansible-playbook-I inventory/hosts playbooks/c_uptime.yml-kSSH password:PLAY [Check Uptime for Servers] * * TASK [check_uptime: Capture timestamp] * * *. Truncate.... PLAY RECAP * * master: ok=6 changed=4 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 workernode1: ok=6 changed=4 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 [root@homebase 6_sysadmin_tasks] #

The output of the script is as follows:

[root@homebase 6_sysadmin_tasks] # cat / var/tmp/uptime-master-20210221004417.txt--- Uptime for master-- -00:44:17 up 44 min 2 users, load average: 0.01, 0.09 0.09Murray-[root@homebase 6_sysadmin_tasks] # cat / var/tmp/uptime-workernode1-20210221184525.txtmuri- -Uptime for workernode1--- 18:45:26 up 44 min 2 users, load average: 0.01,0.01,0.00Murray-

With Ansible, you can get the status of multiple servers in a human-readable format with less effort, and the Jinja template allows you to adjust the output to your own needs. With more automation, you can run as planned and send output via email to achieve the purpose of the report.

2. Configure additional cron jobs

You need to update the server's scheduled jobs regularly according to infrastructure and application requirements. This seems like a trivial task, but it must be done correctly and continuously. Imagine how long it would take if you operated manually on hundreds of production servers. If you do something wrong, it will affect the production application, and if the scheduled jobs overlap, it will cause the application to shut down or affect server performance.

Use install_cron characters and r_cron.yml screenplays:

[root@homebase 6_sysadmin_tasks] # ansible-playbook-I inventory/hosts playbooks/r_cron.yml-kSSH password:PLAY [Install additional cron jobs for root] * * *. Truncate.... PLAY RECAP * * master: ok=10 changed=7 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 workernode1: ok=10 changed=7 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

Verify the results of the script:

[root@homebase 6_sysadmin_tasks] # ansible-I inventory/hosts all-m shell-a "crontab-l"-kSSH password:master | CHANGED | rc=0 > > 12 345 / usr/bin/ls / tmp#Ansible: Iotop Monitoring0 5 2 * / usr/sbin/iotop-b-n 1 > > / var/tmp/iotop.log 2 > > / var/tmp/iotop.errworkernode1 | CHANGED | rc=0 > > 1 2 3 45 / usr/bin/ls / tmp#Ansible: Iotop Monitoring0 5 2 * / usr/sbin/iotop-b-n 1 > > / var/tmp/iotop.log 2 > > / var/tmp/iotop.err

With Ansible, you can update crontab entries on all servers in a fast and consistent manner. You can also use a simple peer-to-peer Ansible command to report the status of the updated crontab to verify recent application changes.

3. Collect server statistics and sars

In order to diagnose server performance or application problems during regular troubleshooting, you need to collect system activity reports system activity reports (sars) and server statistics. In most cases, the server log contains very important information that developers or operations teams need to help solve specific problems that affect the entire environment.

The security team is very special in conducting investigations, and most of the time, they want to view the logs of multiple servers. You need to find an easy way to collect these documents. It would be better if you could delegate the collection task to them.

Accomplish this task through check_stats characters and c_stats.yml scripts:

$ansible-playbook-I inventory/hosts playbooks/c_stats.yml PLAY [Check Stats/sar for Servers] * * * TASK [check_stats: Get current date time] * * changed: [master] changed: [workernode1]. Truncate.... PLAY RECAP * * master: ok=5 changed=4 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 workernode1: ok=5 changed=4 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

The output looks like this:

$cat / tmp/sar-workernode1-20210221214056.txtMurray-sar output for workernode1 -- Linux 4.18.0-193.el8.x86_64 (node1) 21-02-21 _ x86 million 642 CPU) 21:39:30 LINUX RESTART (2 CPU)-4. Collect server logs

In addition to collecting server statistics and sars information, you also need to collect logs from time to time, especially if you need help investigating problems.

This is achieved through check_logs characters and r_cron.yml scripts:

$ansible-playbook-I inventory/hosts playbooks/c_logs.yml-kSSH password: PLAY [Check Logs for Servers] * * *. Truncate.... TASK [check_logs: Capture Timestamp] * * changed: [master] changed: [workernode1] PLAY RECAP * * master: ok=6 changed=4 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 workernode1: ok=6 changed=4 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

To confirm the output, open the file generated by the dump location. The log should look like this:

$cat / tmp/logs-workernode1-20210221214758.txt | more--- Logs gathered: / var/log/messages for workernode1-- -Feb 21 18:00:27 node1 kernel: Command line: BOOT_IMAGE= (hd0 Gpt2) / vmlinuz-4.18.0-193.el8.x86_64 root=/dev/mapper/rhel-root ro crashkernel=auto resume=/dev/mapper/rhel-swap rd.lvm.lv=rhel/root rd.lvm.lv=rhel/swap rhgb quietFeb 21 18:00:27 node1 kernel: Disabled fast string operationsFeb 21 18:00:27 node1 kernel: x86/fpu: Supporting XSAVE feature 0x001: 'x87 floating point registers'Feb 21 18:00:27 node1 kernel: x86/fpu: Supporting XSAVE feature 0x002:' SSE registers'Feb 21 18: 00:27 node1 kernel: x86/fpu: Supporting XSAVE feature 0x004: 'AVX registers'Feb 21 18:00:27 node1 kernel: x86/fpu: xstate_offset [2]: 576 Xstate_sizes [2]: 256Feb 21 18:00:27 node1 kernel: x86/fpu: Enabled xstate features 0x7, context size is 832 bytes, using 'compacted' format.5, install or remove software packages and software

You need to be able to install and update software and packages on your system continuously and quickly. Reduce the time required to install or update software packages and software to avoid unnecessary downtime for servers and applications.

Do this through install_tool characters and r_install.yml scripts:

$ansible-playbook-I inventory/hosts playbooks/r_install.yml-kSSH password:PLAY [Install additional tools/packages] * TASK [install_tool: Install specified Tools in the role vars] * ok: [master] = > (item=iotop) ok: [workernode1] = > (item=iotop) ok: [workernode1] = > (item=traceroute) ok: [master] = > (item=traceroute) PLAY RECAP * * master: ok=1 changed=0 unreachable=0 Failed=0 skipped=0 rescued=0 ignored=0 workernode1: ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

This example installs two specific packages and versions defined in the vars file. With Ansible Automation, you can install multiple packages or software faster than manual installation. You can also use the vars file to define the version of the package you want to install.

$cat roles/install_tool/vars/main.yml---# vars file for install_toolins_action: absentpackage_list:-iotop-0.6-16.el8.noarch-traceroute embraces automation

To be an effective system administrator, you need to accept automation to encourage standardization and collaboration within the team. Ansible enables you to do more in less time, so you can spend your time on more exciting projects instead of repetitive tasks such as managing your event and problem management processes.

The above is all the contents of the article "how to automate repeatable daily tasks by using Ansible". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report