Ansible--快速初学者

原创
小哥 3年前 (2022-11-02) 阅读数 10 #大杂烩

Ansible--快速入门

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
      提供由第三方程序调用的应用程序编程接口。

环境准备 #

IP

系统

主机名

描述

192.168.1.30

CentOS7

ansible

ansible管理节点

192.168.1.31

CentOS7

linux.node01.com

托管节点1

192.168.1.32

CentOS7

linux.node02.com

托管节点2

192.168.1.33

CentOS7

linux.node03.com

托管节点3

192.168.1.36

CentOS6

linux.node06.com

托管节点6

Ansible安装 #

1)配置 epel

[root@ansible ~]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo [root@ansible ~]# yum clean all [root@ansible ~]# yum makecache

2)安装 ansible

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

查看ansible版本

[root@ansible ~]# ansible --version ansible 2.8.0 config file = /etc/ansible/ansible.cfg configured module search path = [u/root/.ansible/plugins/modules, u/usr/share/ansible/plugins/modules] ansible python module location = /usr/lib/python2.7/site-packages/ansible executable location = /usr/bin/ansible python version = 2.7.5 (default, Aug 4 2017, 00:39:18) [GCC 4.8.5 20150623 (Red Hat 4.8.5-16)]

Ansible Inventory文件 #

Inventory中文文档

Inventory 文件通常用于定义主机要管理的身份验证信息,例如 ssh 登录用户名、密码和 key 相关信息。可以同时操作一个组的多个主机,并且组与主机组的关系通过 inventory 文件配置。配置文件路径为: /etc/ansible/hosts

基于密码的连接 #

[root@ansible ~]# vim /etc/ansible/hosts

方法一 主机+端口+密码

[webserver] 192.168.1.31 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass="123456" 192.168.1.32 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass="123456" 192.168.1.33 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass="123456" 192.168.1.36 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass="123456"

方法二 主机+端口+密码

[webserver] 192.168.1.3[1:3] ansible_ssh_user=root ansible_ssh_pass="123456"

方法二 主机+端口+密码

[webserver] 192.168.1.3[1:3] [webserver:vars] ansible_ssh_pass="123456"

基于密钥的连接 #

基于密钥的连接需要先创建公钥和私钥,并发送给被管理机器

1)生成公钥和私钥

[root@ansible ~]# ssh-keygen [root@ansible ~]# for i in {1,2,3,6}; do ssh-copy-id -i 192.168.1.3$i ; done

2)配置连接

[root@ansible ~]# vim /etc/ansible/hosts

方法一 主机+端口+密钥

[webserver] 192.168.1.31:22 192.168.1.32 192.168.1.33 192.168.1.36

方法一 别名主机+端口+密钥

[webserver] node1 ansible_ssh_host=192.168.1.31 ansible_ssh_port=22 node2 ansible_ssh_host=192.168.1.32 ansible_ssh_port=22 node3 ansible_ssh_host=192.168.1.33 ansible_ssh_port=22 node6 ansible_ssh_host=192.168.1.36 ansible_ssh_port=22

主机组的使用 #

主机组变量名称称+主机+密码

[apache] 192.168.1.36 192.168.1.33 [apache.vars] ansible_ssh_pass=123456

主机组变量名称称+主机+密钥

[nginx] 192.168.1.3[1:2]

定义多个组,并将一个组视为另一个组的成员。

[webserver:children] #webserver组由两个子组组成:apache nginx apache nginx

临时指定inventory #

1)首先编辑主机定义列表

[root@ansible ~]# vim /etc/dockers [dockers] 192.168.1.31 ansible_ssh_pass=123456 192.168.1.32 192.168.1.33

2指定执行命令。 inventory

[root@ansible ~]# ansible dockers -m ping -i /etc/dockers -o 192.168.1.33 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"} 192.168.1.32 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"} 192.168.1.31 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"}

Inventory内置参数 #

Ansible Ad-Hoc #

Ad-Hoc中文文档

ad-hoc —— 临时,在 ansible 指需要快速执行且不需要保存的命令。说白了,就是执行一个简单的命令--一个命令。用于复杂的命令。 playbook ,类似于 saltstackstate sls 状态文件。

ansible命令格式 #

1通用命令参数。

[root@ansible ~]# ansible -h Usage: ansible [options] -a MODULE_ARGS #模块参数 -C, --check #检查语法 -f FORKS #并发 --list-hosts #主机列表 -m MODULE_NAME #模块名字 -o 使用精简输出

2)示例

[root@ansible ~]# ansible webserver -m shell -a uptime -o 192.168.1.36 | CHANGED | rc=0 | (stdout) 13:46:14 up 1 day, 9:20, 4 users, load average: 0.00, 0.00, 0.00 192.168.1.33 | CHANGED | rc=0 | (stdout) 21:26:33 up 1 day, 8:51, 3 users, load average: 0.00, 0.01, 0.05 192.168.1.31 | CHANGED | rc=0 | (stdout) 21:26:33 up 1 day, 8:50, 3 users, load average: 0.00, 0.01, 0.05 192.168.1.32 | CHANGED | rc=0 | (stdout) 21:26:33 up 1 day, 8:59, 3 users, load average: 0.00, 0.01, 0.05

3)命令描述

host-pattern格式 #

目标 target 一种主机、主机组匹配方法

主机匹配

目标主机

[root@ansible ~]# ansible 192.168.1.31 -m ping

多个目标主机

[root@ansible ~]# ansible 192.168.1.31,192.168.1.32 -m ping

所有目标主机

[root@ansible ~]# ansible all -m ping

组的匹配

组的配置信息如下:这里定义了一个nginx组和一个apache组

[root@ansible ~]# ansible nginx --list hosts (2): 192.168.1.31 192.168.1.32 [root@ansible ~]# ansible apache --list hosts (3): 192.168.1.36 192.168.1.33 192.168.1.32

组中的所有主机都匹配

[root@ansible ~]# ansible apache -m ping

匹配apache集团拥有,nginx不在组中的所有主机

[root@ansible ~]# ansible apache:!nginx -m ping -o 192.168.1.36 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"} 192.168.1.33 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"}

匹配apache组和nginx组中的所有计算机(串联)

[root@ansible ~]# ansible apache:&nginx -m ping -o 192.168.1.32 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"}

匹配apache组nginx两组所有机器(联合);ansible apache,nginx -m ping

[root@ansible ~]# ansible apache:nginx -m ping -o 192.168.1.32 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"} 192.168.1.31 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"} 192.168.1.33 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"} 192.168.1.36 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"}

版权声明

所有资源都来源于爬虫采集,如有侵权请联系我们,我们将立即删除