ansible batch deployment tool

1.ansibel introduction

1. Ansible is a newly emerging automated operation and maintenance tool. It is developed based on Python and combines the advantages of many operation and maintenance tools (puppet, cfengine, chef, func, fabric) to realize batch system configuration, batch program deployment, batch operation commands, etc. Features.
Insert picture description here
2. Features of Ansible

  • Ansible uses an agentless method to manage machines. Because Ansible's communication is implemented through openssh, you don't need to consider how to remotely upgrade the agent of the managed machine! As long as you can ensure that openssh can communicate normally! Because almost all Linux platforms now come with openssh, Ansible does not need to consider installing client software on remote machines during the deployment phase! This greatly reduces the pre-deployment work!
  • Ansible has a wealth of modules that you can use directly. Of course, there are many excellent developers in the Ansible community who are contributing new modules, so you will always find a module that suits you. Of course, you can also develop your own modules!
    Ansible is developed using python, so the secondary development and module development cost of Ansible is relatively low

3. Ansible's workflow

Insert picture description here

2.ansibel installation

yum search ansible
yum -y install centos-release-ansible-29.noarch
yum -y install ansible

3. Manifest file

  • Inventory (inventory)
    A list of managed nodes. Ansible needs to add the node to the manifest file before managing a node! Manifest files are sometimes referred to as "host manifest files." The manifest file can specify information for each managed node, such as IP address, port and other information! You can also divide the hosts into host groups for management!

Add IP

vim /etc/ansible/hosts
[EFK]
192.168.59.130
192.168.59.131
192.168.59.132

Use the first defined list file to execute commands in batches

ansible-playbook EFK -m ping

Insert picture description here
The above ping fails because it does not implement ssh to avoid password

3. Commonly used ansible modules

ping:

  • The ping module is used to check whether the target host is online
  • Example: ansible webserver -m ping

yum:

  • The yum module is used to install the software package using the yum command on the Centos system
  • Options:
  • name: Specify the name of the installation package
  • state: latest install the latest version present default install installed install absent uninstall
    removed uninstall
  • 例子:ansible webservers -m yum -a ‘name=httpd state=latest’
    command
  • The command module is used to execute system commands, but does not support special symbols under the shell such as: | &&, etc.
  • Example: ansible webservers -m command -a'echo 李想'

shell

  • The use of shell module and command module is basically the same, but it can support special shell symbols, such as: | && etc.
  • 例子:ansible webservers -m shell -a “cd /opt/ && touch lixiang”
    service
  • The service module is used to manage the startup, shutdown, restart and reload of services on centos
  • Options:
  • name: Service name
  • state: started (started) stopped (stopped) restarted (restarted) reloaded (reloaded)
  • enabled: The default is no, set the service to self-start after boot

file

  • The file module is used to create files, directories, and link files
  • Options:
  • group: Define the group of files/directories
  • mode: defines the permissions of the file/directory
  • owner: defines the owner of the file/directory
  • path: required option, defines the path of the file/directory
  • recurse: recursively set file attributes, only valid for directories
  • src: The path of the source file to be linked, only used when state=link
  • dest: the path to be linked to, only used when state=link
  • state:
  • directory: If the directory does not exist, create a directory
  • file: Even if the file does not exist, it will not be created
  • link: create a soft link
  • hard: Create a hard link
  • touch: If the file does not exist, a new file will be created, if the file or directory already exists, its last modification time will be updated
  • absent: delete directories, files or unlink files

user

  • The user module is used to create users
  • Options:
  • home: Specify the created home directory
  • groups: Specify user groups
  • uid: Specify UID
  • password: Set the password, the password must be cipher text
    Openssl passwd
  • name: the name of the created user
  • createhome: Whether to create a home directory (yes/no)
  • state: Whether to create or delete. (Present, absent), the default is to create
  • shell: Specify the shell environment for user login
  • remove: delete the user's home directory, the default is no

group

  • group is used to create user groups
  • Options
  • gid: Specifies the gid used.
  • name: Specify the user name.
  • state: Whether to create or delete. (Present, absent)

