Ansible 基础/命令/配置 (2)

本文介绍了Ansible的HostInventory配置文件,用于管理及分类主机,并展示了如何根据用途和地点进行分组。同时,文章详细讲解了ansible命令的使用,包括指定目标主机、模块、参数等,并通过实例演示了主机连通性检查、文件传输、包安装及系统信息获取等操作。
摘要由CSDN通过智能技术生成

1 Host Inventory

Host Inventory 是配置文件,用来告诉Ansible需要管理哪些主机。并且把这些主机根据按需分类。

可以根据用途分类:数据库节点,服务节点等;根据地点分类:中部,西部机房。

默认的文件是: /etc/ansible/hosts

最简单的hosts文件:

192.168.1.50
aserver.example.org
bserver.example.org

带分类的hosts文件:

mail.example.com

[webservers]
foo.example.com
bar.example.com

[dbservers]
one.example.com
two.example.com
three.example.com

配置此文件:

vim /etc/ansible/hosts

[web1]
192.168.143.192

[web2]
192.168.143.194

:wq

其他常用配置文件:

/etc/ansible:主程序,临时命令执行工具

/etc/ansible-playbook:定制自动化任务,编排剧本工具

/etc/ansible-doc:查看配置文档,模块功能查看工具
    
/etc/ansible-galaxy:上传和下载优秀代码或者Roles模块的官网平台

/etc/ansible-pull:远程执行命令工具

/etc/ansible-console:基于console界面与用户交互的执行工具

2 Ansible 命令

ansible命令的格式是:

ansible <host-pattern> [options]

host-pattern:指定目标主机,可以与/etc/ansible/hosts的格式一样
-m:指定模块名
-a:指定模块参数
-k:输入此选项使用密码认证(不建议使用)
-b:指定sudo到哪个用户,如-b root
-C:检查,不执行
–list-hosts:查看主机列表,可简写–list
-v:显示详细的执行过程,要想更详细的信息-vv、-vvv

例子:

验证是否连通web1 web2

[root@controller ~]# ansible web1 -m ping
192.168.143.192 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "ping": "pong"
}
[root@controller ~]# ansible web2 -m ping
192.168.143.194 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "ping": "pong"
}

检查ansible安装环境
检查所有的远程主机,是否以root用户创建了ansible主机可以访问的环境。

[root@controller ~]# ansible all -m ping -u root
192.168.143.194 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "ping": "pong"
}
192.168.143.192 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "ping": "pong"
}

在所有的远程主机上,以当前bash的同名用户,在远程主机执行“echo bash”

[root@controller ~]# ansible all -a "/bin/echo hello"
192.168.143.192 | CHANGED | rc=0 >>
hello
192.168.143.194 | CHANGED | rc=0 >>
hello

拷贝文件/etc/host到远程主机(组)web1,位置为/tmp/hosts

[root@controller ~]# ansible web1 -m copy -a "src=/etc/hosts dest=/tmp/hosts"
192.168.143.192 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "checksum": "606d67a1d2bafa4b7b30cad2fd321070d057af41",
    "dest": "/tmp/hosts",
    "gid": 0,
    "group": "root",
    "md5sum": "6fee9ae14c8e52ee1f552e29a88b9e7f",
    "mode": "0644",
    "owner": "root",
    "size": 263,
    "src": "/root/.ansible/tmp/ansible-tmp-1649314945.6000264-3681-98402353471501/source",
    "state": "file",
    "uid": 0
}

远程主机(组)web1安装yum包tree

[root@controller ~]# ansible web1 -m yum -a "name=tree state=present"
192.168.143.192 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "msg": "",
    "rc": 0,
    "results": [
        "Installed: tree-1.7.0-15.el8.x86_64"
    ]
}

查看远程主机的全部系统信息

[root@controller ~]# ansible all -m setup
192.168.143.192 | SUCCESS => {
    "ansible_facts": {
        "ansible_all_ipv4_addresses": [
            "10.10.20.192"
        ],
        "ansible_all_ipv6_addresses": [
            "fe80::f816:3eff:fe47:1c0b"
        ],
        "ansible_apparmor": {
            "status": "disabled"
        },
        "ansible_architecture": "x86_64",
        "ansible_bios_date": "04/01/2014",
        "ansible_bios_version": "1.11.0-2.el7",
        "ansible_brq18557e19_f2": {
            "active": true,
            "device": "brq18557e19-f2",
            "features": {
            ......
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不会长胖的斜杠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值