ansible常用命令的简单练习

ansible常用命令的简单练习

用ansible临时命令完成以下操作

1、对node1主机操作,安装httpd服务,网页存放在/www目录中,能够通过curl http://node1访问到网页内容为welcome to luoqi
(1)配置yum仓库并安装httpd服务
powershell 复制代码
[student@master ansible]$ ansible node1 -m yum_repository -a 'file=server name=aa description=aa1 baseurl=http://ansible.example.com/rhel9/BaseOS enabled=yes gpgcheck=no'
node1 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": true,
    "repo": "aa",
    "state": "present"
}
[student@master ansible]$ ansible node1 -m yum_repository -a 'file=server name=bb description=bb1 baseurl=http://ansible.example.com/rhel9/AppStream enabled=yes gpgcheck=no'
node1 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": true,
    "repo": "bb",
    "state": "present"
}
[student@master ansible]$ ansible node1 -m yum -a 'name=httpd state=present'
node1 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": true,
    "msg": "",
    "rc": 0,
    "results": [
        "Installed: redhat-logos-httpd-90.4-1.el9.noarch",
        "Installed: mailcap-2.1.49-5.el9.noarch",
        "Installed: httpd-2.4.53-7.el9.x86_64",
        "Installed: apr-1.7.0-11.el9.x86_64",
        "Installed: mod_http2-1.15.19-2.el9.x86_64",
        "Installed: apr-util-1.6.1-20.el9.x86_64",
        "Installed: apr-util-bdb-1.6.1-20.el9.x86_64",
        "Installed: httpd-core-2.4.53-7.el9.x86_64",
        "Installed: httpd-filesystem-2.4.53-7.el9.noarch",
        "Installed: httpd-tools-2.4.53-7.el9.x86_64",
        "Installed: apr-util-openssl-1.6.1-20.el9.x86_64",
        "Installed: mod_lua-2.4.53-7.el9.x86_64"
    ]
}
(2)创建/www的目录设置其context值为httpd_sys_content_t
powershell 复制代码
[student@master ansible]$ ansible node1 -m file -a 'path=/www state=directory setype=httpd_sys_content_t'
node1 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": true,
    "gid": 0,
    "group": "root",
    "mode": "0755",
    "owner": "root",
    "path": "/www",
    "secontext": "unconfined_u:object_r:httpd_sys_content_t:s0",
    "size": 6,
    "state": "directory",
    "uid": 0
}
(3)在/www下写入一个index.html的文件,其内容为welcome to luoqi,设置其context值为httpd_sys_content_t
powershell 复制代码
[student@master ansible]$ ansible node1 -m copy -a 'content="welcome to luoqi" dest=/www/index.html setype=httpd_sys_content_t'
node1 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": true,
    "checksum": "2433ad103a9ed17b296822118d46955ba31ad8ef",
    "dest": "/www/index.html",
    "gid": 0,
    "group": "root",
    "md5sum": "fd2ab3cea037b6fdd568d5014b787c14",
    "mode": "0644",
    "owner": "root",
    "secontext": "system_u:object_r:httpd_sys_content_t:s0",
    "size": 16,
    "src": "/home/student/.ansible/tmp/ansible-tmp-1756436213.0637414-868-126882287786795/source",
    "state": "file",
    "uid": 0
}
(4)替换配置文件中的内容(http访问的目录改为/www,有两个地方需要修改)
powershell 复制代码
[student@master ansible]$ ansible node1 -m replace -a 'path=/etc/httpd/conf/httpd.conf regexp="DocumentRoot "/var/www/html"" replace="DocumentRoot "/www""'
node1 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": true,
    "msg": "1 replacements made",
    "rc": 0
}
[student@master ansible]$ ansible node1 -m replace -a 'path=/etc/httpd/conf/httpd.conf regexp="<Directory "/var/www">" replace="<Directory "/www">"'
node1 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": true,
    "msg": "1 replacements made",
    "rc": 0
}
(5)设置防火墙(允许访问http服务)
powershell 复制代码
[student@master ansible]$ ansible node1 -m firewalld -a 'service=http permanent=yes state=enabled immediate=yes'
node1 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": true,
    "msg": "Permanent and Non-Permanent(immediate) operation, Changed service http to enabled"
}
(6)起服务
powershell 复制代码
[student@master ansible]$ ansible node1 -m service -a 'name=httpd state=restarted enabled=yes'
node1 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
......

[student@master ansible]$ curl http://node1
welcome to luoqi[student@master ansible]$ 
2、对node2主机操作,创建一个1000MiB的分区,格式化成ext4的文件系统,并挂载到/testdir目录下。

使用ansible node2 -m shell -a 'df -Th'验证

