Ansible自动化运维工具之常用模块使用实战(6)

本文涉及的产品
云数据库 RDS SQL Server,独享型 2核4GB
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介: Ansible自动化运维工具之常用模块使用实战(6)

6.lineinfile|replace模块

20.png

- lineinfile


[root@k8s_node1 ~]# cat /etc/my.cnf   //查看托管主机k8s_node1上my.cnf文件的内容
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
binlog-format=mixed     ---》k8s_node1主机上binlog-format为mixed模式
...
[root@k8s_node2 ~]# cat /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
binlog-format=mixed    ---》k8s_node2主机上binlog-format为mixed模式
...
[root@k8s_master1 ~]# ansible k8s_node -m lineinfile -a 'path=/etc/my.cnf regexp="^binlog-format" line="binlog-format=row"'   //远程将目标文件/etc/my.cnf中匹配以binlog-format开头的行,修改为binlog-format=row
k8s_node2 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "backup": "",
    "changed": true,
    "msg": "line replaced"
}
k8s_node1 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "backup": "",
    "changed": true,
    "msg": "line replaced"
}
[root@k8s_node1 ~]# cat /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
binlog-format=row   ---》修改完成后,k8s_node1主机上binlog-format已经变成row模式
...
[root@k8s_node2 ~]# cat /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
binlog-format=row  ---》修改完成后,k8s_node2主机上binlog-format已经变成row模式
...

也可以使用lineinfile模块将文件内容修改为空

[root@k8s_node1 ~]# cat /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
binlog-format=row
[root@k8s_master1 ~]# ansible k8s_node -m lineinfile -a 'path=/etc/my.cnf regexp="^binlog-format" line=" "'           k8s_node2 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "backup": "",
    "changed": true,
    "msg": "line replaced"
}
k8s_node1 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "backup": "",
    "changed": true,
    "msg": "line replaced"
}
[root@k8s_node1 ~]# cat /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
...

21.png

- replace


[root@k8s_node1 ~]# cat /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
binlog-format=row
...
[root@k8s_node2 ~]# cat /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
binlog-format=row
...
[root@k8s_master1 ~]# ansible k8s_node -m replace -a 'path=/etc/my.cnf regexp="row" replace="mixed"'   //远程将目标文件/etc/my.cnf文件中匹配的row,替换为mixed
k8s_node2 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "msg": "1 replacements made"
}
k8s_node1 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "msg": "1 replacements made"
}
[root@k8s_node1 ~]# cat /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
binlog-format=mixed
...
[root@k8s_node2 ~]# cat /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
binlog-format=mixed
...

7.yum模块

22.png23.png

- yum


 安装

