Understand ansible architecture and working principles
Sep 02, 2024 pm 03:59 PMAnsible is a model-driven configuration manager that supports multi-node publishing and remote task execution. By default, SSH is used for remote connections. There is no need to install additional software on managed nodes and it can be extended using a variety of programming languages.
The picture above shows the basic architecture of ansible. From the picture above, you can understand that it consists of the following parts:
- Core: ansible
- Core Modules: These are the modules that come with ansible
- Extension modules (Custom Modules): If the core module is not enough to complete a certain function, you can add extension modules
- Plugins: Complete the supplement of module functions
- Playbooks: Ansible's task configuration file, which defines multiple tasks in the playbook and is automatically executed by ansible
- Connectior Plugins: ansible connects to each host based on connection plug-ins. Although ansible uses ssh to connect to each host, it also supports other connection methods, so a connection plug-in is required
- Host Inventory: Define the hosts managed by ansible
The above are two ansible working principle diagrams found on the Internet. Both diagrams are basically expansions based on the architecture diagram. You can understand from the picture above:
1. The management terminal supports three ways to connect to the managed terminal: local, ssh, and zeromq. The default is to use the ssh-based connection---this part corresponds to the connection module in the basic architecture diagram;
2. Host Inventory (host group) can be classified according to application type, etc. The management node implements corresponding operations through various modules - a single module, batch execution of a single command, we can call it ad-hoc ;
3. The management node can use playbooks to implement a collection of multiple tasks to implement a type of functions, such as the installation and deployment of web services, batch backup of database servers, etc. We can simply understand playbooks as configuration files that the system operates by combining multiple ad-hoc operations.
After installing ansible, we found that ansible provides us with seven instructions in total: ansible, ansible-doc, ansible-galaxy, ansible-lint, ansible-playbook, ansible-pull, ansible-vault. Here we only look at the usage part, and the detailed part can be obtained through the "command -h" method.
1.[root@localhost ~]# ansible -h 2.Usage: ansible [options]
Ansible is the core part of the command, which is mainly used to execute ad-hoc commands, that is, a single command. By default, the host and options parts need to be followed. When the module is not specified by default, the command module is used. Such as:
1.[root@361way.com ~]# ansible 192.168.0.102 -a 'date' 2192.168.0.102 | success | rc=0 >> 3Tue May 12 22:57:24 CST 2015
However, the default module can be modified in ansible.cfg. The parameters under the ansible command are explained as follows:
- Parameters:
- -a 'Arguments', --args='Arguments' command line parameters
- -m NAME, --module-name=NAME The name of the execution module. The command module is used by default, so if you only execute a single command, you do not need the -m parameter
- -i PATH, --inventory=PATH specifies the path to the inventory host file, the default is /etc/ansible/hosts.
- -u Username, --user=Username execution user, use this remote username instead of the current user
- -U --sud-user=SUDO_User Which user to sudo to, the default is root
- -k --ask-pass login password, prompt for SSH password instead of assuming key-based authentication
- -K --ask-sudo-pass prompts for password use sudo
- -s --sudo sudo run
- -S --su Use su command
- -l --list displays all supported modules
- -s --snippet specifies the module to display script snippets
- -f --forks=NUM Number of parallel tasks. NUM is specified as an integer, the default is 5. #ansible testhosts -a "/sbin/reboot" -f 10 Restart all machines in the testhosts group, 10 machines at a time
- --private-key=PRIVATE_KEY_FILE private key path, use this file to verify the connection
- -v --verbose details
- all executes for all hosts defined by hosts
- -M MODULE_PATH, --module-path=MODULE_PATH The path of the module to be executed, the default is /usr/share/ansible/
- --list-hosts only prints which hosts will execute this playbook file, not actually executes the playbook file
- -o --one-line compressed output, summarized output. Try to output everything on one line.
- -t Directory, --tree=Directory Save the contents in this output directory, saving the results in a file on each host.
- -B background running timeout
- -P Investigate background program time
- -T Seconds, --timeout=Seconds time in seconds
- -P NUM, --poll=NUM Poll background work every few seconds. Required - b
- -c Connection, --connection=Connection connection type to use. Possible options are paramiko(SSH), SSH and local. Local is mainly used for crontab or startup.
- --tags=TAGS Only execute the task with the specified tags Example: ansible-playbook test.yml --tags=copy Only execute the task with the tag copy
- --list-hosts only prints which hosts will execute this playbook file, not actually executes the playbook file
- --list-tasks List all tasks that will be executed
- -C, --check just tests what will be changed and will not actually execute it; instead, it tries to predict some possible changes
- --syntax-check Perform syntax check of the script, but do not execute it
- -l SUBSET, --limit=SUBSET further limit the selected host/group mode --limit=192.168.0.15 Only execute this ip
- --skip-tags=SKIP_TAGS Only run plays and tasks with tags that do not match these values ??--skip-tags=copy_start
- -e EXTRA_VARS, --extra-vars=EXTRA_VARS Extra variables set as key=value or YAML/JSON
- #cat update.yml
- ---
- - hosts: {{ hosts }}
- remote_user: {{ user }}
- ............
- #ansible-playbook update.yml --extra-vars "hosts=vipers user=admin" Pass {{hosts}}, {{user}} variables, hosts can be ip or group name
- -l,--limit Execute tasks on the specified host/group--limit=192.168.0.10, 192.168.0.11 or -l 192.168.0.10, 192.168.0.11 Execute tasks only on these 2 IPs
# ansible-doc -h Usage: ansible-doc [options] [module...]
該指令用于查看模塊信息,常用參數(shù)有兩個(gè)-l 和 -s ,具體如下:
- //列出所有已安裝的模塊
- # ansible-doc -l
- //查看具體某模塊的用法,這里如查看command模塊
- # ansible-doc -s command
# ansible-galaxy -h Usage: ansible-galaxy [init|info|install|list|remove] [--help] [options] ...
ansible-galaxy 指令用于方便的從https://galaxy.ansible.com/ 站點(diǎn)下載第三方擴(kuò)展模塊,我們可以形象的理解其類似于centos下的yum、python下的pip或easy_install 。如下示例:
[root@localhost ~]# ansible-galaxy install aeriscloud.docker - downloading role 'docker', owned by aeriscloud - downloading role from https://github.com/AerisCloud/ansible-docker/archive/v1.0.0.tar.gz - extracting aeriscloud.docker to /etc/ansible/roles/aeriscloud.docker - aeriscloud.docker was installed successfully
這個(gè)安裝了一個(gè)aeriscloud.docker組件,前面aeriscloud是galaxy上創(chuàng)建該模塊的用戶名,后面對(duì)應(yīng)的是其模塊。在實(shí)際應(yīng)用中也可以指定txt或yml 文件進(jìn)行多個(gè)組件的下載安裝。這部分可以參看官方文檔。
ansible-lint是對(duì)playbook的語(yǔ)法進(jìn)行檢查的一個(gè)工具。用法是ansible-lint playbook.yml 。
該指令是使用最多的指令,其通過(guò)讀取playbook 文件后,執(zhí)行相應(yīng)的動(dòng)作,這個(gè)后面會(huì)做為一個(gè)重點(diǎn)來(lái)講。
該指令使用需要談到ansible的另一種模式---pull 模式,這和我們平常經(jīng)常用的push模式剛好相反,其適用于以下場(chǎng)景:你有數(shù)量巨大的機(jī)器需要配置,即使使用非常高的線程還是要花費(fèi)很多時(shí)間;你要在一個(gè)沒(méi)有網(wǎng)絡(luò)連接的機(jī)器上運(yùn)行Anisble,比如在啟動(dòng)之后安裝。這部分也會(huì)單獨(dú)做一節(jié)來(lái)講。
ansible-vault主要應(yīng)用于配置文件中含有敏感信息,又不希望他能被人看到,vault可以幫你加密/解密這個(gè)配置文件,屬高級(jí)用法。主要對(duì)于playbooks里比如涉及到配置密碼或其他變量時(shí),可以通過(guò)該指令加密,這樣我們通過(guò)cat看到的會(huì)是一個(gè)密碼串類的文件,編輯的時(shí)候需要輸入事先設(shè)定的密碼才能打開。這種playbook文件在執(zhí)行時(shí),需要加上 --ask-vault-pass參數(shù),同樣需要輸入密碼后才能正常執(zhí)行。具體該部分可以參查官方博客。
注:上面七個(gè)指令,用的最多的只有兩個(gè)ansible 和ansible-playbook ,這兩個(gè)一定要掌握,其他五個(gè)屬于拓展或高級(jí)部分。
The above is the detailed content of Understand ansible architecture and working principles. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Integrating Postman applications on CentOS can be achieved through a variety of methods. The following are the detailed steps and suggestions: Install Postman by downloading the installation package to download Postman's Linux version installation package: Visit Postman's official website and select the version suitable for Linux to download. Unzip the installation package: Use the following command to unzip the installation package to the specified directory, for example /opt: sudotar-xzfpostman-linux-x64-xx.xx.xx.tar.gz-C/opt Please note that "postman-linux-x64-xx.xx.xx.tar.gz" is replaced by the file name you actually downloaded. Create symbols

