ansible

目录

一、为什么学ansible

二、ansible概述

三、ansible特点

四、ansible架构图运行机制

五、安装预配置

六、ansible基础操作命令的模块

1.command 模块

2.shell 模块

3.cron 模块

4.user 模块

5.group 模块

6.copy 模块

一、为什么学ansible

学习 Ansible 可以更有效地管理 IT 基础设施、自动化部署和配置管理,提高工作效率和准确性,并且可以应用于各种不同的场景和环境。

二、ansible概述

ansible 是一种自动化工具,可以在大规模计算机系统配置、部署和管理应用程序

三、ansible特点

1、部署简单,只需在主控端部署Ansible环境, 被控端无需做任何操作

2、默认使用SSH协议设备进行管理;

3、主从集中化管理

4、配置简单、功能强大、扩张性强;

5、支持API及自定义模块,可以通过Pyhton轻松扩展

6、通过playbooks 来定制强大的配置、状态管理

7、对云计算平台、大数据都有很好的支持

四、ansible架构图运行机制

ansible_第1张图片

Ansible: Ansible核心程序

Hostinventory: 记录有ansible管理主机的信息, 包括密码、端口、IP等

Playbooks:“剧本”YAML格式文件,多个任务定义在一个文件中,定义主机需要调用哪些模块来完成的功能

Core Modules:"核心模块"主要操作是通过调用核心模块(yum、shell、file、useradd等)来完成管理任务

CustomModules: 自定义模块,完成核心模块无法完成的功能,支持多钟语言。

ConnectionPlugins:连接插件,Ansible和Host主机通信使用

Plugins: 记录功能日志

五、安装预配置

管理端: 192.168.86.20 ansible

被管理端: 192.168.86.30

被管理端: 192.168.86.10

#只需要管理端安装 ansible

yum install -y epel-release

yum install -y ansible

配置主机清单:

cd /etc/ansible

vim hosts

[webservers] #配置组名

192.168.86.30 #组里包含的被管理的主机IP地址或主机名(主机名需要先修改/etc/hosts文件)

[dbservers]

192.168.86.10

//配置密钥对验证

ssh-keygen -t rsa #一路回车,使用免密登录

sshpass -p '123456' ssh-copy-id [email protected]

sshpass -p '123456' ssh-copy-id [email protected]

六、ansible基础操作命令的模块

命令格式:ansible <组名> -m <模块> -a <参数列表>

ansible-doc -l #列出所有已安装的模块,按q退出

1.command 模块

//在远程主机执行命令,不支持管道,重定向等shell的特性。

ansible-doc -s command #-s 列出指定模块的描述信息和操作动作

ansible 192.168.86.30 -m command -a 'date' #指定 ip 执行 date

ansible webservers -m command -a 'date' #指定组执行 date

ansible dbservers -m command -a 'date'

ansible all -m command -a 'date' #all 代表所有 hosts 主机

ansible all -a 'ls /' #如省略 -m 模块,则默认运行 command 模块

//常用的参数:

chdir:在远程主机上运行命令前提前进入目录

creates:判断指定文件是否存在,如果存在,不执行后面的操作

removes:判断指定文件是否存在,如果存在,执行后面的操作

ansible all -m command -a "chdir=/home ls ./"

2.shell 模块

//在远程主机执行命令,相当于调用远程主机的shell进程,然后在该shell下打开一个子shell运行命令(支持管道符号等功能)

ansible-doc -s shell

ansible dbservers -m shell -a 'echo 123456 | passwd --stdin test'

ansible dbservers -m shell -a 'echo $(ifconfig ens33 | awk "NR==2 {print $2}") | cut -d " " -f2'

ansible dbservers -m shell -a 'echo $(ifconfig ens33 | awk "NR==2 {print $2}")'

3.cron 模块

//在远程主机定义任务计划。其中有两种状态(state):present表示添加(可以省略),absent表示移除。

ansible-doc -s cron #按 q 退出

//常用的参数:

minute/hour/day/month/weekday:分/时/日/月/周

job:任务计划要执行的命令

name:任务计划的名称

ansible webservers -m cron -a 'minute="*/1" job="/bin/echo helloworld" name="test crontab"'

ansible webservers -a 'crontab -l'

ansible webservers -m cron -a 'name="test crontab" state=absent' #移除计划任务,假如该计划任务没有取名字,name=None即可

4.user 模块

//用户管理的模块

ansible-doc -s user

//常用的参数:

name:用户名,必选参数

state=present|absent:创建账号或者删除账号,present表示创建,absent表示删除

system=yes|no:是否为系统账号

uid:用户uid

group:用户基本组

shell:默认使用的shell

move_home=yse|no:如果设置的家目录已经存在,是否将已经存在的家目录进行移动

password:用户的密码,建议使用加密后的字符串

comment:用户的注释信息

remove=yes|no:当state=absent时,是否删除用户的家目录

ansible dbservers -m user -a 'name="test01"' #创建用户test01

ansible dbservers -m command -a 'tail /etc/passwd'

ansible dbservers -m user -a 'name="test01" state=absent' #删除用户test01

5.group 模块

//用户组管理的模块

ansible-doc -s group

ansible dbservers -m group -a 'name=mysql gid=306 system=yes' #创建mysql组

ansible dbservers -a 'tail /etc/group'

ansible dbservers -m user -a 'name=test01 uid=306 system=yes group=mysql' #将test01用户添加到mysql组中

ansible dbservers -a 'tail /etc/passwd'

ansible dbservers -a 'id test01'

6.copy 模块

//用于复制指定主机文件到远程主机的

ansible-doc -s copy

//常用的参数:

dest:指出复制文件的目标及位置,使用绝对路径,如果是源目录,指目标也要是目录,如果目标文件已经存在会覆盖原有的内容

src:指出源文件的路径,可以使用相对路径或绝对路径,支持直接指定目录,如果源是目录则目标也要是目录

mode:指出复制时,目标文件的权限

owner:指出复制时,目标文件的属主

group:指出复制时,目标文件的属组

content:指出复制到目标主机上的内容,不能与src一起使用

ansible dbservers -m copy -a 'src=/etc/fstab dest=/opt/fstab.bak owner=root mode=640'

ansible dbservers -a 'ls -l /opt'

ansible dbservers -a 'cat /opt/fstab.bak'

ansible dbservers -m copy -a 'content="helloworld" dest=/opt/hello.txt' #将helloworld写入/opt/hello.txt文件中

ansible dbservers -a 'cat /opt/hello.txt'

你可能感兴趣的:(ansible,perl,开发语言)