CentOS-7 Rapid Deployment Ansible Automation Tool

Install the Ansible automation tool:

Check Python version

python -V

Check if there is ansible rpm package in the yum repository

yum list |grep ansible

insert image description here
Install ansible service

yum install -y ansible

insert image description here

Modify ansible configuration and host list hosts

1) Close the command prompt when using ansible to connect to the client for the first time:

sed -i "s@\#host_key_checking = False@host_key_checking = False@g" /etc/ansible/ansible.cfg

Specify the log path:

sed -i "s@\#log_path = \/var\/log\/ansible.log@log_path = \/var\/log\/ansible.log@g" /etc/ansible/ansible.cfg

2) Add all host IPs to the /etc/ansible/hosts file:
define host groups and hosts

cat >/etc/ansible/hosts<<EOF
[app]
192.168.137.132
192.168.137.133
EOF

insert image description here

Replenish:

默认ssh的端口为22端口,如果为其他端口号,可在主机名后面加上端口号;
如:192.168.159.131:9604 ,也可以修改配置文件中的remote_port变量值;
/etc/ansible/hosts也可以定义一个主机范围,如192.168.159.[100:200] ,表示192.168.159.100 - 192.168.159.200 的主机。

Create and configure SSH public key authentication (password-free login):

Follow the prompts to use the default configuration and press Enter all the way:

ssh-keygen -t rsa

insert image description here
Distribute the public key to each host through the ansible command:

ansible all -m authorized_key -a "user=root key='{
    
    { lookup('file', '/root/.ssh/id_rsa.pub') }}' path=/root/.ssh/authorized_keys manage_dir=no" --ask-pass -c paramiko

insert image description here
Log in to 192.168.137.133 to view: insert image description here
Modify the ansible configuration and specify the private key file path:

sed -i "s@\#private_key_file = \/path\/to\/file@private_key_file = \/root\/.ssh\/id_rsa@g" /etc/ansible/ansible.cfg

ping command test:

ansible all -m ping

insert image description here

Use ansible-playbook to install mysql in batches:

cd /etc/ansible		#进入目录
mkdir deploy-yml	#创建目录
cat >/etc/ansible/deploy-yml/mysql.yml<<EOF	  #创建 mysql.yml 文件
- hosts: mysql-test
  remote_user: root
  gather_facts: False
  roles:
    - mysql
EOF

insert image description here

Enter roles to create the mysql directory, and create the following directories and files:

The directories that are not used temporarily are empty: templates, files, handlers, meta
insert image description here

Create a directory structure under /etc/ansible/roles/mysql/

mkdir files handlers meta tasks templates vars

insert image description here

Create copy.yml under tasks

cat >tasks/copy.yml<<EOF
#复制源码至目标服务器
- name: copy mysql source code to client
  copy: src={
    
    {
    
    mysql_package}} dest={
    
    {
    
    install_dir}} owner=root group=root
#复制模板文件至目标服务器
- name: copy mysql install script to client
  template: src={
    
    {
    
    shell_dir}} dest={
    
    {
    
    install_dir}} owner=root group=root mode=0775
EOF

insert image description here

Create install.yml under tasks

cat >tasks/install.yml<<EOF
#执行模板文件进行安装
- name: install mysql
  shell: bash {
    
    {
    
    install_dir}}/{
    
    {
    
    shell_name}}
EOF

insert image description here

Create main.yml under tasks

cat >tasks/main.yml<<EOF
#引用copy、install模块
- include: copy.yml
- include: install.yml
EOF

insert image description here

Create main.yml under vars

cat >vars/main.yml<<EOF
mysql_version: mysql-5.7.12
#这里事先下好了包 放在指定路径下即可
mysql_package: /usr/local/mysql-5.7.12-linux-glibc2.5-x86_64.tar.gz
shell_name : insmysql.sh
install_dir: /usr/local
shell_dir: /install/insmysql.sh
EOF

insert image description here

Create a mysql-test group in /etc/ansible/hosts and add the ip that needs to install mysql

Only one machine is used here for testing, and the machines can be directly added under the mysql-test group in batches - - all machines must be SSH-free password-free login

cat >>/etc/ansible/hosts<<EOF
[mysql-test]
192.168.137.133
EOF

Test with ansible-playbook -C mysql.ymlinsert image description here

After the test is ok, " ansible-playbook mysql.yml " executes

Guess you like

Origin blog.csdn.net/wkl1007/article/details/103510310