linux————ansible

目录

一、认识自动化运维

自动化运维主要关注的方面

常见的开源自动化运维工具

二、ansible

特点

环境

免密ssh​编辑

管理机hd1 安装ansible

测试连接性

一、服务器分组

二、ansible模块

查看所有支持的模块

hostname模块

file模块

stat模块

copy模块

fetch模块

user模块

group模块

cron模块

yum_repository模块

yum模块

service模块

scripts模块

commend与shell模块

三、playbook

语法

ymal格式

四、roles(角色)

roles目录结构

通过roles实现LAMP

一、创建roles目录结构

二、准备httpd服务器的主页文件,php测试页和配置文件等

三、编写httpd角色的main.yml文件

四、编写httpd角色里的handler

五、编写mysql角色的main.yml文件

六、编写php角色的main.yml文件

vim /etc/ansible/roles/php/tasks/main.yml

七、编写lamp的playbook文件调用前面定义好的三个角色

八、执行剧本


一、认识自动化运维

        自动化运维: 将日常IT运维中大量的重复性工作,小到简单的日常检查、配置变更和软件安装,大到整个变更流程的组织调度,由过去的手工执行转为自动化操作,从而减少乃至消除运维中的延迟,实现“零延时”的IT运维。

自动化运维主要关注的方面
  1. 管理机与被管理机的连接(管理机如何将管理指令发送给被管理机)

  2. 服务器信息收集 (如果被管理的服务器有centos7.5外还有其它linux发行版,如suse,ubuntu等。当你要做的事情在不同OS上有所不同,你需要收集信息,并将其分开处理)

  3. 服务器分组(因为有些时候我要做的事情不是针对所有服务器,可能只针对某一个分组)

  4. 管理内容的主要分类

  • 文件目录管理(包括文件的创建,删除,修改,查看状态,远程拷贝等)

  • 用户和组管理

  • cron时间任务管理

  • yum源配置与通过yum管理软件包

  • 服务管理

  • 远程执行脚本

  • 远程执行命令

常见的开源自动化运维工具
  1. puppet

    基于ruby语言,成熟稳定。适合于大型架构,相对于ansible和saltstack会复杂些。

  2. saltstack

    基于python语言。相对简单,大并发能力比ansible要好, 需要维护被管理端的服务。如果服务断开,连接就会出问题。

  3. ansible

    基于python语言。简单快捷,被管理端不需要启服务。直接走ssh协议,需要验证所以机器多的话速度会较慢。

二、ansible

        ansible是一种由Python开发的自动化运维工具,集合了众多运维工具(puppet、cfengine、chef、func、fabric)的优点,实现了批量系统配置、批量程序部署、批量运行命令等功能。

特点

        部署简单

        默认使用ssh进行管理,基于python里的==paramiko==模块开发

        管理端和被管理端不需要启动服务

        配置简单,功能强大,扩展性强

        能过playbook(剧本)进行多个任务的编排

环境

        关闭防火墙, selinux

        时间同步

        静态ip

        修改主机名互相解析

        vim /etc/hosts

192.168.100.3        hd1
192.168.100.4        hd2
192.168.100.5        hd3

免密ssh
管理机hd1 安装ansible

yum install -y epel-release

yum install -y ansible

将被管理机hd2,hd3 加入管理机组(单独加入时不会进行分组)

vim /etc/zasible/hosts

192.168.100.3

192.168.100.4

192.168.100.5

测试连接性

ansible -m ping  master

ansible -m ping  web

ansible -m ping  all

一、服务器分组

ansible通过一个主机清单功能来实现服务器分组。

Ansible的默认主机清单配置文件为/etc/ansible/hosts.

[web]                    组名
192.168.100.4           表示192.168.100.4客户端

示例: 定义192.168.100.3这台服务器的别名为master

master ansible_ssh_host=192.168.100.3 ansible_ssh_port=22

没有做免密登录的服务器可以指定用户名与密码

master  ansible_ssh_host=192.168.100.3 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass="123456"