copy

  • The copy module is used to copy files to the target host
  • Options:
  • src: the relative path or relative path of the file on the management host
  • dest: Copy the file to the path of the target host
  • backup: Whether to back up the file with the same name on the target host, the default is no
  • mode: authorization
  • directory_mode: recursive authorization
  • 例子:
    ansible webservers -m copy -a ‘src=/root/nginx.sh dest=/opt/’
    unarchive
  • The unarchive module is used to decompress files
  • Options:
  • copy: Whether to copy the file to the remote host before decompressing the file, the default is yes. If no, the compressed package must exist on the target host
  • creates: Specify a file name, when the file exists, the decompression command will not be executed
  • dest: A path on the remote host, that is, the absolute path where the file is decompressed.
  • group: The group of the decompressed directory or file
  • mode: file permissions after decompression
  • src: If copy is yes, you need to specify the source path of the compressed file
  • owner: the owner of the file or directory after decompression
  • 例子:
    ansible webservers -m unarchive -a ‘src=/root/nginx.tar.gz dest=/opt/ group=www ower=www mode=777 ’
    get_url
  • get_url module, this module is mainly used to download files from http, ftp, https servers (similar to wget
  • Options:
  • url: Specify the URL address of the file to be downloaded
  • 例子:
    ansible webservers -m get_url -a ‘url= http://nginx.org/download/nginx-1.15.7.tar.gz dest=/root/’
    synchronize
  • Use rsync to synchronize files and push the master directory to the directory of the specified node. To use this module, you need to install rsync first
  • delete: delete a file that does not exist, delete=yes makes the content on both sides the same (that is, the pusher is the main one), the default is no
  • src: The path on the source host to be synchronized to the destination; the path can be absolute or relative. If the path ends with "/", only the contents of the directory will be copied. If the path is not ended with "/", the entire contents including the directory will be copied.
  • dest: The path on the destination host that will be synchronized with the source; the path can be absolute or relative.
  • dest_port: The port on the default directory host, the default is 22, and the ssh protocol is used.
  • mode: push or pull, the default push is generally used to upload files from the local machine to the remote host, and the pull mode is used to fetch files from the remote host.
  • rsync_opts: Specify other rsync options by passing an array.

fetch

  • The fetch module is used to obtain files from remote machines and store them locally in a file tree organized by hostname.
  • Options:
  • src: The file to be obtained on the remote system. This must be a file, not a directory. Subsequent versions may support recursive extraction.
  • dest: directory
    setup to save files
  • The setup module is used to collect some basic information of the remote host.
  • Options:
  • filter parameter: used for conditional filtering. If set, only information that matches the filter conditions will be returned.
  • Commonly used filter keywords:
  • ansible_all_ipv4_addresses: Only display ipv4 information
  • ansible_devices: Only display disk device information
  • ansible_distribution: display what system it is, for example: centos, suse, etc.
  • ansible_distribution_major_version: shows the major version of the system
  • ansible_distribution_version: display only the system version
  • ansible_machine: display the system type, for example: 32-bit or 64-bit
  • ansible_eth0: Only display information about eth0
  • ansible_hostname: display only the host name
  • ansible_kernel: Only display the kernel version
  • ansible_lvm: display information about lvm
  • ansible_memtotal_mb: Display the total memory of the system
  • ansible_memfree_mb: display available system memory
  • ansible_memory_mb: Show memory in detail
  • ansible_swaptotal_mb: Display the total swap memory
  • ansible_swapfree_mb: Show the free memory of swap memory
  • ansible_mounts: display system disk mount
  • ansible_processor: display the number of cpu (specifically display the model of each cpu)
  • ansible_processor_vcpus: Display the number of CPUs (only display the total number)
  • ansible_python_version: display python version
  • Example:
    Get the ipv4 address of the target host
    ansible webservers -m setup -a'filter=ansible_all_ipv4_addresses'

Guess you like

Origin blog.csdn.net/APPLEaaq/article/details/109062535