Ansible(一)Ansible 安装

一、环境

环境版本
主机操作系统CentOS 7.9.2009
Python版本2.7.5
管理端192.168.0.11
普通主机192.168.0.12
普通主机192.168.0.13
普通主机192.168.0.14
普通主机192.168.0.15
普通主机192.168.0.16

中文文档:https://ansible-tran.readthedocs.io/en/latest/docs/intro.html

二、yum安装

管理主机安装 ansible

yum install -y wget \
&& wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo \
&& yum install -y ansible

查看版本

[root@192 ~]# ansible --version
ansible 2.9.25
  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, Oct 14 2020, 14:45:30) [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)]

配置文件

# ansible 主配置文件
/etc/ansible/ansible.cfg

# ansible 管理主机清单
/etc/ansible/hosts

三、添加主机列表

3.1 禁用公钥认证

编辑 /etc/ansible/ansible.cfg 配置文件,取消 host_key_checking 注释,这样在连接新主机时就不用输入 yes 确认了

[defaults]
host_key_checking = False

如图
在这里插入图片描述

3.2 主机与主机组

Ansible 可同时操作属于一个组的多台主机,组和主机之间的关系通过 inventory 文件配置。默认的文件路径为 /etc/ansible/hosts

示例:

# 示例一:
[nginx]					组名
apache[1:10].aaa.com	表示 apache1.aaa.com 到 apache10.aaa.com 这 10 台机器
nginx[a:z].aaa.com		表示 nginxa.aaa.com 到 nginxz.aaa.com 共 26 台机器
10.1.1.[11:15]			表示 10.1.1.11 到 10.1.1.15 这 5 台机器
10.1.1.13:2222			表示 10.1.1.13 主机 ssh 端口为 2222

# 示例二:
# 定义 10.1.1.12:2222 这台服务器的别名为 nginx1
[nginx]
nginx1 ansible_ssh_host=10.1.1.12 ansible_ssh_port=2222
# 没有做免密登录的服务器可以指定用户名与密码
nginx1 ansible_ssh_host=10.1.1.12 ansible_ssh_port=2222 ansible_ssh_user=root ansible_ssh_pass="123456"

# 示例三:利用别名来分组
nginx1 ansible_ssh_host=10.1.1.12 ansible_ssh_port=2222 ansible_ssh_user=root ansible_ssh_pass="123456"
nginx2 ansible_ssh_host=10.1.1.13

[nginx]
nginx1
nginx2

添加以下配置

nginx	ansible_ssh_host="192.168.0.12"	ansible_ssh_user="root"	ansible_ssh_pass="root"
php1 	ansible_ssh_host="192.168.0.13"	ansible_ssh_user="root"	ansible_ssh_pass="root"
php2 	ansible_ssh_host="192.168.0.14"	ansible_ssh_user="root"	ansible_ssh_pass="root"
mysql 	ansible_ssh_host="192.168.0.15"	ansible_ssh_user="root"	ansible_ssh_pass="root"
redis 	ansible_ssh_host="192.168.0.16"	ansible_ssh_user="root"	ansible_ssh_pass="root"

[Nginx]
nginx

[PHP]
php1
php2

[Mysql]
mysql

[cache]
redis

参数说明

ansible_ssh_host
      将要连接的远程主机名.与你想要设定的主机的别名不同的话,可通过此变量设置.

ansible_ssh_port
      ssh端口号.如果不是默认的端口号,通过此变量设置.

ansible_ssh_user
      默认的 ssh 用户名

ansible_ssh_pass
      ssh 密码(这种方式并不安全,我们强烈建议使用 --ask-pass 或 SSH 密钥)

ansible_sudo_pass
      sudo 密码(这种方式并不安全,我们强烈建议使用 --ask-sudo-pass)

ansible_sudo_exe (new in version 1.8)
      sudo 命令路径(适用于1.8及以上版本)

ansible_connection
      与主机的连接类型.比如:local, ssh 或者 paramiko. Ansible 1.2 以前默认使用 paramiko.1.2 以后默认使用 'smart','smart' 方式会根据是否支持 ControlPersist, 来判断'ssh' 方式是否可行.

ansible_ssh_private_key_file
      ssh 使用的私钥文件.适用于有多个密钥,而你不想使用 SSH 代理的情况.

ansible_shell_type
      目标系统的shell类型.默认情况下,命令的执行使用 'sh' 语法,可设置为 'csh' 或 'fish'.

ansible_python_interpreter
      目标主机的 python 路径.适用于的情况: 系统中有多个 Python, 或者命令路径不是"/usr/bin/python",比如  \*BSD, 或者 /usr/bin/python
      不是 2.X 版本的 Python.我们不使用 "/usr/bin/env" 机制,因为这要求远程用户的路径设置正确,且要求 "python" 可执行程序名不可为 python以外的名字(实际有可能名为python26).

      与 ansible_python_interpreter 的工作方式相同,可设定如 ruby 或 perl 的路径....

四、连通性测试

命令格式如下:

ansible <pattern_goes_here> -m <module_name> -a <arguments>

4.1 测试 Mysql 组

[root@192 ~]# ansible Mysql -m ping
mysql | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}

4.2 测试 PHP 组

[root@192 ~]# ansible PHP -m ping
php1 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
php2 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}

4.3 测试 redis 单个主机

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

4.4 测试所有组

[root@192 ~]# ansible all -m ping
php1 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
php2 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
redis | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
mysql | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
nginx | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值