In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
Editor to share with you what is an Ansible script, I believe most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article, let's go to understand it!
The script playbook is more powerful than the peer-to-peer command mode, and it's completely different.
It uses / usr/bin/ansible-playbook binaries and provides rich features to make complex tasks easier.
Scripts are very useful if you want to run a task frequently. It is also useful if you want to perform multiple tasks on a server group.
The script is written in YAML. YAML represents a markup language that is easier to read and write than other common data formats such as XML or JSON.
The following flow chart of the Ansible script will tell you the details of its structure.
Understand the terminology of Ansible screenplay Control Node Control node
The machine installed by Ansible, which is responsible for managing client nodes
Controlled node Managed node
The list of hosts managed by the control node
Script playbook
A script file contains a set of automation tasks
Host list Inventory
This file contains information about the server you are managing
Task Task
Every play has a lot of tasks. Tasks are executed on a specified machine in turn (one or more hosts).
Module Module
A module is a unit of code that collects information from a client node
Role Role
A role is a way to automatically load variable files, tasks, and handlers based on a known file structure
Action Play
Each script contains a large number of actions, and each action performs a specific automation from beginning to end.
Handler Handler
It can help you reduce the restart tasks in the script. The handler task list is actually no different from regular tasks, and the change is notified by the notifier. If the handler does not receive any notification, it will not work.
What is the basic script?
Here is a template for a script:
-[YAML file should start with three dashes]-name: [script description] hosts: group [add host or host group] become: true [if you want to run the task as root Then mark it] tasks: [what actions do you want to perform under the task]-name: [enter module options] module: [enter modules to be executed] module_options-1: value [input module options] module_options-2: value. Module_options-N: how value understands the output of Ansible
The output of an Ansible screenplay comes in four colors. Here's what it means:
Green: ok represents success, the associated task data already exists and has been configured as needed.
Yellow: the data specified by changed has been updated or modified according to the needs of the task.
Red: if FAILED has any problems with the task, it will return a failure message, it could be anything, and you need to fix it accordingly.
White: indicates that there are multiple parameters.
To do this, create a script catalog and put them all in the same place.
$sudo mkdir / etc/ansible/playbooks script-1: install an Apache Web server on a RHEL system
This sample script allows you to install an Apache Web server on a specified target machine:
Sudo nano / etc/ansible/playbooks/apache.yml-hosts: web become: yes name: "Install and Configure Apache Web server" tasks:-name: "Install Apache Web Server" yum: name: httpd state: latest-name: "Ensure Apache Web Server is Running" service: name: httpd state: started$ ansible-playbook apache1.yml
How to understand the execution of plays in Ansible
Use the following command to view syntax errors. If no errors are found, it only displays the script file name. If it detects any errors, you will get an error as shown below, but the content may vary depending on your input file.
$ansible-playbook apache1.yml-syntax-check ERROR! Syntax Error while loading YAML. Found a tab character that violate indentationThe error appears to be in'/ etc/ansible/playbooks/apache1.yml': line 10, column 1, but maybe elsewhere in the file depending on the exact syntax problem.The offending line appears to be: state: latest^ hereThere appears to be a tab character at the start of the line. YAML does not use tabs for formatting. Tabs should be replaced with spaces.For example:-name: update tooling vars: version: 1.2.all spaces here ^-there is a tab there.Should be written as:-- name: update tooling vars: version: 1.2.mom ^-all spaces here.
Alternatively, you can use this URL YAML Lint to check the content of the Ansible script online.
Execute the following command to perform the walkthrough. When you run a script with the-check option, it doesn't make any changes to the remote machine. Instead, it will tell you what changes it is going to make but not really implement them.
$ansible-playbook apache.yml-- check PLAY [Install and Configure Apache Webserver] * TASK [Gathering Facts] * * * * ok: [node2.2g.lab] ok: [node1.2g.lab] TASK [Install Apache Web Server] * * * changed: [node2.2g.lab] changed: [node1.2g.lab] TASK [Ensure Apache Web Server is Running] * * * * changed: [node1.2g.lab] changed: [node2.2g.lab] PLAY RECAP * * * node1.2g.lab: ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0node2.2g.lab: ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
If you want to know the details of the ansible script implementation, use the-vv option, which shows how to collect this information.
$ansible-playbook apache.yml-- check-vv ansible-playbook 2.9.2 config file = / etc/ansible/ansible.cfg configured module search path = ['/ home/daygeek/.ansible/plugins/modules','/ usr/share/ansible/plugins/modules'] ansible python module location = / usr/lib/python3.8/site-packages/ansible executable location = / usr/bin/ansible-playbook python version = 3.8.1 (default, Jan 8 2020 23:09:20) [GCC 9.2.0] Using / etc/ansible/ansible.cfg as config file PLAYBOOK: apache.yml * * 1 plays in apache.yml PLAY [Install and Configure Apache Webserver] * TASK [Gathering Facts] * * task path: / etc/ansible/playbooks/apache.yml:2ok: [node2.2g.lab] ok: [node1. 2g.lab] META: ran handlers TASK [Install Apache Web Server] * task path: / etc/ansible/playbooks/apache.yml: 6changed: [node2.2g.lab] = > {"changed": true "msg": "Check mode: No changes made, but would have if not in check mode", "rc": 0, "results": ["Installed: httpd"]} changed: [node1.2g.lab] = > {"changed": true, "changes": {"installed": ["httpd"], "updated": []}, "msg": "," obsoletes ": {" urw-fonts ": {" dist ":" noarch " "repo": "@ anaconda", "version": "2.4-16.el7"}}, "rc": 0 "results": []} TASK [Ensure Apache Web Server is Running] * task path: / etc/ansible/playbooks/apache.yml:10changed: [node1.2g.lab] = > {"changed": true "msg": "Service httpd not found on host, assuming it will exist on full run"} changed: [node2.2g.lab] = > {"changed": true, "msg": "Service httpd not found on host Assuming it will exist on full run "} META: ran handlersMETA: ran handlers PLAY RECAP * * node1.2g.lab: ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0node2.2g.lab: ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 script-2: install Apache Web server on Ubuntu system
This sample script allows you to install an Apache Web server on a specified target node.
Sudo nano / etc/ansible/playbooks/apache-ubuntu.yml-hosts: web become: yes name: "Install and Configure Apache Web Server" tasks:-name: "Install Apache Web Server" yum: name: apache2 state: latest-name: "Start the Apache Web Server" service: name: apaceh3 state: started-name: "Enable mod_rewrite module" apache2_module: Name: rewrite state: present notify:-start apache handlers:-name: "Ensure Apache Web Server is Running" service: name: apache2 state: restarted enabled: yes script-3: list of installation packages on Red Hat system
This sample script allows you to install the package on the specified target node.
Method-1:
$sudo nano / etc/ansible/playbooks/packages-redhat.yml-hosts: web become: yes name: "Install a List of Packages on Red Hat Based System" tasks:-name: "Installing a list of packages" yum: name:-curl-httpd-nano-htop
Method-2:
$sudo nano / etc/ansible/playbooks/packages-redhat-1.yml-hosts: web become: yes name: "Install a List of Packages on Red Hat Based System" tasks:-name: "Installing a list of packages" yum: name= {{item}} state=latest with_items:-curl-httpd-nano-htop
Method-3: use array variables
$sudo nano / etc/ansible/playbooks/packages-redhat-2.yml-hosts: web become: yes name: "Install a List of Packages on Red Hat Based System" vars: packages: ['curl',' git', 'htop'] tasks:-name: Install a list of packages yum: name= {{item}} state=latest with_items: "{{packages}}" script-4: install updates on Linux systems
This sample script allows you to install updates on Red Hat or Debian-based Linux systems.
$sudo nano / etc/ansible/playbooks/security-update.yml-hosts: web become: yes name: "Install Security Update" tasks:-name: "Installing Security Update on Red Hat Based System" yum: name=* update_cache=yes security=yes state=latest when: ansible_facts ['distribution'] = = "CentOS"-name: "Installing Security Update on Ubuntu Based System" apt: upgrade=dist update_cache=yes when: ansible_facts [' distribution'] = "Ubuntu "the above is all the content of the article" what is an Ansible script? " 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.