Ansible运维自动化1

一、Ansible

简述

  • Ansible是一款为类Unix系统开发的自由开源的配置和自动化工具。
  • 它用Python写成,类似于saltstack和Puppet,但是有一个不同和优点是我们不需要在节点中安装任何客户端。
  • 它使用SSH来和节点进行通信。Ansible基于 Python paramiko 开发,分布式,无需客户端,轻量级,配置语法使用 YMAL 及 Jinja2模板语言,更强的远程命令执行操作。

功能

  • Ansible主要功能是帮助运维实现IT工作的自动化、降低人为操作失误、提高业务自动化率、提升运维工作效率。可以理解为是一种配置管理工具。
  • 常用于软件部署自动化、配置自动化、管理自动化、系统化系统任务、持续集成、零宕机平滑升级等。

特点

1、部署简单,没有客户端,只需在主控端部署Ansible环境,被控端无需做任何操作;
2. 模块化:调用特定的模块,完成特定任务
3. 默认使用SSH协议对设备进行管理;
4. 主从集中化管理;
5、配置简单、功能强大、扩展性强;
6、支持API及自定义模块,可通过Python轻松扩展;
7、通过Playbooks来定制强大的配置、状态管理
8. 对云计算平台、大数据都有很好的支持;
9. 具有幂等性:一个操作在一个主机上执行一遍和执行N遍的结果是一样的

  • ansible是基于模块工作的,本身没有批量部署的能力。真正具有批量部署的是ansible所运行的模块,ansible只是提供一种框架。主要包括:
    (1)、连接插件connection plugins:负责和被监控端实现通信;
    (2)、host inventory:指定操作的主机,是一个配置文件里面定义监控的主机;
    (3)、各种模块核心模块、command模块、自定义模块;
    (4)、借助于插件完成记录日志邮件等功能;
    (5)、playbook:剧本执行多个任务时,非必需可以让节点一次性运行多个任务

通信机制

  • Ansible主推的卖点是其无需任何Daemon维护进程即可实现相互间的通信,且通信方式是基于业内统一标准的安全可靠的SSH安全连接。
  • 无客户端,只需安装SSH、Python即可。
  • 基于OpenSSH通信,底层基于SSH协议(Windows基于PowerShell)。
  • 支持密码和SSH认证,建议使用公私钥方式认证,因为密码认证方式的密码需明文写配置文件,虽然配置文件可加密,但会增加Ansible使用的复杂度。
  • 支持Windows,但仅支持客户端,服务端必须是Linux系统。

工作机制

  • Ansible为多层部署而设计,Ansible 通过描述所有系统如何相互关联来为您的 IT 基础架构建模,而不仅仅是一次管理一个系统。
  • 它不使用代理,也没有额外的自定义安全基础设施,因此易于部署。最重要的是,它使用一种非常简单的语言(YAML,以 Ansible Playbooks 的形式),描述自动化任务。
  • ansible系统由控制主机和被管理主机组成,控制主机不支持windows平台。
  • 核心: ansible
    Core Modules: ansible自带的模块
    Custom Modules: 核心模块功能不足时,用户可以添加扩展模块
    Plugins: 通过插件来实现记录日志,发送邮件或其他功能
    Playbooks: 剧本,YAML格式文件,多个任务定义在一个文件中,定义主机需要调用哪些模块来完成的功能
    Connectior Plugins: ansible基于连接插件连接到各个主机上,默认是使用ssh
    Host Inventory: 记录由Ansible管理的主机信息,包括端口、密码、ip等

官网:传送门

二、安装部署

老样子,我们直接将源放到真机的apache发布目录下
在这里插入图片描述

在控制端server1添加真机的ansible源

[root@server1 /etc/yum.repos.d] pwd
/etc/yum.repos.d
[root@server1 /etc/yum.repos.d] cat ansible.repo 
[ansible]
name=ansible
baseurl=http://172.25.51.250/ansible
gpgcheck=0

列源安装

[root@server1 /etc/yum.repos.d] yum repolist
[root@server1 /etc/yum.repos.d] yum install -y ansible
[root@server1 /etc/yum.repos.d] ansible --version
ansible 2.8.5
  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, Sep 12 2018, 05:31:16) [GCC 4.8.5 20150623 (Red Hat 4.8.5-36)]

在这里插入图片描述

添加远程主机
修改主机文件,指定远程主机,将主机server2、3分配给jjr这个组。
ansible.cfg主配置文件
roles角色文件