(1)添加一块硬盘(略)
(2)创建分区
powershell 复制代码
[student@master ansible]$ ansible node2 -m parted -a 'device=/dev/vdb number=1 part_type=primary part_start=10MiB part_end=1010MiB state=present'
node2 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": true,
    "disk": {
        "dev": "/dev/vdb",
        "logical_block": 512,
        "model": "Virtio Block Device",
        "physical_block": 512,
        "size": 20971520.0,
        "table": "msdos",
        "unit": "kib"
    },
    "partitions": [
        {
            "begin": 10240.0,
            "end": 1034240.0,
            "flags": [],
            "fstype": "",
            "name": "",
            "num": 1,
            "size": 1024000.0,
            "unit": "kib"
        }
    ],
    "script": "unit KiB mklabel msdos mkpart primary 10MiB 1010MiB"
}
(3)格式化(文件系统),并查看UUID
powershell 复制代码
[student@master ansible]$ ansible node2 -m filesystem -a 'dev=/dev/vdb1 fstype=ext4'
node2 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": true
}
[student@master ansible]$ ansible node2 -m shell -a 'blkid /dev/vdb1'
node2 | CHANGED | rc=0 >>
/dev/vdb1: UUID="ab990f13-b8d4-4215-aebb-85f5cf2f519a" TYPE="ext4" PARTUUID="88bd718e-01"
(4)创建挂载目录,并挂载
powershell 复制代码
[student@master ansible]$ ansible node2 -m file -a 'path=/testdir state=directory'
node2 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": true,
    "gid": 0,
    "group": "root",
    "mode": "0755",
    "owner": "root",
    "path": "/testdir",
    "secontext": "unconfined_u:object_r:default_t:s0",
    "size": 6,
    "state": "directory",
    "uid": 0
}
[student@master ansible]$ ansible node2 -m mount -a 'src=UUID=ab990f13-b8d4-4215-aebb-85f5cf2f519a path=/testdir fstype=ext4 state=mounted'
node2 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "backup_file": "",
    "boot": "yes",
    "changed": true,
    "dump": "0",
    "fstab": "/etc/fstab",
    "fstype": "ext4",
    "name": "/testdir",
    "opts": "defaults",
    "passno": "0",
    "src": "UUID=ab990f13-b8d4-4215-aebb-85f5cf2f519a"
}
[student@master ansible]$ ansible node2 -m shell -a 'df -Th'
node2 | CHANGED | rc=0 >>
Filesystem     Type      Size  Used Avail Use% Mounted on
devtmpfs       devtmpfs  4.0M     0  4.0M   0% /dev
tmpfs          tmpfs     985M     0  985M   0% /dev/shm
tmpfs          tmpfs     394M  5.6M  389M   2% /run
/dev/vda3      xfs        17G  1.2G   16G   7% /
/dev/vda1      xfs      1014M  182M  833M  18% /boot
tmpfs          tmpfs     197M     0  197M   0% /run/user/0
tmpfs          tmpfs     197M     0  197M   0% /run/user/1000
/dev/vdb1      ext4      966M   24K  900M   1% /testdir
3、对node3主机操作创建卷组datastorage,逻辑卷database,大小为800M,格式化为xfs的文件系统,并挂载到/lv目录下

使用ansible node3 -m shell -a 'df -Th'验证

