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

Ansible of automation tools for operation and maintenance

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

Share

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

1 installation and getting started 1.1 ansible installation # ansible can be installed in a variety of ways, such as compiler installation, git installation, pip installation and so on. Yum installation is used here. This method requires the existing epel source [root@ansible ~] # yum install epel-release-y [root@ansible ~] # yum install ansible-y1.2 to confirm the installation of [root@ansible ~] # ansible-- versionansible 2.9.1 config file = / etc/ansible/ansible.cfg configured module search path = [uplink and rootlash. Ansible python module location ansible python module location usr/lib/python2.7/site-packages/ansible executable location = / usr/bin/ansible python version = 2.7.5 (default, Aug 7 2019, 00:51:29) [GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] 1.3 ansible related files 1.3.1 configuration file / etc/ansible/ansible.cfg main configuration file Configure the working characteristics of ansible / etc/ansible/hosts host list / directory of etc/ansible/roles/ storage role 1.3.2 ansible main configuration file description [defaults] # inventory = / etc/ansible/hosts # host list configuration file # library = / usr/share/my_modules/ # library file storage directory # remote_tmp = $HOME/.ansible/tmp # temporary py command file is stored in the remote host directory # local _ tmp = $HOME/.ansible/tmp # Local temporary command execution directory # forks = 5 # default concurrency # sudo_user = root # default sudo user # ask_sudo_pass = True# whether to ask the ssh password every time the ansible command is executed # ask_pass = True#remote_port = 22#host_key_checking = False # check the host_key of the corresponding server It is recommended to uncomment # log_path=/var/log/ansible.log # log file, and it is recommended to enable # module_name = command # default module, which can be modified to shell module 1.3.3 inventory host list. The main function of ansible is batch host operation. In order to easily use some of these hosts, you can group them in inventory file and name the default inventory file to / etc/ansible/hostsinventory file. There can be multiple hosts. And can also use Dynamic Inventory to dynamically generate 1.3.4 host manifest file format inventory file follows the INI file style, the characters in brackets for the group name. You can merge the same host into several different groups at the same time if the target host uses a non-default SSH port, you can also use a colon plus port number after the host name to indicate if the host name follows a similar naming pattern You can also use a list to identify each host [example]: ntp.aliyun.com [webservers] www1.abc.com: 2222www.w2.abc.com [dbservers] db1.abc.comdb2.abc.com [websrvs] www [1Vl100] .example.com [dbsrvs] db- [aRAF] .example.com [appsrvs] 10.0.0.1.4 ansible related tools / usr/bin/ansible # main program Temporary command execution tool / usr/bin/ansible-doc # View configuration documentation, module function view tool / usr/bin/ansible-galaxy # download / upload excellent code or Roles module official website platform / usr/bin/ansible-playbook # customize automation tasks Scripting tool / usr/bin/ansible-pull # remote command execution tool / usr/bin/ansible-vault # File encryption tool / usr/bin/ansible-console # execution tool that interacts with users based on Console interface the main way to manage using ansible is Ad-Hoc, that is, using the ansible command, mainly for temporary command usage scenarios Ansible-playbook is mainly used for long-term planned, large-scale project scenarios Need to have a previous planning process 1.4.1 ansible-doc function: used to display help information for each module syntax: ansible-doc [options] [module...]-l,-- list # list available modules-s -- snippet # displays the playbook snippet of the specified module [example]: # list all modules ansible-doc-l # View specified module help usage ansible-doc ping# View specified module help usage ansible-doc-s ping1.4.2 ansible function: through ssh protocol To implement configuration management, application deployment, task execution and other functions of remote hosts, it is recommended to configure key verification syntax before use: ansible [- m module_name] [- an args] option description:-- version # display version-m module # specify module, default to command-v # detailed procedure-vv-vvv for more details-- list-hosts # display host list Can be abbreviated-- list-k,-- ask-pass # prompt for ssh connection password, default Key authentication-C,-- check # check, and do not execute-T,-- timeout=TIMEOUT # timeout for command execution, default 10s-u,-- user=REMOTE_USER # user for remote execution-- b,-- become # instead of the old sudo switch-- become-user=USERNAME # specifies the runas user of sudo Default is root-K -- ask-become-pass # prompts for password 1.4.2.1 ansible host-pattern used to match the list of controlled hosts All: indicates all hosts in all Inventory [example]: # allansible all-m ping# *: wildcard ansible "*"-m pingansible 192.168.1.*-m ping# or relational ansible "websrvs:appsrvs"-m pingansible "192.168.1.11192.168 .1.2 "- m ping# logic and # host ansible" websrvs:&dbsrvs "- m ping# logic in websrvs group and in dbsrvs group not # in websrvs group But hosts that are not in the dbsrvs group # Note: here is the single quotation mark ansible 'websrvsansible'-m ping# synthesis logic ansible 'websrvsVV dbsrvs'-m ping# regular expression ansible "websrvs:&dbsrvs"-m pingansible "~ (db). *\ .ABC\ .com"-m ping1.4.2.2 ansible command execution procedure 1. Load your own configuration file default / etc/ansible/ansible.cfg2. Load your own corresponding module files, such as: command3. Generate the corresponding temporary py file through ansible, and transfer the file to the corresponding executor $HOME/.ansible/tmp/ansible-tmp- digital / XXX.PY file 4. Execute 5 for file + x. Execute and return result 6. Delete temporary py files Exit the execution status of 1.4.2.3 ansible [root@ansible ~] # grep-A 14'\ [colors\]'/ etc/ansible/ansible.cfg [colors] # highlight = white#verbose = blue#warn = bright purple#error = dark gray#deprecate = purple#skip = cyan#unreachable = red#ok = green#changed = yellow#diff_add = green#diff_remove = red#diff_lines = cyan Green: executed successfully and no change is required Operation yellow: execute successfully and change the target host red: execution failed 1.4.2.4 ansible use example # to zhangsan user to perform ping survival test ansible all-m ping-u zhangsan-k # to zhangsan sudo to root perform ping survival test ansible all-m ping-u zhangsan-k-b # to zhangsan sudo to lisi users perform ping survival test ansible all-m ping-u zhangsan-k-b-become-user=lisi# to Zhangsan sudo to root users execute lsansible all-m command-u zhangsan-a'ls / root'-b-become-user=root-k-K1.4.3 ansible-galaxy function: connect https://galaxy.ansible.com to download corresponding roles [example]: # list all installed galaxyansible-galaxy list# install galaxyansible-galaxy install geerlingguy.redis# delete galaxyansible-galaxy remove geerlingguy.redis1.4.4 ansible-pull function: push ansible command to remote Unlimited improvement in efficiency 1.4.5 ansible-playbook function: used to execute written playbook tasks [example]: ansible-playbook hello.ymlcat hello.yml---# hello world yml file- hosts: websrvs remote_user: root tasks:-name: hello world command: / usr/bin/wall hello world1.4.6 ansible-vault function: used to encrypt and decrypt yml files syntax: ansible-vault [create | decrypt | edit | encrypt | rekey | view] [example]: ansible-vault encrypt hello .yml # encrypt ansible-vault decrypt hello.yml # decrypt ansible-vault view hello.yml # View ansible-vault edit hello.yml # Edit encrypted file ansible-vault rekey hello.yml # modify password ansible-vault create new.yml # create new file 1.4.7 ansible-console function: interactive command execution Support tab completion, ansible 2.0 + new prompt format: host group performing user @ current operation (number of hosts in the current group) [f: concurrency] $common subcommand: set concurrency: forks n for example: forks 10 switch group: cd host group for example: cd web lists the current group host list: list lists all built-in commands:? Or help [example]: [root@ansible ansible] # ansible-consoleWelcome to the ansible console.Type help or? To list commands.root@all (3) [FRV 5] $list192.168.7.71192.168.7.72192.168.7.73root@all (3) [FRV 5] $cd websrvsroot@websrvs (3) [FRV 5] $list192.168.7.71192.168.7.72192.168.7.73root@websrvs (3) [FRV 5] $yum name=httpd state=present1.5 ansible Common Module reference: https://docs.ansible.com/ansible/latest/modules/ Modules_by_category.html1.5.1 command module function: execute commands on the controlled computer Default module of ansible, but ignore the-m option Note: the command of this module does not support $VARNAME

< >

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