技术改变生活

博客园 首页 新随笔 联系 订阅 管理

 

 

[root@x112 ~]# yum repolist
[root@x112 ~]#ssh-keygen 
[root@x112 ~]#ssh-copy-id 192.168.0.160
[root@x112 ~]#ssh 192.168.0.160

[root@x112 ~]# yum -y install ansible      #需要epel扩展源
[root@x112 ~]# cat /etc/ansible/ansible.cfg

添加模块信息,例如如下:
[root@x112 ~]#  vim /etc/ansible/hosts 
[webserver]
192.168.0.160

[root@x112 ~]# ansible  webserver -m shell -a 'systemctl stop firewalld'
[root@web1 ~]# systemctl status firewalld.service 
firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
   Active: inactive (dead) since Fri 2020-08-07 15:19:44 CST; 29s ago

  

开启inventory
[root@x112 yum.repos.d]# vim /etc/ansible/ansible.cfg
inventory      = /etc/ansible/hosts



[root@x112 yum.repos.d]# vim /etc/ansible/hosts
[root@x112 yum.repos.d]# grep -Ev '^[$|#]' /etc/ansible/hosts
[webserver]            #定义主机组的名称
192.168.0.61           #主机组中的主机地址 
192.168.0.62
192.168.0.63
192.168.0.64
192.168.0.65
[root@x112 yum.repos.d]#
[root@x112 yum.repos.d]# ansible webserver --list-host
  hosts (5):
    192.168.0.61
    192.168.0.62
    192.168.0.63
    192.168.0.64
    192.168.0.65
[root@x112 yum.repos.d]# ansible webserver -m ping 
#命令格式,ansible  主机组名称   -m  模块名称   -a 模块参数

[root@x112 yum.repos.d]# ansible webserver -m ping
192.168.0.63 | UNREACHABLE! => {
    "changed": false, 
    "msg": "Failed to connect to the host via ssh: ssh: connect to host 192.168.0.63 port 22: No route to host", 
    "unreachable": true
}
192.168.0.65 | UNREACHABLE! => {
    "changed": false, 
    "msg": "Failed to connect to the host via ssh: ssh: connect to host 192.168.0.65 port 22: No route to host", 
    "unreachable": true
}

  

  

posted on 2020-08-07 15:30  小阿峰  阅读(145)  评论(0编辑  收藏  举报