Ansible介绍与安装使用

Ansible 介绍与安装

Ansible的定义

ansible是一种自动化运维工具,基于paramiko开发的,并且基于模块化工作,Ansible是一种集成IT系统的配置管理、应用部署、执行特定任务的开源平台,它是基于python语言,由Paramiko和PyYAML两个关键模块构建。集合了众多运维工具的优点,实现了批量系统配置、批量程序部署、批量运行命令等功能.ansible是基于模块工作的,本身没有批量部署的能力.真正具有批量部署的是ansible所运行的模块,ansible只是提供一种框架.ansible不需要在远程主机上安装client/agents,因为它们是基于ssh来和远程主机通讯的.

Ansible的基础架构

ansible系统由控制端和被控端组成,控制主机不支持windows平台

名字 含义
Ansible Ansible核心程序
HostInventory 记录由Ansible管理的主机信息,包括端口、密码、ip等
Playbooks “剧本”YAML格式文件,多个任务定义在一个文件中,定义主机需要调用哪些模块来完成的功能
CoreModules 核心模块,主要操作是通过调用核心模块来完成管理任务。
CustomModules 自定义模块,完成核心模块无法完成的功能,支持多种语言。
ConnectionPlugins 连接插件,Ansible和Host通信使用

Ansible的程序目录结构

  • 配置文件:/etc/ansible/
  • 执行文件目录:/usr/bin/
  • lib依赖库:/usr/lib/python3.6/site-packages/ansible/
  • help文件:/usr/lib/python3.6/site-packages/ansible/

Anisible特性

  1. 模块化
  2. 有Paramkio(基于SSH),PyYAML(语言,实现playbook),jinja2(模块语言)三个关键模块
  3. 支持自定义模块
  4. 幂等性:执行一次和执行多次的情况一样,如果发现已经执行过,不会执行第二次。
  5. 支持playbook编排任务
  6. 无需代理不依赖PKI(无需SSL)
  7. 安全,基于OpenSSH
  8. 部署简单,只需要python,SSH,agentless

注意事项

  1. 执行ansible的主机成语主控端,中控,master或者堡垒机
  2. 主控端python版本需要2.6或者3.5以上
  3. 被控端如果python版本小于2.4需要安装python-simplejson
  4. 被控端如果开启seLinux,需要安装libselinux-python
  5. windows不能作为主控端。

安装Ansible


[root@localhost ~]# yum install -y ansible

总下载量:22 M
安装大小:124 M
Downloading packages:
(1/22): PyYAML-3.10-11.el7.x86_64.rpm                           | 153 kB  00:00:00 
(2/22): libyaml-0.1.4-11.el7_0.x86_64.rpm                       |  55 kB  00:00:00 
(3/22): python-backports-1.0-8.el7.x86_64.rpm                   | 5.8 kB  00:00:00 
(4/22): python-backports-ssl_match_hostname-3.5.0.1-1.el7.noarc |  13 kB  00:00:00 
(5/22): python-cffi-1.6.0-5.el7.x86_64.rpm                      | 218 kB  00:00:00
......
完毕!

自定义清单(配置组内成员)

需要编辑ansible.conf中的inventory,到新的文件中。此后只修改新增的文件即可。

[root@localhost ansible]# cp hosts inventory

[root@localhost ansible]# vim inventory   此后只需编辑inventory

[test]
192.168.190.133 ansible_password=123456
192.168.190.134 ansible_password=123456
192.168.190.135 ansible_password=123456

ansible ip/组名 --list-hosts 查看组内或者某个对象的信息

[root@localhost ansible]# ansible test --list-hosts      查看test组内的主机信息
  hosts (3):
    192.168.190.133
    192.168.190.134
    192.168.190.135

Anisble实现管理方式

  1. Ad-Hoc:即ansible命令。临时使用场景。

    格式:ansible 主机集合 -m 模块名 -a "参数"

  2. Asible-playbook:主要用于长期规划好的,大型项目的场景,需要有前提的规划。

Ansible常用模块

ping 模块

Ansible最基础的模块是ping模块,主要用于判断远程客户端是否在线,用于ping本身服务器

密码认证:

  • 方法1:-k输入密码
  • 方法2:inventory中写入密码
[root@localhost ansible]# ansible 192.168.190.135 -m ping -k   方法1:-k 然后输入登录密码即可。
SSH password: 
192.168.190.135 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}

[root@localhost ansible]# vim /etc/ansible/inventory    
[test]
192.168.190.133 ansible_password=123456    方法二:主机清单中添加password。也可指定用户名
192.168.190.134 ansible_password=123456
192.168.190.135


[root@localhost ansible]# ansible all -m ping   3台被控机都已ping通。
192.168.190.135 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}
192.168.190.134 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "ping": "pong"
}
192.168.190.133 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "ping": "pong"
}

user 模块

实现Linux系统账户管理

ansible 192.168.190.133 -m user -a 'name=tom uid=5000 state=present/absent'

present:表示存在状态(默认状态)

absent: 表示缺席状态

[root@localhost ansible]# ansible 192.168.190.133 -m user -a 'name=sawyer'  133主机上创建sawyer用户
192.168.190.133 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "append": false,
    "changed": false,
    "comment": "",
    "group": 1000,
    "home": "/home/sawyer",
    "move_home": false,
    "name": "sawyer",
    "shell": "/bin/bash",
    "state": "present",
    "uid": 1000
}

[root@localhost ansible]# ansible 192.168.190.133 -m user -a 'name=sawyer state=absent'   状态为absent,表明把sawyer这个用户从133的主机上删除掉。
192.168.190.133 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "force": false,
    "name": "sawyer",
    "remove": false,
    "state": "absent"
}

command/raw/shell 模块

注:Anisble默认模块为command

在被控端执行任意命令

模块名 说明
command 无法使用环境变量等
raw 无需被控主机安装python,用于被主机设备(网络设备等)
shell 可使用管道符,重定向等
[root@localhost ansible]# ansible 192.168.190.133 -a 'ls'   command查看
192.168.190.133 | CHANGED | rc=0 >>
anaconda-ks.cfg

[root@localhost ansible]# ansible 192.168.190.133 -m shell -a 'echo "你好" > file1.txt'    引用shell模块新建file1文件
192.168.190.133 | CHANGED | rc=0 >>

[root@localhost ansible]# ansible 192.168.190.133 -m shell -a 'ls'  查看,发现已经新增
192.168.190.133 | CHANGED | rc=0 >>
anaconda-ks.cfg
file1.txt

script 模块

会把-a后面的脚本拷贝到被管理端主机,然后执行这个脚本

[root@localhost /]# mkdir scripts
[root@localhost /]# ls /scripts/
test.sh
[root@localhost /]# chmod u+x scripts/test.sh   给脚本添加执行权限。

[root@localhost scripts]# vim /scripts/test.sh   编写脚本
#!/bin/bash

echo "hello world"



成功在133主机上执行test.sh脚本
[root@localhost scripts]# ansible 192.168.190.133 -m script -a '/scripts/test.sh' 
192.168.190.133 | CHANGED => {
    "changed": true,
    "rc": 0,
    "stderr": "Shared connection to 192.168.190.133 closed.\r\n",
    "stderr_lines": [
        "Shared connection to 192.168.190.133 closed."
    ],
    "stdout": "hello world\r\n",
    "stdout_lines": [
        "hello world"
    ]
}
posted @ 2020-08-27 17:41  阿不思布丁  阅读(350)  评论(0编辑  收藏  举报