ansible 基本介绍

介绍#

Ansible是一款简单的运维自动化工具,只需要使用ssh协议连接就可以来进行系统管理,自动化执行命令,部署等任务。

Ansible的特点

  1、ansible不需要单独安装客户端,也不需要启动任何服务
  2、ansible是python中的一套完整的自动化执行任务模块
  3、ansible playbook 采用yaml配置,对于自动化任务执行过一目了然

Ansible组成结构 

ansible是Ansible的命令工具,核心执行工具;一次性或临时执行的操作都是通过该命令执行。
Ansible Playbook
任务剧本(又称任务集),编排定义Ansible任务集的配置文件,由Ansible顺序依次执行,yaml格式。
Inventory
Ansible管理主机的清单,默认是/etc/ansible/hosts文件。
Modules
Ansible执行命令的功能模块,Ansible2.3版本为止,共有1039个模块。还可以自定义模块。
Plugins
插件,模块功能的补充,常有连接类型插件,循环插件,变量插件,过滤插件,插件功能用的较少。
API
提供给第三方程序调用的应用程序编程接口。

ansible 安装

1、ansible 安装   

apt install ansible

2、Ansible Inventory文件

  Inventory文件通常用于定义要管理的主机的认证信息,例如ssh登录用户名、密码以及key相关信息。可以同时操作一个组的多台主机,组与主机组之间的关系都是通过inventory文件配置。配置文件路径为:/etc/ansible/hosts,这里是默认配置,可以自己指定在项目中使用;

使用默认文件添加主机信息,如:

[server]
192.168.108.22 ansible_ssh_port=22 ansible_ssh_user=user1 ansible_ssh_pass="123456"
192.168.108.23 ansible_ssh_port=22 ansible_ssh_user=user1 ansible_ssh_pass="123456"
命令行执行:

        root@master:~# ansible all -m ping -o
        192.168.108.22 | FAILED! => {"msg": "to use the 'ssh' connection type with passwords, you must install the sshpass program"}
        192.168.108.23 | FAILED! => {"msg": "to use the 'ssh' connection type with passwords, you must install the sshpass program"}
        安装缺少的sshpass 程序;
        root@master:~# apt install sshpass
        Reading package lists... Done
        Building dependency tree
        Reading state information... Done
        The following package was automatically installed and is no longer required:
          tcpd
        Use 'apt autoremove' to remove it.
        The following NEW packages will be installed:
          sshpass
        0 upgraded, 1 newly installed, 0 to remove and 62 not upgraded.
        Need to get 10.5 kB of archives.
        After this operation, 30.7 kB of additional disk space will be used.
        Get:1 http://mirrors.aliyun.com/ubuntu focal/universe amd64 sshpass amd64 1.06-1 [10.5 kB]
        Fetched 10.5 kB in 0s (61.3 kB/s)
        Selecting previously unselected package sshpass.
        (Reading database ... 124960 files and directories currently installed.)
        Preparing to unpack .../sshpass_1.06-1_amd64.deb ...
        Unpacking sshpass (1.06-1) ...
        Setting up sshpass (1.06-1) ...
        Processing triggers for man-db (2.9.1-1) ...
        root@master:~#
        root@master:~#
        root@master:~#

再次进行测试:

root@master:~# ansible all -m ping -o
        192.168.108.22 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python3"},"changed": false,"ping": "pong"}
        192.168.108.23 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python3"},"changed": false,"ping": "pong"}

3、ansible 参数:

-m 模块,执行命令默认为shell
-a 参数,必须给定;
-u 给定用户执行;

一些用户会受到安全规则的限制,使用 sudo 切换时只能运行指定的命令.这与 ansible的 no-bootstrapping 思想相悖,而且 ansible 有几百个模块,在这种限制下
无法进行正常的工作. 所以执行 ansible 命令时,应使用一个没有受到这种限制的账号来执行;

 

posted @ 2023-03-24 17:22  橘子飞飞  阅读(46)  评论(0编辑  收藏  举报