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

2022-06-22 15:56:26 浏览数 (1)

6.lineinfile|replace模块

在这里插入图片描述 - lineinfile

代码语言:javascript复制
[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模块将文件内容修改为空

代码语言:javascript复制
[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

...

在这里插入图片描述 - replace

代码语言:javascript复制
[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模块

在这里插入图片描述

在这里插入图片描述 - yum

 安装

代码语言:javascript复制
[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: fastestmirrornLoading mirror speeds from cached hostfilen * base: mirrors.bfsu.edu.cnn * extras: mirrors.bfsu.edu.cnn * updates: mirrors.bfsu.edu.cnnResolving Dependenciesn--> Running transaction checkn---> Package unzip.x86_64 0:6.0-22.el7_9 will be installedn--> Finished Dependency ResolutionnnDependencies Resolvednn================================================================================n Package        Arch            Version                  Repository        Sizen================================================================================nInstalling:n unzip          x86_64          6.0-22.el7_9             updates          171 knnTransaction Summaryn================================================================================nInstall  1 PackagennTotal download size: 171 knInstalled size: 365 knDownloading packages:nRunning transaction checknRunning transaction testnTransaction test succeedednRunning transactionn  Installing : unzip-6.0-22.el7_9.x86_64                                    1/1 n  Verifying  : unzip-6.0-22.el7_9.x86_64                                    1/1 nnInstalled:n  unzip.x86_64 0:6.0-22.el7_9                                                   nnComplete!n"
    ]
}
k8s_node2 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "changes": {
        "installed": [
            "unzip"
        ]
    },
    "msg": "",
    "rc": 0,
    "results": [
        "Loaded plugins: fastestmirrornLoading mirror speeds from cached hostfilen * base: mirrors.bfsu.edu.cnn * extras: mirrors.bfsu.edu.cnn * updates: mirrors.bfsu.edu.cnnResolving Dependenciesn--> Running transaction checkn---> Package unzip.x86_64 0:6.0-22.el7_9 will be installedn--> Finished Dependency ResolutionnnDependencies Resolvednn================================================================================n Package        Arch            Version                  Repository        Sizen================================================================================nInstalling:n unzip          x86_64          6.0-22.el7_9             updates          171 knnTransaction Summaryn================================================================================nInstall  1 PackagennTotal download size: 171 knInstalled size: 365 knDownloading packages:nRunning transaction checknRunning transaction testnTransaction test succeedednRunning transactionn  Installing : unzip-6.0-22.el7_9.x86_64                                    1/1 n  Verifying  : unzip-6.0-22.el7_9.x86_64                                    1/1 nnInstalled:n  unzip.x86_64 0:6.0-22.el7_9                                                   nnComplete!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软件

 卸载

代码语言:javascript复制
[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": [
        "已加载插件:fastestmirrorn正在解决依赖关系n--> 正在检查事务n---> 软件包 unzip.x86_64.0.6.0-22.el7_9 将被 删除n--> 解决依赖关系完成nn依赖关系解决nn================================================================================n Package        架构            版本                    源                 大小n================================================================================n正在删除:n unzip          x86_64          6.0-22.el7_9            @updates          365 knn事务概要n================================================================================n移除  1 软件包nn安装大小:365 knDownloading packages:nRunning transaction checknRunning transaction testnTransaction test succeedednRunning transactionn  正在删除    : unzip-6.0-22.el7_9.x86_64                                   1/1 n  验证中      : unzip-6.0-22.el7_9.x86_64                                   1/1 nn删除:n  unzip.x86_64 0:6.0-22.el7_9                                                   nn完毕!n"
    ]
}
k8s_node1 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "changes": {
        "removed": [
            "unzip"
        ]
    },
    "msg": "",
    "rc": 0,
    "results": [
        "已加载插件:fastestmirrorn正在解决依赖关系n--> 正在检查事务n---> 软件包 unzip.x86_64.0.6.0-22.el7_9 将被 删除n--> 解决依赖关系完成nn依赖关系解决nn================================================================================n Package        架构            版本                    源                 大小n================================================================================n正在删除:n unzip          x86_64          6.0-22.el7_9            @updates          365 knn事务概要n================================================================================n移除  1 软件包nn安装大小:365 knDownloading packages:nRunning transaction checknRunning transaction testnTransaction test succeedednRunning transactionn  正在删除    : unzip-6.0-22.el7_9.x86_64                                   1/1 n  验证中      : unzip-6.0-22.el7_9.x86_64                                   1/1 nn删除:n  unzip.x86_64 0:6.0-22.el7_9                                                   nn完毕!n"
    ]
}

[root@k8s_node1 ~]# yum list installed | grep unzip

[root@k8s_node2 ~]# yum list installed | grep unzip

8.service模块

在这里插入图片描述 - service

代码语言:javascript复制
[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模块

在这里插入图片描述 - setup

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

代码语言:javascript复制
[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信息

代码语言:javascript复制
[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信息

代码语言:javascript复制
[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内存信息

代码语言:javascript复制
[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
}

0 人点赞