ansible折腾记(一)

2023-02-12  本文已影响0人  随风遣入夜

学前准备

学习思路

安装

yum -y install ansible

验证

ansible --version

设置主机清单

[root@127.0.0.1 ~]# sudo vi /etc/ansible/hosts
# 添加如下内容
[web]
192.168.0.230
192.168.0.15

更改本机名称

[root@127.0.0.1 ~]# hostnamectl set-hostname centos184
[root@127.0.0.1 ~]# hostname
centos184

设置ssh免密登录

[root@centos184 ~]# ssh-keygen -t rsa  <!--生成密钥对-->
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):<!--密钥对存放路径-->
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase):    
       <!--输入私钥保护密码,直接按Enter键表示无密码-->
Enter same passphrase again:    <!--再次输入-->
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:cJz6NRTrvMDxX+Jpce6LRnWI3vVEl/zvARL7D10q9WY root@centos184
The key's randomart image is:
+---[RSA 2048]----+
|          .   . .|
|       . . +   oo|
|      . = o o. oo|
|       = * o..+ *|
|      . S *.=+=*+|
|       . o =+XooE|
|        . ..=.++.|
|           ..o ..|
|           .. o. |
+----[SHA256]-----+
[root@centos184 ~]# ssh-copy-id -i .ssh/id_rsa.pub  root@192.168.0.230   <!--复制公钥到指定远端-->
[root@centos184 ~]# ssh-copy-id -i .ssh/id_rsa.pub  root@192.168.0.15    <!--复制公钥到指定远端-->

测试免密登录

[root@centos184 ~]# ssh root@192.168.0.230
Last failed login: Mon Dec 13 11:17:57 CST 
[root@230 ~]#

用ansible hostname模块 修改230主机hostname

[root@centos184 ~]# ansible 192.168.0.230 -m hostname -a "name=centos230"
192.168.0.230 | CHANGED => {
    "ansible_facts": {
        "ansible_domain": "",
        "ansible_fqdn": "centos230",
        "ansible_hostname": "centos230",
        "ansible_nodename": "centos230",
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "name": "centos230"
}
[root@centos184 ~]#
# 登录进去看主机名是否改变
[root@centos184 ~]# ssh root@192.168.0.230
Last login: Mon Dec 13 11:34:44 2021 from 
Welcome to nokvm, For more information 
[root@centos230 ~]#
# 已经改变 退出即可
[root@centos230 ~]# exit
logout
Connection to 192.168.0.230 closed.

用ansible shell模块 修改230主机hostname

[root@centos184 ~]# ansible web -m shell -a "hostnamectl set-hostname centos230test"
192.168.0.230 | CHANGED | rc=0 >>

[root@centos184 ~]# ansible web -m shell -a "hostname"
192.168.0.230 | CHANGED | rc=0 >>
centos230test
[root@centos184 ~]#

用ansible copy模块将本机的文件复制到web组所有机器上

[root@centos184 ~]# cd /tmp/
[root@centos184 tmp]# ls
[root@centos184 tmp]# echo 'hello world'=> start.txt
[root@centos184 tmp]# ls
start.txt
[root@centos184 tmp]# ansible web -m copy -a "src=/tmp/start.txt dest=/tmp/sss.txt mode=777"
192.168.0.230 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "checksum": "96b79fbf28162c88bfee7bf76cd15ebba1f2e9d8",
    "dest": "/tmp/sss.txt",
    "gid": 0,
    "group": "root",
    "md5sum": "d340e393d20ce0881c27a16c8d08d999",
    "mode": "0777",
    "owner": "root",
    "size": 13,
    "src": "/root/.ansible/tmp/ansible-tmp-1639380182.77-4304-32121682697141/source",
    "state": "file",
    "uid": 0
}
[root@centos184 tmp]# ansible web -m shell -a "ls /tmp"
192.68.0.230 | CHANGED | rc=0 >>
aaa
ansible_command_payload_VybZ9u
sss.txt
[root@centos184 tmp]#

综合实践用ansible 为web组主机安装nginx服务

1.使用yum模块为web组主机添加nginx

ansible web -m yum -a "name=nginx"
# 结果省略...太长了
# 查看安装结果
[root@192 tmp]# ansible web -m shell -a "rpm -qa |grep nginx"
192.168.0.184 | CHANGED | rc=0 >>
nginx-filesystem-1.20.1-9.el7.noarch
nginx-1.20.1-9.el7.x86_64
192.168.0.230 | CHANGED | rc=0 >>
nginx-filesystem-1.20.1-9.el7.noarch
nginx-1.20.1-9.el7.x86_64

2.放开nginx所需端口

# 查看web组主机打开的端口
[root@192 tmp]# ansible web -m shell -a "firewall-cmd --zone=public --list-ports"        192.168.0.184 | CHANGED | rc=0 >>
80/tcp
192.168.0.230 | CHANGED | rc=0 >>
80/tcp
# 我这里是放开了80端口 若没有放开执行下面的
ansible web -m shell -a "firewall-cmd --zone=public --add-port=80/tcp --permanent"
# 重启防火墙
[root@192 tmp]# ansible web -m shell -a "firewall-cmd --reload"                          192.168.0.184 | CHANGED | rc=0 >>
success
192.168.0.230 | CHANGED | rc=0 >>
success


3.启动nginx 服务

[root@192 tmp]# ansible web -m service -a "name=nginx enabled=yes state=started"

4.根据ip访问nginx欢迎页

ansible service模块扩展

service模块为用来管理远程主机上的服务的模块。常见的参数如下:

上一篇下一篇

猜你喜欢

热点阅读