Ansible自动化运维[亲测有效]

Ansible自动化运维[亲测有效]ansible自动化运维工具,基于Python开发,集合了众多运维工具(puppet、cfengine、chef、func、fabric)的优点,实现了批量系统配置、批量程序部署、批量运行命令等功能,无客户端。管理节点management定义主机组,通过SSH连接远程主机,使用功能模块实现对node节点的部署操作。通过编写playbook来实现较为复杂的项目。ansible域名解析2、安装ansible3、定义主机清单变量表三、Ansible模块Ad-Hoc-点对点模式②、复制模块③、用

大家好,欢迎来到IT知识分享网。


Ansible自动化运维

一、Ansible简介

在这里插入图片描述

ansible自动化运维工具,基于Python开发,集合了众多运维工具(puppet、cfengine、chef、func、fabric)的优点,实现了批量系统配置、批量程序部署、批量运行命令等功能,无客户端。

1、工作原理

在这里插入图片描述

管理节点management定义主机组,通过SSH连接远程主机,使用功能模块实现对node节点的部署操作。

通过编写playbook来实现较为复杂的项目。

二、Ansible部署

1、环境

ansible域名解析

                     IP                      Role
               192.168.116.131              ansible
               192.168.116.132               host1
               192.168.116.133               host2
               192.168.116.134               host3

2、安装ansible

# yum -y install ansible

3、定义主机清单

# vim /etc/ansible/ansible.cfg
host_key_checking = False
------------------------------------------------------------------
# vim /etc/ansible/hosts
[web1]
host1
[web2]
host[2:3]
[web:children]
web1
web2
[web:vars]
ansible_ssh_user="root"
ansible_ssh_pass="1"
ansible_ssh_port="22"
------------------------------------------------------------------
# 参数
[web]                            # 主机组
host1                            # 主机
[web:children]                   # 子分组
[web:vars]                       # 组变量
ansible_ssh_user=""              # 用户变量
ansible_ssh_pass=""              # 密码变量
ansible_ssh_port=""              # 端口变量

变量表

在这里插入图片描述

4、连通测试

# ansible web -m ping -o
---------------------------------------------------------
# 参数
web                       # 主机组
-m                        # 模块
ping                      # ssh探测
-o                        # 简介输出
---------------------------------------------------------
使用自定义外部主机清单
# ansible -i 文件路径 主机组 -m 模块

三、Ansible模块

Ad-Hoc-点对点模式

①、Shell模块

# ansible web -m shell -a 'hostname' -f 2
# ansible web -m shell -a 'yum -y install httpd' -f 2
---------------------------------------------------------------------------------------
-m                         # 模块
shell                      # shell模块
-a                         # 执行参数
-f                         # 并发线程数

②、复制模块

# ansible web -m copy -a 'src=/etc/hosts dest=/mnt/1.txt owner=root group=bin mode=777 backup=yes'
---------------------------------------------------------------------------------------
copy                      # 复制模块
src                       # 源地址
dest                      # 目标地址
owner                     # 属主
group                     # 属组
backup                    # 备份(是否覆盖)

③、用户模块

# ansible web -m user -a 'name=lz state=present password="XXX" shell=/sbin/nologin'
---------------------------------------------------------------------------------------
user                      # 用户模块
name                      # 用户名
state                     # 状态 present创建 absent删除
password                  # 密码 (echo XXX|openssl passwd -1 -stdin)
shell                     # 登录权限
append=yes                # 追加修改

④、Yum模块

# ansible web -m yum -a 'name=httpd state=present'
---------------------------------------------------------------------------------------
yum                       # 软件模块
name                      # 软件包名
state                     # 状态 present安装 latest安装最新版 absent卸载

⑤、服务模块

# ansible web -m service -a 'name=httpd state=started enabled=yes'
---------------------------------------------------------------------------------------
service                   # 服务模块
name                      # 程序名
state                     # 状态 started启动 stopped停止 restarted重启
enabled=yes               # 开机自启

