Ansible自动化运维

1 ansible介绍和架构

1.1 什么是ansible

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

ansible是基于 paramiko 开发的,并且基于模块化工作,本身没有批量部署的能力。真正具有批量部署的是ansible所运行的模块,ansible只是提供一种框架。ansible不需要在远程主机上安装client/agents,因为它们是基于ssh来和远程主机通讯的。ansible目前已经已经被红帽官方收购,是自动化运维工具中大家认可度最高的,并且上手容易,学习简单。是每位运维工程师必须掌握的技能之一。

1.2 ansible功能

  • 批量执行远程命令,可以对远程的多台主机同时进行命令的执行
  • 批量安装和配置软件服务,可以对远程的多台主机进行自动化的方式配置和管理各种服务
  • 编排高级的企业级复杂的IT架构任务,Ansible的Playbook和role可以轻松实现大型的IT复杂架构
  • 提供自动化运维工具的开发API,有很多运维工具,如jumpserver就是基于ansible实现自动化管理功能

1.3 ansible特性

  • 基于Python语言实现
  • 模块化:调用特定的模块完成特定任务,支持自定义模块,可使用任何编程语言写模块
  • 部署简单,基于python和SSH(默认已安装),agentless,无需代理不依赖PKI(无需ssl)
  • 安全,基于OpenSSH
  • 幂等性:一个任务执行1遍和执行n遍效果一样,不因重复执行带来意外情况,此特性非绝对
  • 支持playbook编排任务,YAML格式,编排任务,支持丰富的数据结构
  • 较强大的多层解决方案 role

1.4 ansible架构

组合INVENTORY、API、MODULES、PLUGINS的绿框,为ansible命令工具,其为核心执行工具。

  • INVENTORY:Ansible管理主机的清单/etc/anaible/hosts
  • MODULES:Ansible执行命令的功能模块,多数为内置核心模块,也可自定义
  • PLUGINS:模块功能的补充,如连接类型插件、循环插件、变量插件、过滤插件等
  • API:供第三方程序调用的应用程序编程接口

2 ansible安装和入门

2.1 ansible安装

2.1.1 基础配置

(三个节点均进行相同操作)

复制代码
#修改主机名称
[root@localhost ~]# hostnamectl set-hostname ansible
[root@ansible ~]#
[root@localhost ~]# hostnamectl set-hostname node1
[root@node1 ~]#
[root@localhost ~]#  hostnamectl set-hostname node2
[root@node2 ~]#
#关防火墙和selinux
[root@ansible ~]# setenforce 0
[root@ansible ~]# systemctl disable firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@ansible ~]# systemctl stop firewalld
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
[root@ansible ~]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
#配置yum源
[root@node1 ~]# mkdir /opt/centos
[root@ansible ~]# mount CentOS-7-x86_64-DVD-1804.iso /opt/centos/
mount: /dev/loop0 is write-protected, mounting read-only
[root@ansible ~]# mv /etc/yum.repos.d/* /media
[root@ansible ~]# vi /etc/yum.repos.d/local.repo
[centos]
name=centos
baseurl=file:///opt/centos
gpgcheck=0
enabled=1
[root@ansible ~]# yum clean all && yum repolist
Loaded plugins: fastestmirror
Cleaning repos: centos
Cleaning up everything
Maybe you want: rm -rf /var/cache/yum, to also free up space taken by orphaned data from disabled or removed repos
Cleaning up list of fastest mirrors
Loaded plugins: fastestmirror
Determining fastest mirrors
centos                                                   | 3.6 kB     00:00
(1/2): centos/group_gz                                     | 166 kB   00:00
(2/2): centos/primary_db                                   | 3.1 MB   00:00
repo id                              repo name                            status
centos                               centos                               3,971
repolist: 3,971
[root@ansible ~]# yum install -y wget
[root@ansible ~]# wget -O /etc/yum.repos.d/CentOS-Base-epel.repo http://mirrors.aliyun.com/repo/Centos-7.repo
2.1.2 ansible安装

(三个节点均进行相同操作)

