阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

自动化运维工具Ansible安装使用详解

173次阅读
没有评论

共计 3290 个字符,预计需要花费 9 分钟才能阅读完成。

1. 前言

Ansible 是自动化运维的工具,基于 Python 开发,实现了批量系统配置、批量程序部署、批量运行命令等功能。

Ansible 是基于模块工作的,ansible 提供一个框架,通过模块实现批量部署。

自动化运维工具 Ansible 安装使用详解

2. 安装,使用

2.1 安装 Ansible

使用 epel 的源安装,添加 epel 源此处不详述。

1
# yum install ansible --enablerepo=epel

2.2 设置密钥登录

生成 SSH 公钥密钥对

1
# ssh-keygen -t rsa -P ''

拷贝公钥到被管理端的服务器

1
2
# cat /root/.ssh/id_rsa.pub >> /root/.ssh/authorized_keys
# chmod 600 /root/.ssh/authorized_keys

确认可以用密钥连接到管理端的服务器

2.3 配置 Ansible

定义主机组,可以使用主机名或 IP

1
2
3
4
# vi /etc/ansible/hosts
[tests]
test167
test154

另外,Ansible 的配置文件在 /etc/ansible/ansible.cfg,默认不需要修改。

2.4 使用 Ansible

2.4.1 Ping 模块

1
# ansible tests -m ping

自动化运维工具 Ansible 安装使用详解

2.4.2 执行命令,command、shell 模块

1
2
3
4
# ansible tests -m command -a 'uptime'
# ansible tests -m shell -a 'date'
 
# ansible tests -m command -a 'cat  /etc/resolv.conf'

2.4.3 查看配置,setup 模块

1
# ansible tests -m setup

2.4.4 拷贝文件,copy 模块

1
# ansible tests -m copy -a 'src=/home/ec2-user/test.txt dest=/tmp/test222.txt mode=0644'

2.4.5 添加用户,user 模块

1
# ansible tests -m user -a 'name=test comment="test user"uid=1000 password="crypted-password"'

密码生成方法:

1
2
3
4
# yum install python-pip
# pip install passlib
 
# python -c "from passlib.hash import sha512_crypt; import getpass; print sha512_crypt.encrypt(getpass.getpass())"

2.4.6 安装软件,yum 模块

1
# ansible tests -m yum -a 'name=vsftpd state=present'

2.4.7 启动服务,设置开机自启动,service 模块

1
# ansible tests -m service -a 'name=vsftpd state=started enabled=yes'

查看

1
2
# ansible tests -m shell -a 'ps -ef| grep ftp'
# ansible tests -m shell -a 'ss -tln| grep 21'

2.4.8 支持管道,raw,shell 模块

1
# ansible tests -m raw -a 'ss -tln| grep 21'

2.5 其他命令

2.5.1 查看帮助

列出所有已安装模块

1
# ansible-doc -l

查看某模块的简介

1
# ansible-doc -s ping

2.5.2 ansible-pull

使用 pull 模式,(默认是 push 模式)

3. Playbook 文件

Playbook 是由一个或多个“play”组成的列表,可以让它们联同起来按事先编排的机制执行

1
# ansible-playbook test.yml

Playbook 文件的格式,YAML 是一个可读性高的标记语言。

Role 可以把 playbook 分成一个一个模块,使结构更清晰。

3.1 Role 的构造

包括 tasks, defaults, vars, files, templates, mata, handlers 各目录,其中 tasks 是必需的。

自动化运维工具 Ansible 安装使用详解

3.2 Role 的例子

本例子是最基本的构成,只包括 tasks

3.2.1 创建目录 roles/apache2/tasks

1
# mkdir -p roles/apache2/tasks

3.2.2 创建 tasks/main.yml

1
2
3
4
5
6
7
8
---
- name: Install apache2 (RedHat).
  yum: name=httpd
  when: "ansible_os_family =='RedHat'"
 
- name: Install apache2 (Debian).
  apt: name=apache2
  when: "ansible_os_family =='Debian'"

3.2.3 创建 Playbook (site.yml)

1
2
3
4
5
6
7
---
- name: Install Apache2
  hosts: tests
  remote_user: root
 
  roles:
    - apache2

3.2.4 执行

1
# ansible-playbook site.yml

自动化运维工具 Ansible 安装使用详解

3.3 官方的 playbook 例子

https://github.com/ansible/ansible-examples

3.4 playbook 文件加密

ansible-vault 对配置文件(比如 playbooks),进行基于密码的加密,防止敏感信息泄露

3.4.1 加密已存在文件

1
# ansible-vault encrypt ./site.yml

3.4.2 加密并创建文件

1
# ansible-vault create filename

加密后的 playbook

自动化运维工具 Ansible 安装使用详解

3.4.3 执行加密后的 playbook

1
# ansible-playbook ./site.yml --ask-vault-pass

3.4.4 解密

1
# ansible-vault decrypt ./site.yml

4. 后记

Ansible 使用简单,不需要客户端,模块化程度高,定制灵活,当管理服务器的数量多的时候,能起到很大的帮助。是一个很好的自动化运维工具。

下面关于 Ansible 的文章您也可能喜欢,不妨参考下:

使用 Ansible 批量管理远程服务器  http://www.linuxidc.com/Linux/2015-05/118080.htm

在 CentOS 7 中安装并使用自动化工具 Ansible  http://www.linuxidc.com/Linux/2015-10/123801.htm

CentOS 7 上搭建 Jenkins+Ansible 服务  http://www.linuxidc.com/Linux/2016-12/138737.htm

Linux 下源码编译安装 Ansible 及排错记录  http://www.linuxidc.com/Linux/2017-03/141427.htm

Ansible 基础—安装与常用模块  http://www.linuxidc.com/Linux/2017-02/140216.htm

Ansible 配置及使用  http://www.linuxidc.com/Linux/2017-03/142121.htm

自动化运维工具之 Ansible 介绍及安装使用  http://www.linuxidc.com/Linux/2016-12/138104.htm

自动化运维之 Ansible 详解  http://www.linuxidc.com/Linux/2017-03/142191.htm

Ansible 入门 notify 和 handlers  http://www.linuxidc.com/Linux/2017-02/140871.htm

CentOS 6.5 安装自动化工具 Ansible 和图形化工具 Tower  http://www.linuxidc.com/Linux/2017-03/141422.htm

Ansible 的详细介绍:请点这里
Ansible 的下载地址:请点这里

本文永久更新链接地址:http://www.linuxidc.com/Linux/2017-05/143593.htm

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-21发表,共计3290字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中