The main difference between Java and other programming languages ??is its cross-platform feature of "writing at once, running everywhere". 1. The syntax of Java is close to C, but it removes pointer operations that are prone to errors, making it suitable for large enterprise applications. 2. Compared with Python, Java has more advantages in performance and large-scale data processing. The cross-platform advantage of Java stems from the Java virtual machine (JVM), which can run the same bytecode on different platforms, simplifying development and deployment, but be careful to avoid using platform-specific APIs to maintain cross-platformity.

Setting the location of the interpreter in PyCharm can be achieved through the following steps: 1. Open PyCharm, click the "File" menu, and select "Settings" or "Preferences". 2. Find and click "Project:[Your Project Name]" and select "PythonInterpreter". 3. Click "AddInterpreter", select "SystemInterpreter", browse to the Python installation directory, select the Python executable file, and click "OK". When setting up the interpreter, you need to pay attention to path correctness, version compatibility and the use of the virtual environment to ensure the smooth operation of the project.

The steps to manually install the plug-in package in VSCode are: 1. Download the .vsix file of the plug-in; 2. Open VSCode and press Ctrl Shift P (Windows/Linux) or Cmd Shift P (Mac) to call up the command panel; 3. Enter and select Extensions:InstallfromVSIX..., then select .vsix file and install. Manually installing plug-ins provides a flexible way to install, especially when the network is restricted or the plug-in market is unavailable, but attention needs to be paid to file security and possible dependencies.

