目录

ansible模块介绍

1)介绍

2)安装

3)配置文件介绍

4)command模块

5)copy 模块

6)yum 模块

7)file 模块

8)cron 模块

9)service 模块

10)shell 模块

2、实操

1)主机清单

2)主机名设置

3)安装ssh

4)添加主机清单hosts

5)ansible操作


ansible模块介绍

1)介绍

ansible 是一款强大的配置管理工具,目的是帮助系统管理员高效率的管理成百上千台主机

ansible Ad-hoc 模式:一次执行一条命名,批量执行在远程主机上(暂时只使用这个)

ansible playbook 模式:一次执行多条命令,不同的主机执行不同的命令,更加灵活。

ansible本地安装教程 ansible 安装_linux

2)安装


yum install epel-release -y

yum repolist

yum install ansible -y


3)配置文件介绍

Ansible 工具默认主目录为:/etc/ansible/,其中 hosts 文件为被管理机IP或者主机名列表ansible.cfg 为 ansible 主配置文件,roles 为角色或者插件路径,默认该目录为空

常用的 Ansible 工具管理模块包括:command、shell、script、yum、copy、File、async、docker、cron、mysql_user、ping、sysctl、user、acl、add_host、easy_install、haproxy 等。

可以使用 ansible-doc -l|more 查看 ansible 支持的模块

基于 Ansible 批量管理之前,需将被管理的服务器 IP 列表添加至/etc/ansible/hosts 文件中

由于 Ansible 管理远程服务器基于 SSH,在登录远程服务器执行命令时需要远程服务器的用户名和密码,也可以加入-k参数手动输入密码或者基于 ssh-keygen 生成免秘钥

4)command模块

command 模块为 ansible 默认模块,主要用于执行 Linux 基础命令,可以执行远程服务器命令执行、任务执行等操作。

Chdir

执行命令前,切换到目录

Creates

当该文件存在时,则不执行该步骤

Executable

换用 shell 环境执行命令

Free_form

需要执行的脚本

Removes

当该文件不存在时,则不执行该步骤

Warn

如果在 ansible.cfg 中存在告警,如果设定了 False,不会警告此行

命令格式:ansible [主机] [-m 模块] [-a args]

#Ansible ping
ansible all -m command -a "ping -c 1 www.baidu.com"
#Ansible df -h
ansible 192.168.149.13* -m command -a "df -h"
ansible all -m command -a "chdir=/testdir ls"

5)copy 模块

copy 模块主要用于文件或者目录拷贝,支持文件、目录、权限、用户组功能

copy 模块操作,src 表示源文件,dest 表示目标目录或者文件,owner指定拥有者

src

Ansible 端源文件或者目录,空文件夹不拷贝

content

用来替代 src,用于将指定文件的内容,拷贝到远程文件内

dest

客户端目标目录或者文件,需要绝对路径

backup

拷贝之前,先备份远程节点上的原始文件

directory_mode

用于拷贝文件夹,新建的文件会被拷贝,而老旧的不会被拷贝

follow

支持 link 文件拷贝

force

覆盖远程主机不一致的内容

group

设定远程主机文件夹的组名

mode

指定远程主机文件及文件及的权限

owner

设定远程主机文件夹的用户名

#Ansible copy 拷贝文件
ansible all -m copy -a 'src=/etc/passwd dest=/tmp/ mode=755 owner=root'
# 拷贝本机目录到远程客户端
ansible all -m copy -a "src=/etc/ansible dest=/tmp owner=root mode=755"
 ① 如果远程客户端 /tmp/ansible 目录不存在,则会直接把源目录拷贝成这个目录名
 ② 如果远程客户端 /tmp/ansible 目录存在,则会把源目录拷贝为/tmp/ansible/ansible
#Ansible copy 追加内容
ansible all -m copy -a 'content="Hello World" dest=/tmp/jfedu.txt backup=yes mode=755 owner=root'
#Ansible copy 客户端备份结果
ansible all -m copy -a 'content="Hello World" dest=/tmp/jfedu.txt backup=yes mode=755 owner=root'

6)yum 模块

yum 模块主要用于软件的安装、升级、卸载,支持红帽.rpm 软件的管理

conf_file

设定远程 yum 执行时所依赖的 yum 配置文件

disable_gpg_check

安装软件包之前是否坚持 gpg key

name

需要安装的软件名称,支持软件组安装

update_cache

安装软件前更新缓存

enablerepo

指定 repo 源名称

skip_broken

跳过异常软件节点

state

软件包状态,包括:installed、present、latest、absent、removed

#Ansible YUM 安装软件包,name 表示需安装的软件名称,state= installed 表示安装软件
ansible all -m yum -a "name=sysstat,screen state=installed"
#Ansible YUM 卸载软件包
ansible all -m yum -a "name=sysstat,screen state=absent"

7)file 模块

file 模块主要用于对文件的创建、删除、修改、权限、属性的维护和管理

src

Ansible 端源文件或者目录

follow

支持 link 文件拷贝

force

覆盖远程主机不一致的内容

group

设定远程主机文件夹的组名

mode

指定远程主机文件及文件及的权限

