首页 > 技术文章 > linux-ansible自动化运维1

csren12 2021-03-07 00:39 原文

配置管理系统,远程执行命令,批量安装服务,启停服务等等:自动化运维工具fabric\puppet\saltstack\chef\ansible

saltstack\ansible都是基于python编写的。

提高工作效率、减少重复性的劳动力操作

大大的减少了人为出错的可能性

ansible支持数据化管理,数据化追源,找到问题的来源点。

ansible走的是ssh协议,提前配置好免密登录

ansible是一个同时管理多个远程主机的软件,必须是任意可以通过ssh登录的机器。因此ansible可以管理的机器如:远程虚拟机、物理机、本地机器。

ansible功能:批量文件下发、批量数据复制、批量数据修改、删除、批量自动化安装服务软件、批量服务启停、脚本化、自动批量服务部署

不需要安装客户端的,是基于ssh协议通信的,使用yaml配置文件语法

ansible管理机安装部署:

只在管理节点去安装就行了。master(23.146)主机做管理节点;docker01-02(23.144-145)两台主机做被管理的主机,关闭防火墙;三台主机都能互相ping通;配置阿里源与epel源。

一、管理节点

1.在管理节点,管理机上安装ansible软件--ansible的服务器

[root@k8s-master ~]# yum install epel-release ansible libselinux-python -y 

2.检查ansible软件

[root@k8s-master ~]# rpm -ql ansible |grep -E '^/etc|^/usr/bin'

[root@k8s-master ~]# rpm -ql ansible |grep -E '^/etc|^/usr/bin'
/etc/ansible                  #Ansible软件主目录
/etc/ansible/ansible.cfg      #Ansible主配置文件
/etc/ansible/hosts            #Ansible软件被管理节点的主机列表文件
/etc/ansible/roles
/usr/bin/ansible              #Anisble批量管理命令
/usr/bin/ansible-2
/usr/bin/ansible-2.7
/usr/bin/ansible-config
/usr/bin/ansible-connection
/usr/bin/ansible-console
/usr/bin/ansible-console-2
/usr/bin/ansible-console-2.7
/usr/bin/ansible-doc
/usr/bin/ansible-doc-2
/usr/bin/ansible-doc-2.7
/usr/bin/ansible-galaxy
/usr/bin/ansible-galaxy-2
/usr/bin/ansible-galaxy-2.7
/usr/bin/ansible-inventory
/usr/bin/ansible-playbook       #Ansible程序剧本执行命令
/usr/bin/ansible-playbook-2
/usr/bin/ansible-playbook-2.7
/usr/bin/ansible-pull
/usr/bin/ansible-pull-2
/usr/bin/ansible-pull-2.7
/usr/bin/ansible-test
/usr/bin/ansible-vault
/usr/bin/ansible-vault-2
/usr/bin/ansible-vault-2.7

3.检查Ansible版本

[root@k8s-master ~]# ansible --version

二、被管理节点安装

[root@docker01 ~]# yum -y install libselinux-python -y

[root@docker02 ~]# yum -y install libselinux-python -y

三、ansible批量管理的方式

传统密码认证:ssh输入密码

密钥管理

密码认证分为两种:一种需要手动输入密码的方式,一种是将要管理的主机用户名和密码写到/etc/ansible/hosts  

配置管理主机的清单:控制节点下的/etc/ansible/hosts

1.备份旧配置文件

[root@k8s-master ~]# cp /etc/ansible/hosts{,.bak}

2.添加被管理机器的ip地址

[root@k8s-master ~]# tail -3 /etc/ansible/hosts
[root@k8s-master ~]#cat >> /etc/ansible/hosts <<'EOF'
[test1]
192.168.23.144
192.168.23.145
EOF

测试密码登录-手动输入密码:

 1.手动访问客户端机器,生成指纹密钥

[root@k8s-master ~]# ssh root@192.168.23.144

[root@k8s-master ~]# ssh root@192.168.23.145

否则会报错,执行ansible test1 -m command -a "hostname" -k -u root 

[root@k8s-master ~]# ansible test1 -m command -a "hostname" -k -u root

测试密码登录-将密码写入到/etc/ansible/hosts

1.修改/etc/ansible/hosts文件,在文件中定义主机密码

[root@k8s-master ~]# tail -3 /etc/ansible/hosts
[test1]
192.168.23.144 ansible_ssh_user=root ansible_ssh_pass=rcs551552?
192.168.23.145 ansible_ssh_user=root ansible_ssh_pass=rcs551552?

此时不用输入密码,直接可以用ansible的命令

[root@k8s-master ~]# ansible test1 -a "hostname"

SSH密钥登录管理各个主机:将/etc/ansible/hosts下的刚添加的root用户和密码删除

在控制主机创建密钥对,将公钥传到各个主机上

[root@k8s-master ~]# yum -y install sshpass