[Common Directory Description] Directory/bin stores binary executable files (ls, cat, mkdir, etc.), and common commands are generally here. /etc stores system management and configuration files/home stores all user files. The root directory of the user's home directory is the basis of the user's home directory. For example, the home directory of the user user is /home/user. You can use ~user to represent /usr to store system applications. The more important directory /usr/local Local system administrator software installation directory (install system-level applications). This is the largest directory, and almost all the applications and files to be used are in this directory. /usr/x11r6?Directory for storing x?window/usr/bin?Many

Understanding Nginx's configuration file path and initial settings is very important because it is the first step in optimizing and managing a web server. 1) The configuration file path is usually /etc/nginx/nginx.conf. The syntax can be found and tested using the nginx-t command. 2) The initial settings include global settings (such as user, worker_processes) and HTTP settings (such as include, log_format). These settings allow customization and extension according to requirements. Incorrect configuration may lead to performance issues and security vulnerabilities.

The installation and configuration of MySQL can be completed through the following steps: 1. Download the installation package suitable for the operating system from the official website. 2. Run the installer, select the "Developer Default" option and set the root user password. 3. After installation, configure environment variables to ensure that the bin directory of MySQL is in PATH. 4. When creating a user, follow the principle of minimum permissions and set a strong password. 5. Adjust the innodb_buffer_pool_size and max_connections parameters when optimizing performance. 6. Back up the database regularly and optimize query statements to improve performance.

Informix and MySQL are both popular relational database management systems. They perform well in Linux environments and are widely used. The following is a comparison and analysis of the two on the Linux platform: Installing and configuring Informix: Deploying Informix on Linux requires downloading the corresponding installation files, and then completing the installation and configuration process according to the official documentation. MySQL: The installation process of MySQL is relatively simple, and can be easily installed through system package management tools (such as apt or yum), and there are a large number of tutorials and community support on the network for reference. Performance Informix: Informix has excellent performance and