[root@server1 /etc/yum.repos.d] cd /etc/ansible/
[root@server1 /etc/ansible] ls
ansible.cfg  hosts  roles
[root@server1 /etc/ansible] vim hosts
[root@server1 /etc/ansible] tail -n 3 hosts 
[jjr]
server2
server3

尝试着平以下我们添加的主机,发现出现以下状况。为了安全,ansible是不能够随便访问的,我们为了方便实验来做一个免密认证
在这里插入图片描述
免密认证

ssh-keygen
ssh-copy-id server2
ssh-copy-id server3

在这里插入图片描述

ansible调用ping这个模块

ansible all -m ping #所有组
ansible jjr -m ping #jjr这个组

在这里插入图片描述
在这里插入图片描述

[root@ansible ansible]# vim westos
[root@ansible ansible]# ansible all --list
  hosts (1):
    172.25.254.251
[root@ansible ansible]# ansible all -i westos --list
  hosts (2):
    172.25.254.151
    172.25.254.251
[root@ansible ansible]# cat westos 
172.25.254.151
172.25.254.251

创建一个新的用户,来进行连接操作,配置如下
注:提前把主节点主机和node节点主机的指定用户devops创建好,进行免密认证!!!

[jia@ansible ansible]$ ls
ansible.cfg  inventory
[jia@ansible ansible]$ cat ansible.cfg 
[defaults]
inventory     		 = ~/ansible/inventory
host_key_checking	 = False
remote_user		 = devops
module_name		 = shell

[privilege_escalation]
become=True
become_method=sudo
become_user=root
become_ask_pass=False
[jia@ansible ansible]$ cat inventory 
[westos]
172.25.254.151
172.25.254.251

在这里插入图片描述

三、Ansible常用模块

1.ansible实现管理的方式
Ad-Hoc
##利用ansible命令直接完成管理,主要用于临时命令使用场景
playbook
##ansible脚本,主要用于大型项目场景,需要前期的规划

2.Ad-Hoc执行方式中如何获得帮助
ansible-doc
##显示模块帮助的指令
#格式 ansible-doc [参数] [模块…]
#常用参数
-l ##列出可用模块
-s ##显示指定模块的playbook片段

3.ansible命令运行方式及常用参数
#格式:ansible 清单 -m 模块 -a 模块参数
#常用参数
#–version 显示版本
#-m module 指定模块,默认为command模块
#-v 详细过程 -vv -vvv更详细过程
#–list 显示主机列表,也可以用–list-hosts
#-k 提示输入ssh连接密码,默认key认证
#-C 预执行检测
#-T 执行命令的超时时间,默认10s
#-u 指定远程执行的用户
#-b 执行sudo切换身份操作
#-become-user=USERNAME 指定sudo的用户
#-K 提示输入sudo密码

4.ansible的基本颜色代表
绿色 执行成功但为对远程主机做任何改变
黄色 执行成功并对远程主机做改变
红色 执行失败

5.ansible中的常用模块

(1).command
功能: 在远程主机执行命令,此模块为默认模块
chdir 执行命令前先进入到指定目录
cmd 运行命令指定
creates 如果文件存在将不运行
removes 如果文件存在在将运行
free_form 在远程主机中执行的命令,此参数不需要加

[jia@ansible ansible]$ ansible all -m command -a "ls /mnt"

(2).shell
功能:和command功能类似

[jia@ansible ansible]$ ansible all -m shell -a "executable=sh ps ax | grep $$ "

(3).script
功能:在ansible主机中写好的脚本在受控主机中执行

[jia@ansible ansible]$ ansible all -m script -a "/mnt/test.sh"

(4).copy
功能:从ansible主机复制文件到受控主机

[jia@ansible ansible]$ ansible all -m copy -a "src=/mnt/file mode=755 dest=/mnt/ backup=yes"

(5).fetch
功能:从受控主机把文件复制到ansible主机,但不支持目录

[jia@ansible ansible]$ ansible 172.25.254.251 -m fetch -a "src=/etc/sysconfig/network-scripts/ifcfg-ens3 dest=/home/jia/ansible"

(6).file
功能:设置文件的属性

[jia@ansible ansible]$ ansible all -m file -a "path=/mnt/jia state=touch owner=devops group=devops mode=777"

(7).unarchive
功能:解压缩

[jia@ansible ansible]$ tar zcf bin.tar.gz /usr/bin/
[jia@ansible ansible]$ ansible westos -m unarchive -a "src=bin.tar.gz dest=/mnt owner=devops"
[jia@ansible ansible]$ ansible westos -m unarchive -a "src=/mnt/etc.tar.gz dest=/mnt copy=no"

(8).archive
功能:压缩

