自动化运维工具之ansible基础入门
自动化运维工具常用的有 ansiblesaltstackpuppet等 ,前两者都是基于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
2
1、建立免秘钥登陆
# ssh-keygen -t rsa一路回车即可
1
2
# ssh-copy-id -i /root/.ssh/id_rsa.pub 172.16.80.117
# ssh-copy-id -i /root/.ssh/id_rsa.pub 172.16.80.118
测试免秘钥效果
1
2
2、安装ansible
# yum install ansible -y
1
2
3、修改文件
# vim /etc/ansible/hosts定义主机组及主机列表172.16.80.117172.16.80.118
1
4、常用模块介绍
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# 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.
1
2
3
4
模块帮助命令
# 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
1
2
3
4.2 shell模块(需要执行客户机上的脚本可以用该模块,脚本在被控端)
# ansible webservers -m shell -a '/tmp/test.sh'
说明:webservers是主机组的名称,-m后面接的是模块名称,-a后是模块的参数
1
2
4.3script模块 (脚本在主控端)
# ansible webservers -m script -a '/root/run.sh'
1
2
4.4 command模块
# ansible webservers -m command -a 'uptime'
1
2
4.5 yum模块
# ansible webservers -m command -a 'yum install httpd -y'
1
2
3
4.6 service模块
# ansible webservers -m service -a 'name=httpd state=started'
对服务的操作有 startedstopped restarted reloaded四个参数
1
2
4.7 copy模块
# ansible webservers-m copy -a 'dest=/tmp src=/root/run.sh'
1
2
4.8 cron模块
# ansible all -m cron -a 'name="Cron job" minute=*/5 hour=* day=* month=* weekday=* job="/usr/bin/ntpdate pool.ntp.org"'
1
2
4.9 file模块
# ansible all -m file -a "dest=/tmp/test.sh mode=777 owner=martin group=martin"
页:
[1]