二、ansible模块

        ansible是基于模块工作的,本身没有批量部署的能力。真正具有批量部署的是ansible所运行的模块,ansible只是提供一种框架。

        ansible支持的模块非常的多,我们并不需要把每个模块都记住,而只需要熟悉一些常见的模块,其它的模块在需要用到时再查询即可。

查看所有支持的模块

ansible-doc -l

ansible-doc  ping

hostname模块

hostname模块用于修改主机名(注意: 它不能修改/etc/hosts文件)

将其中一远程机器主机名修改为agent1

ansible 192.168.100.4  -m hostname -a 'name=agent1‘

file模块

file模块用于对文件相关的操作(创建, 删除, 软硬链接等)

创建一个目录

ansible master -m file -a ’path=/test  state=directory‘ 

创建一个文件

ansible master -m file -a 'path=/test/111 state=touch'

递归修改owner,group,mode

ansible web -m file -a 'path=/test recurse=yes owner=bin group=daemon mode=1777'

删除目录 absent 缺席的(连同目录里的所有文件)

ansible web -m file -a 'path=/test state=absent'

创建文件并指定owner,group,mode等

ansible web -m file -a 'path=/tmp111 state=touch owner=bin group=daemon mode=1777'

删除文件

ansible web -m file -a 'path=/tmp/111 state=absent'

创建软链接文件

ansible web -m file -a 'src=/etc/fstab path=/tmp/fstab state=link'

创建硬链接文件

ansible web -m file -a 'src=/etc/fstab path=/tmp/fstab2 state=hard'

stat模块

ansible master -m stat -a 'path=/etc/fstab'

copy模块

在master上准备一个文件,拷贝此文件到group1的所有机器上

echo master > /opt/222

ansible web -m copy -a 'src=/opt/222 dest=/tmp/333'使用content参数直接往远程文件里写内容(会覆盖原内容)

ansible web -m copy -a 'content="hello \n world\n" dest=/tmp/333'

使用force参数控制是否强制覆盖

如果目标文件已经存在,则不覆盖

ansible web -m copy -a 'src=/opt/222 dest=/tmp/333 force=no'

如果目标文件已经存在,则会强制覆盖
ansible web -m copy -a 'src=/opt/222 dest=/tmp/333 force=yes'

使用backup参数控制是否备份文件

backup=yes        表示如果拷贝的文件内容与原内容不一样,则会备份一份
web的机器上会将/tmp/333备份一份(备份文件命名加上时间),再远程拷贝新的文件为/tmp/333
ansible web -m copy -a 'src=/etc/fstab dest=/tmp/333 backup=yes owner=daemon group=daemon mode=1777'

fetch模块

fetch模块与copy模块类似,但作用相反。用于把远程机器的文件拷贝到本地。

在两台被管理机上分别创建一个同名文件(但内容不同)

echo agent1 > /tmp/1.txt
echo agent2 > /tmp/1.txt

从master上fecth文件(因为group1里有2台机器,为了避免同名文件文件冲突,它使用了不同的目录)

ansible web  -m fetch -a 'src=/tmp/1.txt dest=/tmp/'

fetch模块不能从远程拷贝目录到本地

user模块

user模块用于管理用户账号和用户属性。

创建aaa用户,默认为普通用户,创建家目录

ansible web -m user -a ‘name=aaa state=present’

创建bbb系统用户,并且登录shell环境为/sbin/nologin

ansible web -m user -a ‘name=bbb state=present system=yes  shell="/sbin/nologin"’

创建ccc用户, 使用uid参数指定uid, 使用password参数传密码

echo 123456 | openssl passwd -1 -stdin
$1$xacDNgkf$/8SQj.hsQYsryXBSm97wC1

ansible web -m user -a 'name=ccc uid=2000 state=present password="$1$xacDNgkf$/8SQj.hsQYsryXBSm97wC1"'

创建一个普通用户叫hadoop,并产生空密码 密钥对

ansible web -m user -a 'name=hadoop generate_ssh_key=yes'

删除aaa用户,但家目录默认没有删除

ansible web -m user -a 'name=aaa state=absent'

删除bbb用户,使用remove=yes参数让其删除用户的同时也删除家目录

ansible  web -m user -a 'name=bbb state=absent remove=yes'

group模块

创建组