(1)添加一块硬盘
(2)创建分区
powershell 复制代码
[student@master ansible]$ ansible node3 -m parted -a 'device=/dev/vdb number=1 part_type=primary part_start=10MiB part_end=1010MiB state=present'
node3 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": true,
    "disk": {
        "dev": "/dev/vdb",
        "logical_block": 512,
        "model": "Virtio Block Device",
        "physical_block": 512,
        "size": 20971520.0,
        "table": "msdos",
        "unit": "kib"
    },
    "partitions": [
        {
            "begin": 10240.0,
            "end": 1034240.0,
            "flags": [],
            "fstype": "",
            "name": "",
            "num": 1,
            "size": 1024000.0,
            "unit": "kib"
        }
    ],
    "script": "unit KiB mklabel msdos mkpart primary 10MiB 1010MiB"
}
(3)配置yum仓库并安装lvm2
powershell 复制代码
[student@master ansible]$ ansible node3 -m yum_repository -a 'file=server name=aa description=aa1 baseurl=http://ansible.example.com/rhel9/BaseOS enabled=yes gpgcheck=no'
node3 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": true,
    "repo": "aa",
    "state": "present"
}
[student@master ansible]$ ansible node3 -m yum_repository -a 'file=server name=bb description=bb1 baseurl=http://ansible.example.com/rhel9/AppStream enabled=yes gpgcheck=no'
node3 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": true,
    "repo": "bb",
    "state": "present"
}
[student@master ansible]$ ansible node3 -m yum -a 'name=lvm2 state=present'
node3 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": true,
    "msg": "",
    "rc": 0,
    "results": [
        "Installed: device-mapper-libs-9:1.02.185-3.el9.x86_64",
        "Installed: lvm2-9:2.03.16-3.el9.x86_64",
        "Installed: lvm2-libs-9:2.03.16-3.el9.x86_64",
        "Installed: device-mapper-persistent-data-0.9.0-13.el9.x86_64",
        "Installed: libaio-0.3.111-13.el9.x86_64",
        "Installed: device-mapper-9:1.02.185-3.el9.x86_64",
        "Installed: device-mapper-event-9:1.02.185-3.el9.x86_64",
        "Installed: device-mapper-event-libs-9:1.02.185-3.el9.x86_64",
        "Removed: device-mapper-9:1.02.187-7.el9.x86_64",
        "Removed: device-mapper-libs-9:1.02.187-7.el9.x86_64"
    ]
}
(4)创建卷组
powershell 复制代码
[student@master ansible]$ ansible node3 -m lvg -a 'vg=datasorage pvs=/dev/vdb1'
node3 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": true
}
(5)创建逻辑卷
powershell 复制代码
[student@master ansible]$ ansible node3 -m lvol -a 'lv=database vg=datasorage size=800M'node3 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": true,
    "msg": ""
}
(6)格式化(文件系统),并查询UUID
powershell 复制代码
[student@master ansible]$ ansible node3 -m filesystem -a 'dev=/dev/datasorage/database fstype=xfs'
node3 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": true
}
[student@master ansible]$ ansible node3 -m shell -a 'blkid /dev/datasorage/database'
node3 | CHANGED | rc=0 >>
/dev/datasorage/database: UUID="4af0ca97-a6ec-441c-a36f-9e85352eac4b" TYPE="xfs"
(7)创建挂载目录,并挂载
powershell 复制代码
[student@master ansible]$ ansible node3 -m file -a 'path=/lv state=directory'
node3 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": true,
    "gid": 0,
    "group": "root",
    "mode": "0755",
    "owner": "root",
    "path": "/lv",
    "secontext": "unconfined_u:object_r:default_t:s0",
    "size": 6,
    "state": "directory",
    "uid": 0
}
[student@master ansible]$ ansible node3 -m mount -a 'src="UUID=4af0ca97-a6ec-441c-a36f-9e85352eac4b" path=/lv fstype=xfs state=mounted'
node3 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "backup_file": "",
    "boot": "yes",
    "changed": true,
    "dump": "0",
    "fstab": "/etc/fstab",
    "fstype": "xfs",
    "name": "/lv",
    "opts": "defaults",
    "passno": "0",
    "src": "UUID=4af0ca97-a6ec-441c-a36f-9e85352eac4b"
}
[student@master ansible]$ ansible node3 -m shell -a 'df -Th'
node3 | CHANGED | rc=0 >>
Filesystem                      Type      Size  Used Avail Use% Mounted on
devtmpfs                        devtmpfs  4.0M     0  4.0M   0% /dev
tmpfs                           tmpfs     985M     0  985M   0% /dev/shm
tmpfs                           tmpfs     394M  5.6M  389M   2% /run
/dev/vda3                       xfs        17G  1.2G   16G   8% /
/dev/vda1                       xfs      1014M  182M  833M  18% /boot
tmpfs                           tmpfs     197M     0  197M   0% /run/user/0
tmpfs                           tmpfs     197M     0  197M   0% /run/user/1000
/dev/mapper/datasorage-database xfs       794M   38M  757M   5% /lv
相关推荐
岁岁种桃花儿8 分钟前
详解 Kubernetes 命令:kubectl exec -it nginx -- bash 及实战场景
运维·nginx·kubernetes
小牛马爱写博客17 分钟前
DNS 服务器与 DHCP 服务器详解及配置指南
linux·运维·服务器·dns·dhcp
维尔切19 分钟前
HAProxy 负载均衡器
linux·运维·数据库·负载均衡
什么半岛铁盒20 分钟前
C++项目:仿muduo库高并发服务器-------Channel模块实现
linux·服务器·数据库·c++·mysql·ubuntu
2503_9248068524 分钟前
动态IP使用中 报错407 怎么办???
服务器·tcp/ip·php
VueVirtuoso24 分钟前
前后端部署 + Nginx 配置 + Cloudflare 全攻略(通俗易懂版)
运维·nginx
QQ129584550432 分钟前
服务器跨域问题CORS的解决
运维·服务器
小白银子34 分钟前
零基础从头教学Linux(Day 42)
linux·运维·服务器·网络·nginx
DDC楼宇自控与IBMS集成系统解读1 小时前
园区3D可视化数字孪生管理平台与 IBMS 智能化集成系统:打造智慧园区新范式
运维·3d可视化·楼宇自控系统·数字孪生管理平台·ibms集成系统·3d可视化数字孪生管理平台·智能化集成系统
望获linux1 小时前
【Linux基础知识系列:第一百四十篇】理解SELinux与系统安全
linux·运维·服务器·数据库·chrome·macos