owner

设定远程主机文件夹的用户名

path

目标路径,也可以用 dest,name 代替

state

状态包括:file、link、directory、hard、touch、absent

attributes

文件或者目录特殊属性

#Ansible file 创建目录
ansible 192.168.* -m file -a "path=/tmp/`date +%F` state=directory mode=755"
#Ansible file 创建文件
ansible 192.168.* -m file -a "path=/tmp/jfedu.txt state=touch mode=755"

8)cron 模块

cron 模块主要用于添加、删除、更新操作系统 Crontab 任务计划

name

任务计划名称

cron_file

替换客户端该用户的任务计划的文件

job

任何计划执行的命令,state 要等于 present

backup

是否备份之前的任务计划

user

新建任务计划的用户

state

指定任务计划 present、absent

#Ansible cron 添加任务计划
ansible all -m cron -a "minute=0 hour=0 day=* month=* weekday=* name='Ntpdate server for sync time' job='/usr/sbin/ntpdate 139.224.227.121'"
#Ansible cron 删除任务计划
ansible all -m cron -a "name='Ntpdate server for sync time' state=absent"

9)service 模块

service 模块主要用于远程客户端各种服务管理,包括启动、停止、重启、 重新加载等

enabled

是否开启启动服务

name

服务名称

runlevel

服务启动级别

arguments

服务命令行参数传递

state

服务操作状态,状态包括 started, stopped, restarted,reloaded

#Ansible service 重启 httpd 服务
ansible all -m service -a "name=httpd state=restarted"
#Ansible service 重启 network 服务
ansible all -m service -a "name=network args=eth0 state=restarted"

10)shell 模块

shell 模块主要用于远程客户端上执行各种 Shell 命令或者运行脚本,远程执行命令通过/bin/sh 环境来执行,支持比 command 更多的指令

Chdir

执行命令前,切换到目录;

Creates

当该文件存在时,则不执行该步骤;

Executable

换用 shell 环境执行命令;

Free_form

需要执行的脚本;

Removes

当该文件不存在时,则不执行该步骤;

Warn

如果在 ansible.cfg 中存在告警,如果设定了 False,不会警告此行。

#Ansible shell 远程执行脚本
ansible all -m shell -a "/bin/sh /tmp/variables.sh >>/tmp/var.log"
# Ansible shell 远程执行脚本,创建目录命令,执行之前切换在/tmp 目录
ansible all -m shell -a "mkdir -p `date +%F` chdir=/tmp/ state=directory warn=no"
#Ansible shell 远程查看进程
ansible all -m shell -a "ps -ef |grep http"
ansible all -m shell -a "netstat -lntp"
ansible all -m shell -a "systemctl status network"
#Ansible shell 远程查看任务计划
ansible all -m shell -a "crontab -l"

2、实操

1)主机清单

主机IP

主机名

功能

10.225.25.1

ch01

Ansible管理主机

10.225.25.2

ch02

 

10.225.25.3

ch03

 

目标:在10.225.25.1配置Ansible,能实现免密管理10.225.25.2、10.225.25.3机器

2)主机名设置

hostnamectl set-hostname ch[01-03]

每台主机增加hosts解析

vi /etc/hosts
10.225.25.1     ch01
10.225.25.2     ch02
10.225.25.3     ch03

3)安装ssh

假定操作的机器为10.225.25.1,需要免密登录的机器为10.225.25.2

在10.225.25.1上做如下操作

1.生成密钥

ssh-keygen -t rsa
cat /root/.ssh/id_rsa.pub > /root/.ssh/authorized_keys

2.赋予权限

chmod 700 /root/.ssh/
chmod 600 /root/.ssh/authorized_keys

3.10.225.25.2机器上赋予权限

mkdir /root/.ssh/
chmod 700 /root/.ssh/

4.将authorized_keys文件传送到需要被免密登录的机器上

scp /root/.ssh/authorized_keys root@10.225.25.2:/root/.ssh/
5.10.225.25.2机器上执行ssh-add命令
ssh-agent bash
ssh-add

6.在10.225.25.1上执行ssh root@10.225.25.2 不需要输入密码则表示成功

4)添加主机清单hosts

vi /etc/ansible/hosts
...
[all]
10.225.25.1
10.225.25.2
10.225.25.3
[others]
10.225.25.2
10.225.25.3

5)ansible操作

ansible all -m ping
#安装常用基础命令
 ansible all -m yum -a "name=vim,expect,screen,lrzsz,tree,openssl,openssh-clients,openssl-devel,openssh-server,telnet,sysstat,wget,ntpdate,dos2unix,net-tools,gcc,gcc-c++,cmake,zip,unzip state=installed"#关闭防火墙
ansible all -m shell -a "systemctl disable firewalld"
ansible all -m shell -a "systemctl stop firewalld"
#关闭selinux
ansible all -m shell -a "setenforce 0"
ansible all -m shell -a "sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config &> /dev/null"
#复制目录
ansible others -m copy -a "src=/usr/local/node_exporter dest=/usr/ owner=root mode=755"
#启动服务
ansible all -m shell -a "systemctl enable node_exporter"