复制代码
#修改主机名称
[root@localhost ~]# hostnamectl set-hostname ansible
[root@ansible ~]#
[root@localhost ~]# hostnamectl set-hostname node1
[root@node1 ~]#
[root@localhost ~]#  hostnamectl set-hostname node2
[root@node2 ~]#
#关防火墙和selinux
[root@ansible ~]# setenforce 0
[root@ansible ~]# systemctl disable firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@ansible ~]# systemctl stop firewalld
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
[root@ansible ~]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
#配置yum源
[root@node1 ~]# mkdir /opt/centos
[root@ansible ~]# mount CentOS-7-x86_64-DVD-1804.iso /opt/centos/
mount: /dev/loop0 is write-protected, mounting read-only
[root@ansible ~]# mv /etc/yum.repos.d/* /media
[root@ansible ~]# vi /etc/yum.repos.d/local.repo
[centos]
name=centos
baseurl=file:///opt/centos
gpgcheck=0
enabled=1
[root@ansible ~]# yum clean all && yum repolist
Loaded plugins: fastestmirror
Cleaning repos: centos
Cleaning up everything
Maybe you want: rm -rf /var/cache/yum, to also free up space taken by orphaned data from disabled or removed repos
Cleaning up list of fastest mirrors
Loaded plugins: fastestmirror
Determining fastest mirrors
centos                                                   | 3.6 kB     00:00
(1/2): centos/group_gz                                     | 166 kB   00:00
(2/2): centos/primary_db                                   | 3.1 MB   00:00
repo id                              repo name                            status
centos                               centos                               3,971
repolist: 3,971
[root@ansible ~]# yum install -y wget
[root@ansible ~]# wget -O /etc/yum.repos.d/CentOS-Base-epel.repo http://mirrors.aliyun.com/repo/Centos-7.repo

2.2 ansible相关文件

2.2.1 配置文件
  • /etc/ansible/ansible.cfg 主配置文件,配置ansible工作特性,也可以在项目的目录中创建此文件当前目录下如果也有ansible.cfg,则此文件优先生效,建议每个项目目录下,创建独有的ansible.cfg文件
  • /etc/ansible/hosts 主机清单
  • /etc/ansible/roles/ 存放角色的目录
2.2.2 主配置文件

ansible配置文件可以放在多个不同的地方,优先级从高到低顺序如下:

复制代码
ANSIBLE_CONFIG  #环境变量
./ansible.cfg   #当前目录下的ansible.cfg
~/.ansible.cfg  #当前用户家目录下的.ansible.cfg
/etc/ansible/ansible.cfg  #系统默认配置文件

Ansible的默认配置文件/etc/ansible/ansible.cfg,其中大部分的配置内容无需进行修改

复制代码
[defaults]
#inventory= /etc/ansible/hosts  #主机列表配置文件
#library=/usr/share/my_modules/  #库文件存放目录
#remote_tmp= $HOME/.ansible/tmp  #临时py命令文件存放在远程主机目录
#local_tmp= $HOME/.ansible/tmp  #本机的临时命令执行目录
#forks=5  #默认并发数
#sudo_user= root  #默认sudo 用户
#ask_sudo_pass = True  #每次执行ansible命令是否询问ssh密码
#ask_pass=True
#remote_port=22
#host_key_checking = False  #检查对应服务器的host_key,建议取消此行注释,实现第一次连接自动信任目标主机
#log_path=/var/log/ansible.log  #日志文件,建议启用
#module_name =command  #默认模块,可以修改为shell模块

[privilege_escalation]  #普通用户提权配置
#become=True
#become method=sudo
#become_user=root
#become ask pass=False

Eg:当前目录下的ansible配置文件优先生效

复制代码
[root@ansible ~]# ansible --version
ansible 2.9.27
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.5 (default, Apr 11 2018, 07:36:10) [GCC 4.8.5 20150623 (Red Hat 4.8.5-28)]
[root@ansible ~]# cp /etc/ansible/ansible.cfg .
[root@ansible ~]# ansible --version
ansible 2.9.27
  config file = /root/ansible.cfg    #注意配置文件路径
  configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.5 (default, Apr 11 2018, 07:36:10) [GCC 4.8.5 20150623 (Red Hat 4.8.5-28)]
2.2.3 inventory主机清单文件

ansible的主要功能用在于批量主机操作,为了便携地使用其中的部分主机,;可以在inventory file中将其分组命名

默认的inventory file为/etc/ansible/hosts

inventory file可以有多个,且也可以通过Dynamic Inventory来动态生成

注意:生产建议在每个项目目录下从创建项目独立的hosts文件