ansible web -m group -a 'name=groupa gid=3000 state=present'

删除组(如果有用户的gid为此组,则删除不了)

ansible web -m group -a 'name=groupa state=absent'

cron模块

cron模块用于管理周期性时间任务

创建一个cron任务,不指定user的话,默认就是root(因为我这里是用root操作的)。
如果minute,hour,day,month,week不指定的话,默认都为*

 ansible web -m cron -a 'name="test cron1" user=root job="touch /tmp/111" minute=*/2' 

删除cron任务

ansible web -m cron -a 'name="test cron1" state=absent'

yum_repository模块

yum_repository模块用于配置yum仓库。

增加一个/etc/yum.repos.d/local.repo配置文件

ansible web -m yum_repository -a "name=local description=localyum baseurl=file:///mnt/ enabled=yes gpgcheck=no"

删除/etc/yum.repos.d/local.repo配置文件

ansible web -m yum_repository -a "name=local state=absent" 

yum模块

yum模块用于使用yum命令来实现软件包的安装与卸载。

使用yum安装一个软件(前提:group1的机器上的yum配置都已经OK)

使用yum安装vsftpd

ansible group1 -m yum -a 'name=vsftpd state=present'

使用yum卸载vsftpd

service模块

service模块用于控制服务的启动,关闭,开机自启动等。

启动httpd服务,并设为开机自动启动(自启可选用  yes/no  true/false  on/off)

 ansible web -m service -a 'name=httpd state=started enabled=on'

关闭vsftpd服务,并设为开机不自动启动

ansible web -m service -a 'name=httpd state=stopped enabled=false'

scripts模块

script模块用于在远程机器上执行**本地**脚本。

在master上准备一个脚本

master# vim 1.sh
#!/bin/bash
mkdir /tmp/haha
touch /tmp/haha/{1..10}

在web的远程机器里都执行master上的/tmp/1.sh脚本(此脚本不用给执行权限)

ansible web -m script -a '1.sh'

使用shell脚本实现在web的被管理机里的mariadb里创建一个abc库

vim  1.sh

#!/bin/bash

yum install mariadb-server -y  &> /dev/null

systemctl start mariadb
systemctl enable mariadb

mysql << EOF
create database abc;
quit
EOF

ansible web -m scripts -a ‘1.sh'’

commend与shell模块

        两个模块都是用于执行linux命令的,这对于命令熟悉的工程师来说,用起来非常high。

shell模块与command模块差不多(command模块不能执行一些类似$HOME,>,<,|等符号,但shell可以)

ansible -m command web -a "useradd user2"

ansible -m command web -a "cat /etc/passwd |wc -l"

 ansible -m shell web -a "cat /etc/passwd |wc -l"    shell模块并不是百分之百任何命令都可以,比如vim或ll别名就不可以

三、playbook

playbook(剧本): 是ansible用于配置,部署,和管理被控节点的剧本。用于ansible操作的编排。

使用的格式为yaml格式(saltstack,elk,docker,docker-compose,kubernetes等也都会用到yaml格式)

语法

hosts:        用于指定要执行任务的主机,其可以是一个或多个由冒号分割主机组

remote_user :        用于指定远程主机上的执行任务的用户

tasks :        任务列表,按顺序执行任务 

handlers :        类似task,但需要使用notify进行通知调用

variables:        定义变量

ymal格式

        以.yaml或.yml结尾

        文件的第一行以 "---"开始,表明YMAL文件的开始(可选的)
         以#号开头为注释 
        列表中的所有成员都开始于相同的缩进级别, 并且使用一个 `"- "` 作为开头(一个横杠和一个空格)
        一个字典是由一个简单的 `键: 值` 的形式组成(这个冒号后面必须是一个空格)
        注意: 写这种文件不要使用tab键,都使用空格

 创建一个存放playbook的目录(路径自定义)

mkdir /etc/ansible/playbook

准备httpd配置文件,并修改成你想要的配置

yum install httpd -y

vim /etc/httpd/conf/httpd.conf

写一个playbook文件(后缀为.yml或.yaml)

# vim /etc/ansible/playbook/example.yaml
---

