快捷搜索:  汽车  科技

ansible工具对比(跨平台自动化部署工具)

ansible工具对比(跨平台自动化部署工具)ansible all -m ping 2. command来执行简短指令ansible all -m command -a "echo Heelo World" 3. 使用shell模块ansible webs -m shell -a 'ls /home' 其它可选参数:ansible webs -m ping ping所有主机ansible -i ./hosts Inventory支持多种格式,如:# This is the default ansible 'hosts' file. # # It should live in /etc/ansible/hosts # # - Comments begin with the '#' character # - Blank lines are ignored #

ansible工具对比(跨平台自动化部署工具)(1)

一、简介

Ansible是一个自动化统一配置管理工具,有如下特点:

  1. 远程执行
  2. 批量配置软件服务
  3. 事件驱动
  4. 便于二次开发
  5. 跨平台、跨系统

官网地址:
https://www.ansible.com

二、安装Ansible

系统需求:

  • 一个控制节点,支持Python2.7 或Python3.5 的系统,如: Red Hat Debian CentOS macOS any of the BSDs 等。
  • 受控端,通过ssh/sftp(也可以使用scp)与控制节点连接,同样需要支持Python2.7 或Python3.5
  • 如果受控端启用了SELinux,那需要安装libselinux-python

# 安装epel源 wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo # 安装ansible yum install ansible -y ansible --version

ansible工具对比(跨平台自动化部署工具)(2)

三、Inventory资产清单

Inventory通过配置文件来记录被控制节点,清单默认位置:
/etc/ansible/hosts。
命令行可以通过-i来指定hosts的位置:

ansible -i ./hosts

Inventory支持多种格式,如:

# This is the default ansible 'hosts' file. # # It should live in /etc/ansible/hosts # # - Comments begin with the '#' character # - Blank lines are ignored # - groups of hosts are delimited by [header] elements # - You can enter hostnames or ip addresses # - A hostname/ip can be a member of multiple groups # Ex 1: Ungrouped hosts specify before any group headers. # 未分组主机 ## green.example.com ## blue.example.com ## 192.168.100.1 ## 192.168.100.10 # Ex 2: A collection of hosts belonging to the 'webservers' group # 分组到 webservers ## [webservers] ## alpha.example.org ## beta.example.org ## 192.168.1.100 ## 192.168.1.110 # If you have multiple hosts following a pattern you can specify # them like this: # 使用通配符 ## www[001:006].example.com # Ex 3: A collection of database servers in the 'dbservers' group ## [dbservers] ## ## db01.intranet.mydomain.net ## db02.intranet.mydomain.net ## 10.25.1.56 ## 10.25.1.57 # Here's another example of host ranges this time there are no # leading 0s: # 一个主机段 ## db-[99:101]-node.example.com

可以给主机定义别名、指定端口号,如:

[webs] web01 ansible_ssh_host=192.168.1.3 ansible_ssh_port=22 五、 模块

ansible使用模块来完成安装软件、复制软件等大部分任务。

1. ping

ansible webs -m ping

ansible工具对比(跨平台自动化部署工具)(3)

ping所有主机

ansible all -m ping 2. command来执行简短指令

ansible all -m command -a "echo Heelo World" 3. 使用shell模块

ansible webs -m shell -a 'ls /home'

其它可选参数:

-b - “成为”,在运行命令时告诉可以成为另一个用户。 --become-user=root - 以用户“root”运行以下命令(例如,使用命令使用“sudo”)。我们可以在此定义任何现有的用户。 -a 用于将任何参数传递给定义的模块 -m 4. Debian系操作系统使用apt模块

ansible -i ./hosts local --connection=local -b --become-user=root \ -m apt -a 'name=nginx state=installed update_cache=true'

猜您喜欢: