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
相关推荐
A小辣椒1 天前
TShark:Wireshark CLI 功能
linux
A小辣椒1 天前
TShark:基础知识
linux
AlfredZhao1 天前
OCI 明明分配了 200G 系统盘,为什么 df 只看到 30G?
linux·oci
AlfredZhao2 天前
vi 删除指定范围的行,不用再反复按 dd
linux·vi
用户9718356334662 天前
银河麒麟 KY10 申威(SW64) 安装 nginx-1.16.1-2.p01.ky10.sw_64.rpm 详细步骤
linux
猪脚踏浪2 天前
linux 拷贝文件或目录到指定的位置
linux
大树883 天前
金刚石散热越强,管路越先见顶
大数据·运维·服务器·人工智能·ai
摇滚侠3 天前
Linux CentOS7 rpm 安装 MySQL 5.7
linux·运维·mysql
霸道流氓气质3 天前
领域驱动设计(DDD)在 Spring Boot 微服务中的实践指南
运维·spring boot·微服务
bush43 天前
嵌入式linux学习记录十四、术语
linux·嵌入式