[root@k8s-master ~]# cat ssh.sh 
#! /bin/bash
[ -s /root/.ssh/ ] && rm -rf /root/.ssh/id_*
ssh-keygen -t dsa -f /root/.ssh/id_dsa -N ""
for ip in 144 145
do
    sshpass -prcs551552? ssh-copy-id -i /root/.ssh/id_dsa.pub "-o strictHostKeyChecking=no" 192.168.23.$ip
done

重新测试下:

[root@k8s-master ~]# ansible test1 -a "hostname"

 Ansible批量管理的模式有两种:

1、通过ansible命令实现批量管理,直接输入命令

2、通过playbook剧本实现批量管理

 

 

# 模块支持的有3000+数量
[root@k8s-master ~]# ansible-doc -l
# 查看某个模块的具体用法
[root@k8s-master ~]# ansible-doc -s command

获取各个主机的负载情况:

[root@k8s-master ~]# ansible test1 -a "uptime"

[root@k8s-master ~]# ansible test1 -a "ls chdir=/root"   ##切换到root目录下,查看被管理主机root目录下有哪些文件

[root@k8s-master ~]# ansible test1 -a "pwd creates=/etc"   若被管理主机的/etc目录存在,则不执行pwd命令,若目录不存在则执行pwd命令

command模块中的creates\removes\chdir命令;查看command模块有哪些命令

[root@k8s-master ~]# ansible-doc -s command

[root@k8s-master ~]# ansible test1 -a "ls /opt removes=/test1"  若被管理主机存在test1目录,则执行ls /opt;否则跳过也就是不执行ls /opt

command模块和shell模块基本功能差不多,但是涉及通配符、特殊符号、变量等就要用-m shell

shell模块:

 批量查询sshd服务:

[root@k8s-master ~]# ansible test1 -m shell -a "ps -ef|grep sshd |grep -v grep"

批量执行远程脚本:脚本必须在客户端存在

 [root@k8s-master ~]# ansible test1 -m shell -a "mkdir -p /server/scripts/;echo 'hostname' > /server/scripts/hostname.sh;chmod +x /server/scripts/hostname.sh;/usr/bin/bash /server/scripts/hostname.sh warn=False"

script模块

[root@k8s-master ~]# ansible-doc -s script  查看该模块有哪些命令

该脚本不用在远程主机上存在。

[root@k8s-master ~]# echo -e "pwd\nhostname" > pwd.sh
[root@k8s-master ~]# cat pwd.sh
pwd
hostname
[root@k8s-master ~]# chmod +x pwd.sh
[root@k8s-master ~]# ansible test1 -m script -a "pwd.sh"  ##批量在所有客户端执行脚本,该脚本不用在被管理主机上

Ansible 文件类模块

copy模块:复制文件到远程主机

copy模块是远程推送数据模块,只能把数据推送给远程主机节点,无法拉取数据到本地

[root@k8s-master ~]# ansible-doc -s copy  查看该模块有哪些命令

[root@k8s-master ~]# ansible test1 -m copy -a "src=/etc/passwd dest=/tmp/test1.pwd"  #把控制节点的/etc/passwd复制到各个被控制主机的/tmp/test1.pwd中

[root@k8s-master ~]# ansible test1 -m command -a "ls /tmp/test1.pwd"   #查看各个被管理主机是不是存在这个文件

远程批量复制文件、备份、追加内容

[root@k8s-master ~]# ansible test1 -m copy -a "content='Hello,my name is ren' dest=/tmp/test1.txt backup=yes"

[root@k8s-master ~]# ansible test1 -m command -a "ls /tmp/test1.txt"

批量对服务器文件拷贝操作,把content参数定义的内容,写入到test1.txt文件中,并且对test1.txt旧文件内容备份

[root@k8s-master ~]# ansible test1 -m copy -a "content='Hello,my name is cheng' dest=/tmp/test1.txt backup=yes"  #原先test1.txt写的是my name is ren,执行完这个命令之后,就变成了my name is cheng,且会生成一个带时间戳的test1.txt*备份文件,这个里面的呢容就是my name is ren

[root@k8s-master ~]# ansible test1 -m shell -a "ls /tmp/test1.*"     #shell模块支持通配符

file模块

创建文件以及目录、删除文件以及目录、修改文件权限等。

[root@k8s-master ~]# ansible test1 -m file -a "dest=/tmp/ren_dir    state=directory"   #远程创建文件夹

[root@k8s-master ~]# ansible test1 -m shell -a "ls -ld /tmp/ren_*"                              #查看远程主机检查文件夹是否生成

[root@k8s-master ~]# ansible test1 -m file -a "dest=/tmp/test1_666 state=touch"   #远程创建文件

[root@k8s-master ~]# ansible test1 -m shell -a "ls -l /tmp/test1_666"                       #查看远程主机文件是否创建

[root@k8s-master ~]# ansible test1 -m file -a "src=/etc/hosts dest=/tmp/hosts_link state=link"   #远程创建软连接