主机清单文件格式

inventory文件遵循INI文件风格,中括号中的字符为组名。可以将同一个主机同时归并到多个不同的组中

此外,当如若目标主机使用了非默认的SSH端口,还可以在主机名称之后使用冒号加端口号来标明如果主机名称遵循相似的命名模式,还可以使用列表的方式标识各主机

复制代码
[root@ansible ~]# vi /etc/ansible/hosts
[websrvs]
192.168.20.[11:12]

[dbsrvs]
192.168.20.11
192.168.20.13

[testsrvs:children]   #定义testsrvs组中包括其余两个分组,实现组嵌套
websrvs
dbsrvs
#查看组
[root@ansible ~]# ansible all --list-hosts
  hosts (3):
    192.168.20.11
    192.168.20.12
    192.168.20.13
[root@ansible ~]# ansible websrvs --list-hosts
  hosts (2):
    192.168.20.11
    192.168.20.12
[root@ansible ~]# ansible dbsrvs --list-hosts
  hosts (2):
    192.168.20.11
    192.168.20.13
[root@ansible ~]# ansible testsrvs --list-hosts
  hosts (3):
    192.168.20.11
    192.168.20.12
    192.168.20.13

2.3 ansible相关工具

利用ansible实现管理的主要方式:

Ansible Ad-Hoc 即利用ansible命令,主要用于临时命令使用场景

Ansible playbook 主要用于长期规划好的,大型项目的场景,需要有前期的规划过程

ansible 使用前准备

ansible 相关工具大多数是通过ssh协议,实现对远程主机的配置管理、应用部署、任务执行等功能建议:使用此工具前,先配置ansible主控端能基于密钥认证的方式联系各个被管理节点

2.3.1 ansible-doc

此工具用来显示模块帮助,相当于man

语法格式:ansible-doc 参数 模块

参数说明:

  • -l:列出可用模块

  • -s:显示指定模块的playbook片段

    ansible-doc -l #列出所有模块
    ansible-doc ping #查看指定模块帮助用法
    ansible-doc-sping #查看指定模块帮助用法

查看模块数量

复制代码
[root@ansible ~]# date
Tue Jul 30 00:35:58 CST 2024
[root@ansible ~]# ansible --version
ansible 2.9.27
  config file = /root/ansible.cfg
  configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.5 (default, Apr 11 2018, 07:36:10) [GCC 4.8.5 20150623 (Red Hat 4.8.5-28)]
[root@ansible ~]# ansible-doc -l|wc -l
3387
2.3.2ansible

ansible Ad-Hoc的执行方式的主要工具就是ansible

语法格式:

ansible 主机列表 参数 模块

参数说明:

--version #显示版本

-m #指定模块,默认为command

--list-hosts #显示主机列表,可简写--1ist

-C #检查,并不执行

-T #执行命令的超时时间,默认10S

-k #提示输入ssh连接密码,默认Key验证

-u #执行远程执行的用户,默认root

-b #指定sudo的runas用户,默认为root

-K #提示输入sudo时的口令

-f FORKS #指定并发同时执行ansible任务的主机数

复制代码
[root@ansible ~]# ansible all -m ping
192.168.20.13 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}
192.168.20.12 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}
192.168.20.11 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}

ansible的执行状态

绿色:执行成功并且不需要做改变的操作

黄色:执行成功并且对目标主机做变更

红色:执行失败

相关推荐
EMTime2 小时前
Docker运行OpenWRT
运维·docker·容器
lolo大魔王2 小时前
Linux 文件系统超全面详解(原理、结构、挂载、分区、inode、日志、管理命令)
linux·运维·服务器
磊 子4 小时前
详细讲解一下epoll
linux·io·epoll·io多路复用
printfLILEI4 小时前
php中的类与对象以及反序列化
linux·开发语言·php
zyl837215 小时前
Docker 使用手册
运维·docker·容器
古月方枘Fry5 小时前
MGRE实验
运维·服务器
叠叠乐5 小时前
redmi k90 pro max 强解BL,刷海外rom, 并刷入sukisu ultra
linux
stolentime6 小时前
FreeDomain 本地开发环境快速搭建指南
运维·服务器·网络
xiaoye-duck6 小时前
《Linux系统编程》Linux 进程间通信之管道基础解析:从匿名管道原理到基于管道的进程池实现
linux