01 _Ansible笔记+实操

理论

在这里插入图片描述

Ansible - 自动化运维:

  1. Ansible核心使命是什么?
    答: 运维 - 维护整个架构稳定 & 及时排查 & 调优等等
  2. Ansible核心使命 ?
    解决批量部署服务&重启&调试等

其他类似ansible产品(ansible目前最火的)
– Puppet
– Saltstack

Puppet & saltstack 典型的C/S模式工具 - 需要几台设备作为Server端(进行控制-发送指令)

  • 无论多少被管理端,都需要安装Client
    – Ansible & Saltstack 通过Python开发的 --> 相较于传统的源码C C++

为什么ansible最火?

  • 高并发处理
  • 轻量级&便于管控
  • 不需要客户端
  • 基于ssh

对ansible的优化
实际上对于Ansible优化 - 控制效率 并不是优化Ansible本身 - 网络通 & 协议 基于ssh

ssh 所有服务器都算是默认开启
验证: 密码 口令验证
密钥认证 – 密钥对 – 私钥 & 公钥
– 公钥 - 锁头 - 公钥可以通过互联网传输的
– 私钥 - 钥匙 - 不能传
需要优化ssh服务 – 提示取消 & 最大连接数 & 验证时间


什么是轻量级
什么是轻量级: 代码设计上 & 使用上
比如: Apache-重量 & Nginx-轻量
– 代码上 – Nginx减去很多不常用的功能 -->Nginx针对于某个环境|行业开发 - 电商
– Apahce安装部署 & 控制上 - 占用很多资源 Nginx降低


重要组件:
– Ansible: 核心 - 用于处理用户请求
– inventory: 清单 - 用于统计&规定下游设备有哪些? --> IP地址/网段 & 主机名
– Playbook: 剧本 - 用于指定下游设备批量处理任务的顺序
– Module & Plugin : 模块&插件 - 具体的应用,就是下游设备进行某些操作时需要调用的


Ansible重要文件 & 重要命令
– 文件:默认在/etc/ansible下
– inventory 清单 – 掌管了可以进行控制文件 hosts文件
– config配置 – 定义Ansible一些功能 调整清单文件路径,调整角色的路径 - anisble.cfg
– 角色目录 – 定义Ansible多个任务的 -roles

重要命令
– ansible-doc 命令 用于查看“模块|插件”具体功能 & 具备参数
– ansible - 用于单独执行一条控制命令
– ansible-playbook - 用于执行某个剧本的
– ansible-galaxy - 用于创建角色


使用playbook部署多台LAMP环境

Playbook – Roles 会用到的目录:
Files: 存放需要同步到异地的服务器的源码文件 & 配置文件
copy模块 进行传输 - 不要.j2后缀放在files目录下
tasks: 任务集 – 下游设备要执行哪些任务
template: 用于执行模版文件 – 一般放一些脚本 – 常规配置文件 httpd.conf --> httpd.conf.j2
template: 用于执行模版文件 – 一般放一些脚本 – 常规配置文件 httpd.conf --> httpd.conf.j2
template模块 进行传输 - .j2后缀

Copy & template – 两个用于文件传输的模块
– copy通用的 – 主要针对files目录下的文件
– template 针对于.j2格式(.exe .doc等等) 针对于template目录下

实操

通过在Server上使用Ansible 在Client1 & Client2上架构搭建LAMP

环境:server :192.168.1.61
client01: 192.168.1.63
client02:192.168.1.64

1.同步时间&时区

[root@192 ~]# ntpdate ntp1.aliyun.com
[root@192 ~]# cp -a /usr/share/zoneinfo/Asia/Shanghai /etc/localtime 

2.添加epel源安装ansible

[root@192 ~]# yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
[root@192 ~]# yum -y install ansible

3.配置免密登入

   46  ssh-keygen 
   47  ssh-copy-id 192.168.1.63
   48  ssh-copy-id 192.168.1.64

4.配置清单测试ansible

[root@192 ansible]# pwd
/etc/ansible
[root@192 ansible]# tail -3 hosts 
[web]
192.168.1.63
192.168.1.64
[root@192 ansible]# ansible web -m ping 
192.168.1.64 | SUCCESS => {
    
    
    "ansible_facts": {
    
    
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
192.168.1.63 | SUCCESS => {
    
    
    "ansible_facts": {
    
    
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}

5.server安装http&mariadb

[root@192 ansible]# yum -y install httpd mariadb-server mariadb

6.初始化

root@192 roles]# ansible-galaxy init lnmp
- Role lnmp was created successfully
- [root@192 roles]# tree lnmp/
lnmp/
├── defaults
│   └── main.yml
├── files
├── handlers
│   └── main.yml
├── meta
│   └── main.yml
├── README.md
├── tasks
│   └── main.yml
├── templates
├── tests
│   ├── inventory
│   └── test.yml
└── vars
    └── main.yml

7.cp配置文件

[root@192 files]# cp /etc/httpd/conf/httpd.conf ./lnmp/files/
[root@192 files]# cp -a /etc/my.cnf ./lnmp/files/

[root@192 files]# cat index.php 
<?php
  phpinfo();
?>

[root@192 files]# tree .  #完成后一共三个文件
.
├── httpd.conf
├── index.php
└── my.cnf

8.编写剧本模板

[root@192 tasks]# pwd
/etc/ansible/roles/lnmp/tasks

[root@192 tasks]# cat main.yaml 
- name: prepare config
  shell: iptables -F
- name: clean the firewalld
  shell: systemctl stop firewalld

- name: webserver install
  yum: name=httpd state=installed
- name: config file replace
  copy: src="httpd.conf" dest="/etc/httpd/conf/httpd.conf"
- name: provide index page
  copy: src="index.php" dest="/var/www/html"


- name: mysql install
  yum: name=mariadb-server  state=installed
- name: config file replace
  copy: src="my.cnf"  dest="/etc/my.cnf"

- name: install php
  yum: name=php,php-mysql state=installed

- name: start httpd service
  service: name=httpd state=started 
- name: start mariadb service
  service: name=mariadb state=started

9.调用

[root@192 roles]# pwd
/etc/ansible/roles
[root@192 roles]# cat install_lamp.yml 
- name: LAMP bulid
  remote_user: root
  hosts: web
  roles:
    - lamp

[root@192 roles]# ansible-playbook install_lamp.yml 

10.测试
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_45310323/article/details/111415073
今日推荐