这里的例子针对openstack虚机,自动化部署node-exporter并注册到consul中
必要文件:
- ansible-lib.tar.gz
- node-consul-deploy.yml
- node_consul-Tyy-Dev-20230129.tar.gz
- runner.sh
- ssh-copy.yml
ansible-lib.tar.gz,node_consul-Tyy-Dev-20230129.tar.gz两个离线部署包可以找写文档的人要
ansible离线部署
解压离线安装包通过yum本地部署
tar -zxvf ansible-lib.tar.gz && cd ansible-lib
yum localinstall *
禁用ansible host check
注释掉host_key_checking = False这一行
vim /etc/ansible//ansible.cfg
host_key_checking = False
将客户端ip写入hosts内
[AutoDeployTest]
192.168.10.108
192.168.10.144
192.168.10.205
[AutoDeployTest:vars]
ansible_ssh_user="root"
ansible_ssh_pass="xxx"
如果各主机密码不相同时使用如何配置
[AutoDeployTest]
192.168.10.108 ansible_ssh_user="root" ansible_ssh_pass="xxx"
192.168.10.144 ansible_ssh_user="root" ansible_ssh_pass="xxx"
192.168.10.205 ansible_ssh_user="root" ansible_ssh_pass="xxx"
通过playbook批量对host做免密
生成免密公钥
ssh-keygen -t rsa
创建免密剧本ssh-copy.yml
---
- hosts: AutoDeployTest
remote_user: root
tasks:
- authorized_key:
user: root
key: "{{ lookup('file', '/root/.ssh/id_rsa.pub') }}"
执行批量免密剧本
ansible-playbook ssh-copy.yml
自动化批量部署agent
创建运行脚本runner.sh
#!/bin/bash
old_ip=192.168.11.10
new_ip=114.114.22.113
cd /root && tar -zxvf node_consul-Tyy-Dev-20230129.tar.gz && cd node_consul
sed -i "s/http:\/\/$old_ip/http:\/\/$new_ip/g" deploy_amdhost_exporter.sh
bash deploy_amdhost_exporter.sh
创建部署剧本node-consul-deploy.yml
---
- hosts: AutoDeployTest
user: root
tasks:
- name: copy package to agent server
copy:
src: "{{item.src}}"
dest: "{{item.dest}}"
mode: 0755
with_items:
- {src: "/root/node_consul-Tyy-Dev-20230129.tar.gz",dest: "/root"}
- {src: "/root/runner.sh",dest: "/root"}
- name: deploy scripts
script: /root/runner.sh
register: result
执行批量部署剧本
ansible-playbook node-consul-deploy.yml