6.lineinfile
|replace
模块
- 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 ...
- 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
模块
- 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
模块
- 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
模块
- 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 }