- hosts: group1  remote_user: root
 tasks:  

 - name: ensure apache is at the latest version    
   yum: name=httpd,httpd-devel state=latest
    
 - name: write the apache config file        
   copy: src=/etc/httpd/conf/httpd.conf dest=/etc/httpd/conf/httpd.conf
    
   notify:
   - restart apache
    
 - name: ensure apache is running (and enable it at boot)
   service: name=httpd state=started enabled=yes
    
 handlers:    
   - name: restart apache
     service: name=httpd state=restarted

执行写好的palybook

ansible-playbook /etc/ansible/playbook/example.yaml

四、roles(角色)

        roles(角色): 就是通过分别将variables, tasks及handlers等放置于单独的目录中,并可以便捷地调用它们的一种机制。

        假设我们要写一个playbook来安装管理lamp环境,那么这个playbook就会写很长。所以我们希望把这个很大的文件分成多个功能拆分, 分成apache管理,php管理,mysql管理,然后在需要使用的时候直接调用就可以了,以免重复写。就类似编程里的模块化的概念,以达到代码复用的效果。

roles目录结构

files:用来存放由copy模块或script模块调用的文件。
tasks:至少有一个main.yml文件,定义各tasks。
handlers:有一个main.yml文件,定义各handlers。
templates:用来存放jinjia2模板。
vars:有一个main.yml文件,定义变量。
meta:有一个main.yml文件,定义此角色的特殊设定及其依赖关系。

通过roles实现LAMP
一、创建roles目录结构

cd /etc/ansible/roles/

mkdir -p {httpd,mysql,php}/{files,tasks,handlers,templates,vars,meta}

touch {httpd,mysql,php}/{tasks,handlers,vars,meta}/main.yml

yum install tree -y

tree /etc/ansible/roles/

二、准备httpd服务器的主页文件,php测试页和配置文件等

echo "test main page" > /etc/ansible/roles/httpd/files/index.html

echo -e "<?php\n\tphpinfo();\n?>" > /etc/ansible/roles/httpd/files/test.php 

 yum install httpd -y

vim /etc/httpd/conf/httpd.conf

cp /etc/httpd/conf/httpd.conf /etc/ansible/roles/httpd/files/

三、编写httpd角色的main.yml文件

vim /etc/ansible/roles/http/task/main.yaml

 ---
 - name: 安装httpd
   yum: name=httpd,httpd-devel state=present

 - name: 同步httpd配置文件
   copy: src=/etc/ansible/roles/httpd/files/httpd.conf dest=/etc/httpd/conf/httpd.conf

   notify: restart httpd

 - name: 同步主页文件
   copy: src=/etc/ansible/roles/httpd/files/index.html dest=/var/www/html/index.html

 - name: 同步php测试页
   copy: src=/etc/ansible/roles/httpd/files/index.php dest=/var/www/html/index.php

 - name: 启动httpd并开机自启动
   service: name=httpd state=started enabled=yes

四、编写httpd角色里的handler

vim /etc/ansible/roles/httpd/handlers/main.yml

---
- name: restart httpd
  service: name=httpd state=restarted

五、编写mysql角色的main.yml文件

vim /etc/ansible/roles/mysql/task/mail.yaml

---
- name: 安装mysql
  yum: name=mariadb,mariadb-server,mariadb-devel state=present

- name: 启动mysql并开机自启动
  service: name=mariadb state=started enabled=yes

六、编写php角色的main.yml文件
vim /etc/ansible/roles/php/tasks/main.yml

---
- name: 安装php及依赖包
  yum: name=php,php-gd,php-ldap,php-odbc,php-pear,php-xml,php-xmlrpc,php-mbstring,php-snmp,php-soap,curl,curl-devel,php-bcmath,php-mysql state=present

  notify: restart httpd

七、编写lamp的playbook文件调用前面定义好的三个角色

vim /etc/ansible/playbook/lamp.yaml

---
- hosts: group1
  remote_user: root
  roles:
    - httpd
    - mysql
    - php

八、执行剧本

ansible-playbook /etc/ansible/playbook/lamp.yaml

猜你喜欢

转载自blog.csdn.net/a872182042/article/details/132721828