⑥、文件模块

# ansible web -m file -a 'path=/mnt/a.txt state=touch'
---------------------------------------------------------------------------------------
file                      # 文件模块
path                      # 路径
state                     # 状态 touch文件 directory目录

⑦、收集模块

# ansible web -m setup -a 'filter=ansible_processor'
---------------------------------------------------------------------------------------
# ansible host3 -m setup # 查询所有模块
setup                     # 收集模块
filter                    # 过滤信息

四、YAML语法

YAML Ain’t Markup Language-非标记语言

1、编写剧本

# vim apache.yaml
- hosts: web
  tasks:
  - name: install httpd package
    yum: name=httpd state=present
  - name: copy httpd.conf
    copy: src=./httpd.conf dest=/etc/httpd/conf/httpd.conf
    notify: restart httpd
  - name: ensure httpd running
    service: name=httpd state=started enabled=yes
  handlers:
  - name: restart httpd
    service: name=httpd state=restarted
-----------------------------------------------------------------------------------
- host:                   # 针对主机对象
  tasks:                  # 主任务列表
  - name:                 # 任务名
    yum:                  # 任务
    notify:               # 引用处理程序
  handlers:               # 定义处理程序
  - name:                 # 任务名
  service:                # 任务

2、语法检查

# ansible-playbook apache.yaml --syntax-check
# ansible-playbook apache.yaml --list-tasks
# ansible-playbook apache.yaml --list-hosts

3、执行剧本

# ansible-playbook apache.yaml

五、角色扮演

roles则是在ansible中,playbooks的目录组织结构
将代码或文件进行模块化,成为roles的文件目录组织结构
易读,代码可重用,层次清晰

目录结构

在这里插入图片描述

nginx                   角色名
files                   普通文件
handlers                触发器程序
tasks                   主任务
templates               金甲模板(有变量的文件)
vars                    自定义变量

①、编写剧本

# vim roles/site.yaml
- hosts: web
  roles:
  - nginx

②、编写主任务

# vim roles/nginx/tasks/main.yaml
---
- name: copy nginx.repo
  copy: src=nginx.repo dest=/etc/yum.repos.d/nginx.repo
- name: yum makecache
  shell: yum makecache
- name: install epel-release package
  yum: name=epel-release state=present
- name: install nginx package
  yum: name=nginx state=latest
- name: copy index.html
  copy: src=index.html dest=/usr/share/nginx/html/index.html
- name: copy nginx.conf.j2 template
  template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf
  notify: restart nginx
- name: ensure nginx service running
  service: name=nginx state=started enabled=yes

③、编写触发任务

# vim roles/nginx/handlers/main.yaml
---
- name: restart nginx
  service: name=nginx state=restarted

④、配置金甲模板

# cp /etc/nginx/nginx.conf roles/nginx/templates/nginx.conf.j2
# vim roles/nginx/templates/nginx.conf.j2
...
worker_processes  { 
   { 
    ansible_processor_cores }};
...
worker_connections  { 
   { 
    worker_connections }};

⑤、配置变量

# vim roles/nginx/vars/main.yaml
worker_connections: 100

⑥、配置普通文件

# cp /etc/yum.repos.d/nginx.repo roles/nginx/files/nginx.repo
# echo nginx > roles/nginx/files/index.html

⑦、执行剧本

# ansible-playbook site.yaml --syntax-check
# ansible-playbook site.yaml

六、主机清单加密

①、设置加密

# ansible-vault encrypt /etc/ansible/hosts

②、加密编辑

# ansible-vault edit /etc/ansible/hosts --ask-vault-pass

③、加密使用

# ansible web -m ping -o --ask-vault-pass

七、变量设置

- hosts: web
  tasks:
  - name: useradd abc
    user: name={ 
   { 
    item }} state=present group=wheel
    with_items:
    - testuser1
    - testuser2

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/24541.html

(0)

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

关注微信