[root@k8s_node1 ~]# yum list installed | grep unzip
[root@k8s_node2 ~]# yum list installed | grep unzip
--》可以看到k8s_node1主机与k8s_node2主机上都没有安装unzip
[root@k8s_master1 ~]# ansible k8s_node -m yum -a 'name="unzip" state=installed'  //远程为k8s_node主机组中的主机安装unzip软件
k8s_node1 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "changes": {
        "installed": [
            "unzip"
        ]
    },
    "msg": "",
    "rc": 0,
    "results": [
        "Loaded plugins: fastestmirror\nLoading mirror speeds from cached hostfile\n * base: mirrors.bfsu.edu.cn\n * extras: mirrors.bfsu.edu.cn\n * updates: mirrors.bfsu.edu.cn\nResolving Dependencies\n--> Running transaction check\n---> Package unzip.x86_64 0:6.0-22.el7_9 will be installed\n--> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package        Arch            Version                  Repository        Size\n================================================================================\nInstalling:\n unzip          x86_64          6.0-22.el7_9             updates          171 k\n\nTransaction Summary\n================================================================================\nInstall  1 Package\n\nTotal download size: 171 k\nInstalled size: 365 k\nDownloading packages:\nRunning transaction check\nRunning transaction test\nTransaction test succeeded\nRunning transaction\n  Installing : unzip-6.0-22.el7_9.x86_64                                    1/1 \n  Verifying  : unzip-6.0-22.el7_9.x86_64                                    1/1 \n\nInstalled:\n  unzip.x86_64 0:6.0-22.el7_9                                                   \n\nComplete!\n"
    ]
}
k8s_node2 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "changes": {
        "installed": [
            "unzip"
        ]
    },
    "msg": "",
    "rc": 0,
    "results": [
        "Loaded plugins: fastestmirror\nLoading mirror speeds from cached hostfile\n * base: mirrors.bfsu.edu.cn\n * extras: mirrors.bfsu.edu.cn\n * updates: mirrors.bfsu.edu.cn\nResolving Dependencies\n--> Running transaction check\n---> Package unzip.x86_64 0:6.0-22.el7_9 will be installed\n--> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package        Arch            Version                  Repository        Size\n================================================================================\nInstalling:\n unzip          x86_64          6.0-22.el7_9             updates          171 k\n\nTransaction Summary\n================================================================================\nInstall  1 Package\n\nTotal download size: 171 k\nInstalled size: 365 k\nDownloading packages:\nRunning transaction check\nRunning transaction test\nTransaction test succeeded\nRunning transaction\n  Installing : unzip-6.0-22.el7_9.x86_64                                    1/1 \n  Verifying  : unzip-6.0-22.el7_9.x86_64                                    1/1 \n\nInstalled:\n  unzip.x86_64 0:6.0-22.el7_9                                                   \n\nComplete!\n"
    ]
}
[root@k8s_node1 ~]# yum list installed | grep unzip
unzip.x86_64                         6.0-22.el7_9                   @updates
[root@k8s_node2 ~]# yum list installed | grep unzip
unzip.x86_64                         6.0-22.el7_9                   @updates
--》可以看到现在k8s_node主机组中的主机都已经安装好了unzip软件

 卸载

[root@k8s_master1 ~]# ansible k8s_node -m yum -a 'name="unzip" state=removed'
k8s_node2 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "changes": {
        "removed": [
            "unzip"
        ]
    },
    "msg": "",
    "rc": 0,
    "results": [
        "已加载插件:fastestmirror\n正在解决依赖关系\n--> 正在检查事务\n---> 软件包 unzip.x86_64.0.6.0-22.el7_9 将被 删除\n--> 解决依赖关系完成\n\n依赖关系解决\n\n================================================================================\n Package        架构            版本                    源                 大小\n================================================================================\n正在删除:\n unzip          x86_64          6.0-22.el7_9            @updates          365 k\n\n事务概要\n================================================================================\n移除  1 软件包\n\n安装大小:365 k\nDownloading packages:\nRunning transaction check\nRunning transaction test\nTransaction test succeeded\nRunning transaction\n  正在删除    : unzip-6.0-22.el7_9.x86_64                                   1/1 \n  验证中      : unzip-6.0-22.el7_9.x86_64                                   1/1 \n\n删除:\n  unzip.x86_64 0:6.0-22.el7_9                                                   \n\n完毕!\n"
    ]
}
k8s_node1 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "changes": {
        "removed": [
            "unzip"
        ]
    },
    "msg": "",
    "rc": 0,
    "results": [
        "已加载插件:fastestmirror\n正在解决依赖关系\n--> 正在检查事务\n---> 软件包 unzip.x86_64.0.6.0-22.el7_9 将被 删除\n--> 解决依赖关系完成\n\n依赖关系解决\n\n================================================================================\n Package        架构            版本                    源                 大小\n================================================================================\n正在删除:\n unzip          x86_64          6.0-22.el7_9            @updates          365 k\n\n事务概要\n================================================================================\n移除  1 软件包\n\n安装大小:365 k\nDownloading packages:\nRunning transaction check\nRunning transaction test\nTransaction test succeeded\nRunning transaction\n  正在删除    : unzip-6.0-22.el7_9.x86_64                                   1/1 \n  验证中      : unzip-6.0-22.el7_9.x86_64                                   1/1 \n\n删除:\n  unzip.x86_64 0:6.0-22.el7_9                                                   \n\n完毕!\n"
    ]
}
[root@k8s_node1 ~]# yum list installed | grep unzip
[root@k8s_node2 ~]# yum list installed | grep unzip

