自动化运维工具常用的有 ansible saltstack puppet等 ,前两者都是基于python开发,puppet基于ruby开发,今天我们简单介绍下ansible基础
一、基础知识:
1. 简介
ansible基于python开发,集合了众多运维工具的优点,实现了批量系统配置、批量程序部署、批量运行命令等功能。ansible是基于模块工作的
(1) host inventory: 指定操作的主机,是一个配置文件里面定义监控的主机
(2) 各种模块核心模块、command模块、自定义模块;
(3) 借助于插件完成记录日志邮件等功能;
(4) playbook: 剧本执行多个任务时,非必须可以让节点一次性运行多个任务。
2、特性:
(1) no agents: 不需要在被管理主机上安装任务agent
(2) no server: 无服务器端,使用时,直接运行命令即可
(3) modules in any languages: 基于模块工作,可使用任意语言开发模块
(4) yaml not code:使用yaml语言定制剧本playbook
(5) ssh by default:基于SSH工作
3、优点:
(1) 轻量级,无需在客户端安装agent,更新时,只需要在操作机上进行一次更新即可;
(2) 批量任务可以写成脚本,而且不用分发到远程就可以执行
(3) 使用python编写,维护简单
本次实验环境如下
1、建立免秘钥登陆 [[email protected] ansible]# ssh-keygen -t rsa 一路回车即可
[[email protected] ansible]# ssh-copy-id -i /root/.ssh/id_rsa.pub 172.16.80.117 [[email protected] ansible]# ssh-copy-id -i /root/.ssh/id_rsa.pub 172.16.80.118
测试免秘钥效果
2、安装ansible [[email protected] ~]# yum install ansible -y
3、修改文件 [[email protected] ansible]# vim /etc/ansible/hosts 定义主机组及主机列表[webservers]172.16.80.117172.16.80.118
4、常用模块介绍
[[email protected] ansible]# ansible-doc -l a10_server Manage A10 Networks AX/SoftAX/Thunder/vThunder devices a10_service_group Manage A10 Networks devices‘ service groups a10_virtual_server Manage A10 Networks devices‘ virtual servers acl Sets and retrieves file ACL information. add_host add a host (and alternatively a group) to the ansible-playbook in-memory inventory airbrake_deployment Notify airbrake about app deployments alternatives Manages alternative programs for common commands apache2_module enables/disables a module of the Apache2 webserver apk Manages apk packages apt Manages apt-packages apt_key Add or remove an apt key apt_repository Add and remove APT repositories apt_rpm apt_rpm package manager assemble Assembles a configuration file from fragments assert Fail with custom message async_status Obtain status of asynchronous task at Schedule the execution of a command or script file via the at command. authorized_key Adds or removes an SSH authorized key azure create or terminate a virtual machine in azure azure_rm_deployment Create or destroy Azure Resource Manager template deployments azure_rm_networkinterface Manage Azure network interfaces. azure_rm_networkinterface_facts Get network interface facts. azure_rm_publicipaddress Manage Azure Public IP Addresses. azure_rm_publicipaddress_facts Get public IP facts. azure_rm_resourcegroup Manage Azure resource groups. azure_rm_resourcegroup_facts Get resource group facts. azure_rm_securitygroup Manage Azure network security groups.
模块帮助命令 [[email protected] ansible]# ansible-doc -s ping- name: Try to connect to host, verify a usable python and return `pong‘ on success. action: ping 4.1ping模块 ansible webservers -m ping
4.2 shell模块(需要执行客户机上的脚本可以用该模块,脚本在被控端) [[email protected] ansible]# ansible webservers -m shell -a ‘/tmp/test.sh‘
说明:webservers是主机组的名称,-m 后面接的是模块名称,-a后是模块的参数
4.3script模块 (脚本在主控端) [[email protected] ansible]# ansible webservers -m script -a ‘/root/run.sh‘
4.4 command模块 [[email protected] ansible]# ansible webservers -m command -a ‘uptime‘
4.5 yum模块 [[email protected] ~]# ansible webservers -m command -a ‘yum install httpd -y‘
4.6 service模块 [[email protected] ~]# ansible webservers -m service -a ‘name=httpd state=started‘ 对服务的操作有 started stopped restarted reloaded四个参数
4.7 copy模块 [[email protected] ~]# ansible webservers -m copy -a ‘dest=/tmp src=/root/run.sh‘
4.8 cron模块 [[email protected] ~]# ansible all -m cron -a ‘name="Cron job" minute=*/5 hour=* day=* month=* weekday=* job="/usr/bin/ntpdate pool.ntp.org"‘
4.9 file模块 [[email protected] ~]# ansible all -m file -a "dest=/tmp/test.sh mode=777 owner=martin group=martin"
时间: 2024-10-12 02:42:09