[root@k8s-master ~]# ansible test1 -m shell -a "ls -l /tmp/hosts_link"       #查看软连接以及属性

Ansible软件管理模块

yum模块

[root@k8s-master ~]# ansible-doc -s yum   #查看有哪些命令-a 后面引号中的内容

 1.检查客户端机器是否安装了iotop

[root@k8s-master ~]# ansible test1 -m shell -a "rpm -qa iotop warn=false"   #去除警告,查看已经安装了iotop

2.通过yum模块批量安装软件

[root@k8s-master ~]#ansible test1 -m yum -a "name=iotop state=installed"   #其实yum模块,就是远程在客户端机上执行,可以快速登录到节点机器,检查进程

3.远程删除软件包
[root@k8s-master ~]#ansible test1 -m yum -a "name=nginx state=absent"

4.升级iotop软件包
[root@k8s-master ~]#ansible test1 -m yum -a "state=latest name=iotop"

5.升级系统所有软件包,排除某些(nginx)服务
[root@k8s-master ~]#ansible test1 -m yum -a "state=latest name='*' exclude='nginx'"

Ansible服务管理模块

service、systemd模块

如果使用systemctl 管理程序的话,可以使用systemd模块,systemctl 可以 控制程序启/停,reload,开机启动,观察程序状态(status)等,掌握使用后管理就更方便了

主要参数
daemon_reload:在执行任何其他操作之前运行守护进程重新加载,以确保systemd已经读取其他更改
enabled:服务是否开机自动启动yes|no。enabled和state至少要有一个被定义
masked:是否将服务设置为masked状态,被mask的服务是无法启动的
name:必选项,服务名称
no_block(2.3后新增):不要同步等待操作请求完成
state:对当前服务执行启动,停止、重启、重新加载等操作(started,stopped,restarted,reloaded)
user:使用服务的调用者运行systemctl,而不是系统的服务管理者

1、systemd管理服务
ansible test1 -m systemd -a "name=crond state=stopped"
ansible test1 -m systemd -a "name=crond state=started"
ansible test1 -m systemd -a "name=crond state=restarted"
ansible test1 -m systemd -a "name=crond state=reloaded"

2、查看服务状态

[root@k8s-master ~]# ansible test1 -m shell -a "systemctl status crond" |grep Active

3、查看服务是否开机自启

[root@k8s-master ~]# ansible test1 -m shell -a "systemctl list-unit-files" | grep crond

4、开启某个服务并设置开机自启

[root@k8s-master ~]#ansible test1 -m systemd -a "name=nginx enabled=yes state=started"

Ansible剧本playbook学习--yaml文件

ansible剧本有两个基本的部分组成:hosts和tasks;hosts定义管理的主机信息和变量;tasks定义被管理的主要要做什么操作

[root@k8s-master ~]# cat nginx.yaml -n
     1    # install nginx yaml ,by chaoge
     2    - hosts: all
     3      tasks:
     4          - name: Install nginx Package
     5            yum: name=nginx state=present
     6          - name: Copy Nginx.conf
     7            copy: src=./nginx.conf dest=/etc/nginx/nginx.conf mode=0644

1.表示注释信息,可以用#,也可以用 --- 三个短横线
2.定义playbook管理的目标主机,all表示所有的主机,也可以写 主机组名
3.定义playbok所有的任务集合信息,比如该文件,定义了2个任务 ,安装nginx,拷贝nginx配置文件
4.定义了任务的名词,自定义的帮助信息
5.定义任务的具体操作,比如这里用yum模块实现nginx的安装
6.
7.第六、第七两行作用是使用copy模块,把本地当前的nginx.conf配置文件,分发给其他所有客户端机器,且授权

hosts部分

# 方式一:定义所管理的主机IP地址
- hosts: 192.168.178.111
tasks:
动作...

# 方式二:定义所管理主机的名字
- hosts: backup01
tasks:
动作...

# 方式三:定义管理主机
- hosts: 192.168.178.111, rsync01
tasks:
动作...

# 方式四:管理所有主机
- hosts: all
tasks:
动作...

tasks部分

# 方式一:采用变量格式设置任务
tasks:
  - name: make sure apache is running
    service: name=https state=running
# 当传入的参数列表过长时,可以将其分割到多行
tasks:
  - name: copy ansible inventory(清单) file to client
    copy: src=/etc/ansible/hosts dest=/etc/ansible/hosts
          owner=root group=root mode=0644

# 方式二:采用字典格式设置多任务
tasks:
   - name: copy ansible inventory file to client
     copy:
         src: /etc/ansible/hosts
         dest: /etc/ansible/hosts
         owner: root
         group: root
         mode: 0644

加载剧本命令:

ansible-playboook nginx.yaml

ansible-playbook nginx.yml -C   #模拟执行

ansible-playbook nginx.yml --syntax-check #检查playbook语法

 

推荐阅读