8.service模块

24.png

- service


[root@k8s_node1 ~]# ss -antulp | grep sshd
tcp    LISTEN     0      128       *:22                    *:*                   users:(("sshd",pid=9818,fd=3))
tcp    LISTEN     0      128      :::22                   :::*                   users:(("sshd",pid=9818,fd=4))
[root@k8s_node2 ~]# ss -antulp | grep sshd
tcp    LISTEN     0      128       *:22                    *:*                   users:(("sshd",pid=892,fd=3))
tcp    LISTEN     0      128      :::22                   :::*                   users:(("sshd",pid=892,fd=4))
[root@k8s_master1 ~]# ansible k8s_node -m service -a 'name="sshd" enabled="yes" state="stopped"'
k8s_node1 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "enabled": true,
    "name": "sshd",
    "state": "stopped",
    "status": {
        "ActiveEnterTimestamp": "二 2021-08-10 06:53:43 CST",
        "ActiveEnterTimestampMonotonic": "47952521833",
        "ActiveExitTimestamp": "二 2021-08-10 06:53:43 CST",
        "ActiveExitTimestampMonotonic": "47952513940",
        "ActiveState": "active",
        "After": "sshd-keygen.service system.slice network.target basic.target systemd-journald.socket",
        "AllowIsolate": "no",
        "AmbientCapabilities": "0",
        "AssertResult": "yes",
        "AssertTimestamp": "二 2021-08-10 06:53:43 CST",
        "AssertTimestampMonotonic": "47952515333",
        "Before": "shutdown.target multi-user.target",
        "BlockIOAccounting": "no",
        "BlockIOWeight": "18446744073709551615",
        ......
 k8s_node2 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "enabled": true,
    "name": "sshd",
    "state": "stopped",
    "status": {
        "ActiveEnterTimestamp": "二 2021-08-10 00:48:37 CST",
        "ActiveEnterTimestampMonotonic": "6044168",
        "ActiveExitTimestampMonotonic": "0",
        "ActiveState": "active",
        "After": "system.slice network.target basic.target sshd-keygen.service systemd-journald.socket",
        "AllowIsolate": "no",
        "AmbientCapabilities": "0",
        "AssertResult": "yes",
        "AssertTimestamp": "二 2021-08-10 00:48:37 CST",
        "AssertTimestampMonotonic": "5883708",
        "Before": "multi-user.target shutdown.target",
        "BlockIOAccounting": "no",
        "BlockIOWeight": "18446744073709551615",
        "CPUAccounting": "no",
        "CPUQuotaPerSecUSec": "infinity",
        ......
[root@k8s_node1 ~]# ss -antulp | grep sshd
[root@k8s_node2 ~]# ss -antulp | grep sshd

9.setup模块

25.png

- setup


 filter参数:用于进行条件过滤。如果设置,仅返回匹配过滤条件的信息。

[root@k8s_master1 ~]# ansible k8s_node -m setup  //不加任何参数返回全部信息
k8s_node2 | SUCCESS => {
    "ansible_facts": {
        "ansible_all_ipv4_addresses": [
            "192.168.1.20"
        ],
        "ansible_all_ipv6_addresses": [
            "240e:398:390:6920::2",
            "240e:398:390:6920:56c7:8737:b55b:39ce",
            "fe80::f61a:9077:82e9:caa9"
        ],
        "ansible_apparmor": {
            "status": "disabled"
        },
        "ansible_architecture": "x86_64",
        "ansible_bios_date": "04/13/2018",
        "ansible_bios_version": "6.00",
        "ansible_cmdline": {
            "BOOT_IMAGE": "/vmlinuz-3.10.0-862.el7.x86_64",
            "LANG": "zh_CN.UTF-8",
            "quiet": true,
            "rd.lvm.lv": "centos/swap",
            "rhgb": true,
            "ro": true,
            "root": "/dev/mapper/centos-root"
        },
        "ansible_date_time": {
            "date": "2021-08-13",
            "day": "13",
            "epoch": "1628838721",
            "hour": "15",
            "iso8601": "2021-08-13T07:12:01Z",
            "iso8601_basic": "20210813T151201688740",
            "iso8601_basic_short": "20210813T151201",
            "iso8601_micro": "2021-08-13T07:12:01.688740Z",
            "minute": "12",
            "month": "08",
            "second": "01",
            "time": "15:12:01",
            ......

--》可以看到不加任何参数时,返回的信息非常多,很多情况我们并不需要全部的信息,可以通过加入过滤参数来获得指定的信息。


--常用的信息参数如下--:


  • ansible_all_ipv4_addresses:仅显示ipv4的信息。


  • ansible_devices:仅显示磁盘设备信息。


  • ansible_distribution:显示是什么系统,例:centos,suse等。


  • ansible_distribution_major_version:显示是系统主版本。


  • ansible_distribution_version:仅显示系统版本。


  • ansible_machine:显示系统类型,例:32位,还是64位。


  • ansible_eth0:仅显示eth0的信息。


  • ansible_hostname:仅显示主机名。


  • ansible_kernel:仅显示内核版本。


  • ansible_lvm:显示lvm相关信息。


  • ansible_memtotal_mb:显示系统总内存


  • ansible_memfree_mb:显示可用系统内存


  • ansible_memory_mb:详细显示内存情况。


  • ansible_swaptotal_mb:显示总的swap内存。


  • ansible_swapfree_mb:显示swap内存的可用内存。


  • ansible_mounts:显示系统磁盘挂载情况。


  • ansible_processor:显示cpu个数(具体显示每个cpu的型号)。


  • ansible_processor_vcpus:显示cpu个数(只显示总的个数)。


 1.远程获取两台被托管主机的ansible_date_time信息

[root@k8s_master1 ~]# ansible k8s_node -m setup -a 'filter=ansible_date_time'
k8s_node2 | SUCCESS => {
    "ansible_facts": {
        "ansible_date_time": {
            "date": "2021-08-13",
            "day": "13",
            "epoch": "1628839117",
            "hour": "15",
            "iso8601": "2021-08-13T07:18:37Z",
            "iso8601_basic": "20210813T151837817990",
            "iso8601_basic_short": "20210813T151837",
            "iso8601_micro": "2021-08-13T07:18:37.817990Z",
            "minute": "18",
            "month": "08",
            "second": "37",
            "time": "15:18:37",
            "tz": "CST",
            "tz_offset": "+0800",
            "weekday": "星期五",
            "weekday_number": "5",
            "weeknumber": "32",
            "year": "2021"
        },
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false
}
k8s_node1 | SUCCESS => {
    "ansible_facts": {
        "ansible_date_time": {
            "date": "2021-08-13",
            "day": "13",
            "epoch": "1628809519",
            "hour": "07",
            "iso8601": "2021-08-12T23:05:19Z",
            "iso8601_basic": "20210813T070519109683",
            "iso8601_basic_short": "20210813T070519",
            "iso8601_micro": "2021-08-12T23:05:19.109683Z",
            "minute": "05",
            "month": "08",
            "second": "19",
            "time": "07:05:19",
            "tz": "CST",
            "tz_offset": "+0800",
            "weekday": "星期五",
            "weekday_number": "5",
            "weeknumber": "32",
            "year": "2021"
        },
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false
}

 2.远程获取两台被托管主机的filter=ansible_all_ipv4_addresses地址IP信息

[root@k8s_master1 ~]# ansible k8s_node -m setup -a 'filter=ansible_all_ipv4_addresses'
k8s_node1 | SUCCESS => {
    "ansible_facts": {
        "ansible_all_ipv4_addresses": [
            "192.168.1.19"
        ],
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false
}
k8s_node2 | SUCCESS => {
    "ansible_facts": {
        "ansible_all_ipv4_addresses": [
            "192.168.1.20"
        ],
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false
}

 3.远程获取两台被托管主机的filter=ansible_memory_mb内存信息

[root@k8s_master1 ~]# ansible k8s_node -m setup -a 'filter=ansible_memory_mb'
k8s_node1 | SUCCESS => {
    "ansible_facts": {
        "ansible_memory_mb": {
            "nocache": {
                "free": 1814,
                "used": 168
            },
            "real": {
                "free": 1555,
                "total": 1982,
                "used": 427
            },
            "swap": {
                "cached": 0,
                "free": 2047,
                "total": 2047,
                "used": 0
            }
        },
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false
}
k8s_node2 | SUCCESS => {
    "ansible_facts": {
        "ansible_memory_mb": {
            "nocache": {
                "free": 1816,
                "used": 166
            },
            "real": {
                "free": 1561,
                "total": 1982,
                "used": 421
            },
            "swap": {
                "cached": 0,
                "free": 2047,
                "total": 2047,
                "used": 0
            }
        },
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false
}
相关实践学习
容器服务Serverless版ACK Serverless 快速入门:在线魔方应用部署和监控
通过本实验,您将了解到容器服务Serverless版ACK Serverless 的基本产品能力,即可以实现快速部署一个在线魔方应用,并借助阿里云容器服务成熟的产品生态,实现在线应用的企业级监控,提升应用稳定性。
云原生实践公开课
课程大纲 开篇:如何学习并实践云原生技术 基础篇: 5 步上手 Kubernetes 进阶篇:生产环境下的 K8s 实践 相关的阿里云产品:容器服务 ACK 容器服务 Kubernetes 版(简称 ACK)提供高性能可伸缩的容器应用管理能力,支持企业级容器化应用的全生命周期管理。整合阿里云虚拟化、存储、网络和安全能力,打造云端最佳容器化应用运行环境。 了解产品详情: https://www.aliyun.com/product/kubernetes
相关文章
|
22天前
|
Web App开发 人工智能 自然语言处理
【人工智能时代】AI赋能编程 | 自动化工具助力高效办公
【人工智能时代】AI赋能编程 | 自动化工具助力高效办公
【人工智能时代】AI赋能编程 | 自动化工具助力高效办公
|
2月前
|
敏捷开发 API
【sgCreateAPI】自定义小工具:敏捷开发→自动化生成API接口脚本(接口代码生成工具)
【sgCreateAPI】自定义小工具:敏捷开发→自动化生成API接口脚本(接口代码生成工具)
|
1月前
|
设计模式 Java 测试技术
软件测试/测试开发/全日制|Page Object模式:为什么它是Web自动化测试的必备工具
软件测试/测试开发/全日制|Page Object模式:为什么它是Web自动化测试的必备工具
41 0
|
2月前
|
机器学习/深度学习 人工智能 算法
软件测试/人工智能|人工智能与自动化测试结合实战-探索人工智能在测试领域中的应用
软件测试/人工智能|人工智能与自动化测试结合实战-探索人工智能在测试领域中的应用
61 0
|
24天前
|
Web App开发 前端开发 测试技术
如何对自动化工具选型?
如何对自动化工具选型?
|
1天前
|
Web App开发 测试技术 数据安全/隐私保护
Web自动化测试工具Selenium
Web自动化测试工具Selenium
|
2天前
|
测试技术 API Python
Python自动化测试:unittest与pytest的实战技巧
Python自动化测试:unittest与pytest的实战技巧
|
4天前
|
监控 测试技术 API
自动化测试工具与电脑桌面监控软件的集成:Selenium与Python的无缝整合
在当今数字化时代,软件质量保证是每个软件开发团队都必须面对的重要挑战之一。自动化测试工具和电脑桌面监控软件的结合,为开发团队提供了一种有效的方式来确保软件的稳定性和性能。本文将介绍如何利用Python编程语言中的Selenium库,与桌面监控软件进行无缝整合,以实现对应用程序的自动化测试和桌面监控。
22 5
|
23天前
|
Java jenkins 测试技术
常见自动化测试框架和工具
常见自动化测试框架和工具
|
24天前
|
存储 测试技术 定位技术
新一代自动化测试工具需要具备的功能
新一代自动化测试工具需要具备的功能

热门文章

最新文章

相关产品

  • 云迁移中心