[jia@ansible ansible]$ ansible westos -m archive -a "path=/etc dest=/mnt/etc.tar.gz format=gz owner=devops mode=700"

(9)hostname
功能:管理主机名称

ansbile 172.25.254.100 -m hostname -a 'name=lee.westos.com'

(10)cron
功能:计划任务

[jia@ansible ansible]$ ansible westos -m cron -a 'job="echo hello" name=test minute=*/2'
[jia@ansible ansible]$ ansible westos -m cron -a 'job="echo hello" name=test minute=*/2 state=absent'

(11)yum_repository
功能:配置系统软件仓库源文件

[jia@ansible ansible]$ ansible westos -m yum_repository -a 'file=westos name=AppStream description=AppStream baseurl=http://172.25.254.51/rhel8.2/AppStream enabled=yes gpgcheck=no state=present'
[jia@ansible ansible]$ ansible westos -m yum_repository -a "file=westos name=BaseOS description=BaseOS baseurl=http://172.25.254.51/rhel8.2/BaseOS enabled=yes gpgcheck=no state=present"

(12)dnf
功能:管理系统中的dnf仓库及管理软件

[jia@ansible ansible]$ ansible all -m dnf -a "name=httpd state=present"
[jia@ansible ansible]$ ansible all -m dnf -a "name=gcc state=absent"

(13)service
功能:管理系统服务状态

[jia@ansible ansible]$ ansible all -m service -a "name=httpd state=started enabled=yes"
[jia@ansible ansible]$ ansible all -m service -a "name=httpd state=stopped enabled=yes"

(14)firewalld
功能:管理火墙

[jia@ansible ansible]$ ansible westos -m firewalld -a "zone=public service=http permanent=yes state=enabled immediate=yes"

(15)user
功能:模块可以帮助我们管理远程主机上的用户,比如创建用户、修改用户、删除用户、为用户创建密钥对等操作

ansible all -m user -a 'name=lee'
ansible all -m user -a 'name=lee state=absent'
ansible all -m user -a 'name=lee remove=yes state=absent'
ansible all -m user -a 'name=lee group=888'
ansible all -m user -a 'name=lee group=888 groups="user1,user2"'
ansible all -m user -a 'name=lee groups="user3"'
ansible all -m user -a 'name=lee groups="user1,user2" append=yes'
openssl passwd -6 'westos'
ansible all -m user -a 'name=lee
password="$6$F4OBwqoXAigDV.dn$I2OgEPB3kfyl8CPmdh3Y8vKDqewZKrVMIDPPIt8GKnhs/
DW4gZHfxrZX5ziQN7rVjISX7l14KwDQHEd.uprlV/"'
ansible all -m user -a 'name=lee generate_ssh_key=yes'

(16)group
功能:group 模块可以帮助我们管理远程主机上的组。

[jia@ansible ansible]$ ansible westos -m group -a "name=testgroup gid=6666 state=present"
[jia@ansible ansible]$ ansible westos -m group -a "name=testgroup gid=6666 state=absent"

(17)lineinfile
功能:类似vim

[jia@ansible ansible]$ ansible all -m lineinfile -a "path=/mnt/westosfile line='hello jia'create=yes"
[jia@ansible ansible]$ ansible all -m lineinfile -a "path=/mnt/westosfile line='hello jia' create=yes"
[jia@ansible ansible]$ ansible all -m lineinfile -a "path=/mnt/westosfile regexp='^hello' line='hello linux'"
[jia@ansible ansible]$ ansible all -m lineinfile -a "path=/mnt/westosfile regexp='^hello' state=absent"
[jia@ansible ansible]$ ansible all -m lineinfile -a "path=/mnt/westosfile line='hello jia\nhello jia1'"
[jia@ansible ansible]$ ansible all -m lineinfile -a "path=/mnt/westosfile line='############' insertafter=EOF"
[jia@ansible ansible]$ ansible all -m lineinfile -a "path=/mnt/westosfile line='#######begin#####' insertbefore=BOF"

(18)replace
功能:replace 模块可以根据我们指定的正则表达式替换文件中的字符串,文件中所有被匹配到的字符串都会被替换

[jia@ansible ansible]$ ansible all -m replace -a "path=/mnt/westos regexp='westos' replace='WESTOS' backup=yes"

(19)setup
功能:setup模块用于收集远程主机的一些基本信息

[jia@ansible ansible]$ ansible all -m setup
[jia@ansible ansible]$ ansible all -m setup -a "filter='ansible_all_ipv4_addresses'"

(20)debug
功能:调试模块,用于在调试中输出信息

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

贾几人要努力

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值