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

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
云数据库 RDS MySQL,高可用系列 2核4GB
简介: Ansible自动化运维工具之常用模块使用实战(5)

环境介绍:


管理主机 k8s_master1 192.168.1.18


被托管主机 k8s_node1 192.168.1.19


被托管主机 K8S_node2 192.168.1.20


操作系统 Centos 7.5


常用模块:


ansible-docping模块


command模块


shell模块


script模块


copy模块


lineinfile|replace模块


yum模块


service模块


setup模块


模块使用实战:


1.ansible-docping模块

12.png

- ansible-doc


[root@k8s_master1 ~]# ansible-doc -l     //列出所有模块
fortios_router_community_list                                 Configure community lists in Fortinet's FortiOS and...
azure_rm_devtestlab_info                                      Get Azure DevTest Lab facts
ecs_taskdefinition                                            register a task definition in ecs
avi_alertscriptconfig                                         Module for setup of AlertScriptConfig Avi RESTful O...
tower_receive                                                 Receive assets from Ansible Tower
netapp_e_iscsi_target                                         NetApp E-Series manage iSCSI target configuration
azure_rm_acs                                                  Manage an Azure Container Service(ACS) instance
......
[root@k8s_master1 ~]# ansible-doc -l | grep mysql   //查找mysql相关的模块
azure_rm_mysqlfirewallrule_info                               
Get Azure MySQL Firewall Rule facts
azure_rm_mysqlconfiguration_info                              
Get Azure MySQL Configuration facts
mysql_info                                                    
Gather information about MySQL servers                                                                                          
...
[root@k8s_master1 ~]# ansible-doc mysql_db     //查看mysql_db模块的使用手册
> MYSQL_DB    (/usr/lib/python2.7/site-packages/ansible/modules/database/mysql/mysql_db.py)
        Add or remove MySQL databases from a remote host.
  * This module is maintained by The Ansible Community
OPTIONS (= is mandatory):
- ca_cert
        The path to a Certificate Authority (CA) certificate. This option, if used, must
        specify the same certificate as used by the server.
        (Aliases: ssl_ca)[Default: (null)]
        type: path
        version_added: 2.0
- client_cert
        The path to a client public key certificate.
        (Aliases: ssl_cert)[Default: (null)]
        type: path
        version_added: 2.0
...

- ping

[root@k8s_master1 ~]# cat /etc/ansible/hosts
...
[k8s_node]
k8s_node1
k8s_node2
[root@k8s_master1 ~]# ansible k8s_node -m ping   
k8s_node2 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}
k8s_node1 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}
[root@k8s_master1 ~]# ansible k8s_node -m ping -k
SSH password:    --》 -k 表示交互式输入被托管主机连接密码
k8s_node2 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}
k8s_node1 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}

2.command模块

13.png


- command


完整格式:


  ansible 主机集合 -m 模块名称 -a 模块参数

[root@k8s_master1 ~]# ansible k8s_node -m command -a 'uptime'  //ansible远程查看k8s_node主机组中的主机系统负载信息
k8s_node2 | CHANGED | rc=0 >>
 17:51:47 up 1 day,  7:37,  2 users,  load average: 0.00, 0.01, 0.05
k8s_node1 | CHANGED | rc=0 >>
 09:38:28 up 1 day, 16:03,  2 users,  load average: 0.00, 0.01, 0.05
[root@k8s_master1 ~]# ansible k8s_node -m command -a 'date'  //ansible远程查看k8s_node主机组中的主机系统时间
k8s_node2 | CHANGED | rc=0 >>
2021年 08月 11日 星期三 17:51:52 CST
k8s_node1 | CHANGED | rc=0 >>
2021年 08月 11日 星期三 09:38:33 CST
[root@k8s_master1 ~]# ansible k8s_node -m command -a 'cat /etc/passwd'   //ansible远程查看k8s_node主机组中的主机中/etc/passwd文件
k8s_node2 | CHANGED | rc=0 >>
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
......
k8s_node1 | CHANGED | rc=0 >>
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
......

14.png

示例:


[root@k8s_master1 ~]# ansible k8s_node -m command -a 'ps -aux | grep sshd'   //ansible在使用command模块远程控制主机组时无法识别上图字符
k8s_node2 | FAILED | rc=1 >>
error: user name does not exist
Usage:
 ps [options]
 Try 'ps --help <simple|list|output|threads|misc|all>'
  or 'ps --help <s|l|o|t|m|a>'
 for additional help text.
For more details see ps(1).non-zero return code
k8s_node1 | FAILED | rc=1 >>
error: user name does not exist
Usage:
 ps [options]
 Try 'ps --help <simple|list|output|threads|misc|all>'
  or 'ps --help <s|l|o|t|m|a>'
 for additional help text.
For more details see ps(1).non-zero return code

3.shell模块

15.png

- shell


  shell模块基本上可以执行所有类型(除了交互式)的命令。

[root@k8s_master1 ~]# ansible k8s_node -m shell -a 'ps -aux | grep sshd'   //ansible使用shell模块远程查看k8s_node主机组中的sshd服务信息
k8s_node1 | CHANGED | rc=0 >>
root       1164  0.0  0.3 162012  6888 ?        Ss   8月09   0:00 sshd: root@pts/0
root       1166  0.0  0.3 161664  6396 ?        Ss   8月09   0:00 sshd: root@notty
root       2174  0.0  0.2 112892  4344 ?        Ss   8月10   0:00 /usr/sbin/sshd -D
root       5417  0.0  0.2 154968  5868 ?        Ss   09:49   0:00 sshd: root@pts/1
root       5539  0.0  0.0 113172  1208 pts/1    S+   09:50   0:00 /bin/sh -c ps -aux | grep sshd
root       5541  0.0  0.0 112724   952 pts/1    S+   09:50   0:00 grep sshd
k8s_node2 | CHANGED | rc=0 >>
root        892  0.0  0.2 112892  4344 ?        Ss   8月10   0:00 /usr/sbin/sshd -D
root       1185  0.0  0.3 159848  6784 ?        Ss   8月10   0:00 sshd: root@pts/0
root       1187  0.0  0.3 159532  6332 ?        Ss   8月10   0:00 sshd: root@notty
root       5933  0.0  0.2 154964  5872 ?        Ss   18:03   0:00 sshd: root@pts/1
root       6056  0.0  0.0 113172  1212 pts/1    S+   18:03   0:00 /bin/sh -c ps -aux | grep sshd
root       6058  0.0  0.0 112724   956 pts/1    S+   18:03   0:00 grep sshd
[root@k8s_master1 ~]# ansible k8s_node -m shell -a 'uptime'
k8s_node2 | CHANGED | rc=0 >>
 18:04:07 up 1 day,  7:50,  2 users,  load average: 0.00, 0.01, 0.05
k8s_node1 | CHANGED | rc=0 >>
 09:50:48 up 1 day, 16:16,  2 users,  load average: 0.00, 0.01, 0.05
[root@k8s_master1 ~]# ansible k8s_node -m shell -a 'echo ${HOSTNAME}'   //ansible使用shell模块远程查看k8s_node主机组中的HOSTNAME内置变量值
k8s_node2 | CHANGED | rc=0 >>
k8s_node2
k8s_node1 | CHANGED | rc=0 >>
k8s_node1

16.png

以上testfile文件案例验证:


[root@k8s_master1 ~]# ansible k8s_node -m shell -a "cd /tmp"  //ansible使用shell模块远程进入k8s_node主机组中的tmp目录
k8s_node2 | CHANGED | rc=0 >>
k8s_node1 | CHANGED | rc=0 >>
[root@k8s_master1 ~]# ansible k8s_node -m shell -a "touch testfile"  //ansible使用shell模块远程为k8s_node主机组创建testfile文件
[WARNING]: Consider using the file module with state=touch rather than running 'touch'.  If you need to use command
because file is insufficient you can add 'warn: false' to this command task or set 'command_warnings=False' in
ansible.cfg to get rid of this message.
k8s_node2 | CHANGED | rc=0 >>
k8s_node1 | CHANGED | rc=0 >>
--两台被托管主机查看创建的testfile文件---
[root@k8s_node1 ~]# pwd
/root
[root@k8s_node1 ~]# ll testfile
-rw-r--r-- 1 root root 0 8月  11 09:58 testfile
[root@k8s_node2 ~]# pwd
/root
[root@k8s_node2 ~]# ll testfile
-rw-r--r-- 1 root root 0 8月  11 18:11 testfile

 可以看到在创建testfile文件时,并没有创建在tmp目录下,而是被默认创建到了root目录下,说明ansible 是使用 ssh 多次连接执行,连接退出以后之前的状态就全部失效了。


- - 解决办法 - -:


  使用 chdir 代替 cd 命令

[root@k8s_master1 ~]# ansible k8s_node -m shell -a "chdir=/tmp touch testfile"
[WARNING]: Consider using the file module with state=touch rather than running 'touch'.  If you need to use command
because file is insufficient you can add 'warn: false' to this command task or set 'command_warnings=False' in
ansible.cfg to get rid of this message.
k8s_node2 | CHANGED | rc=0 >>
k8s_node1 | CHANGED | rc=0 >>
[root@k8s_node1 ~]# ll /tmp/testfile    //成功将testfile文件创建到tmp文件夹中
-rw-r--r-- 1 root root 0 8月  11 10:09 /tmp/testfile
[root@k8s_node2 ~]# ll /tmp/testfile
-rw-r--r-- 1 root root 0 8月  11 18:22 /tmp/testfile

例子:为k8s_node主机组中的主机创建一个test用户并且设置密码为123。

[root@k8s_master1 ~]# ansible k8s_node -m shell -a 'useradd test'
k8s_node2 | CHANGED | rc=0 >>
k8s_node1 | CHANGED | rc=0 >>
[root@k8s_master1 ~]# ansible k8s_node -m shell -a 'echo 123 | passwd --stdin test'
k8s_node2 | CHANGED | rc=0 >>
更改用户 test 的密码 。
passwd:所有的身份验证令牌已经成功更新。
k8s_node1 | CHANGED | rc=0 >>
更改用户 test 的密码 。
passwd:所有的身份验证令牌已经成功更新。

4.script模块

17.png

- script


案例:在两台被托管主机上判断有无用户tom,如果没有则创建用户tom并配置密码为123。

[root@k8s_master1 ~]# cat >> user.sh << EOF
> #!/bin/bash
> id tom
> if [  $? != 0 ];then
> useradd tom
> echo 123 | passwd --stdin tom
> fi
> EOF
[root@k8s_master1 ~]# cat user.sh
#!/bin/bash
id tom
if [ $? != 0 ];then
useradd tom
echo 123 | passwd --stdin tom
fi
[root@k8s_master1 ~]# ansible k8s_node -m script -a '/root/user.sh'
k8s_node2 | CHANGED => {
    "changed": true,
    "rc": 0,
    "stderr": "Shared connection to k8s_node2 closed.\r\n",
    "stderr_lines": [
        "Shared connection to k8s_node2 closed."
    ],
    "stdout": "id: tom: no such user\r\n更改用户 tom 的密码 。\r\npasswd:所有的身份验证令牌已经成功更新。\r\n",
    "stdout_lines": [
        "id: tom: no such user",
        "更改用户 tom 的密码 。",
        "passwd:所有的身份验证令牌已经成功更新。"
    ]
}
k8s_node1 | CHANGED => {
    "changed": true,
    "rc": 0,
    "stderr": "Shared connection to k8s_node1 closed.\r\n",
    "stderr_lines": [
        "Shared connection to k8s_node1 closed."
    ],
    "stdout": "id: tom: no such user\r\n更改用户 tom 的密码 。\r\npasswd:所有的身份验证令牌已经成功更新。\r\n",
    "stdout_lines": [
        "id: tom: no such user",
        "更改用户 tom 的密码 。",
        "passwd:所有的身份验证令牌已经成功更新。"
    ]
}
---两台被托管主机查看ansible执行情况---
[root@k8s_node1 ~]# id tom
uid=1001(tom) gid=1001(tom) 组=1001(tom)
[root@k8s_node1 ~]# cat /etc/passwd | grep tom
tom:x:1001:1001::/home/tom:/bin/bash
[root@k8s_node2 ~]# id tom
uid=1001(tom) gid=1001(tom) 组=1001(tom)
[root@k8s_node2 ~]# cat /etc/passwd | grep tom
tom:x:1001:1001::/home/tom:/bin/bash

5.copy模块

18.png

19.png

- copy


[root@k8s_master1 ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.1.18 k8s_master1
192.168.1.19 k8s_node1
192.168.1.20 k8s_node2
[root@k8s_master1 ~]# ansible k8s_node -m copy -a 'src=/etc/hosts dest=/etc/hosts'
k8s_node2 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "checksum": "561ed24474a2938a845088d89f280b8e3e864103",
    "dest": "/etc/hosts",
    "gid": 0,
    "group": "root",
    "mode": "0644",
    "owner": "root",
    "path": "/etc/hosts",
    "size": 229,
    "state": "file",
    "uid": 0
}
k8s_node1 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "checksum": "561ed24474a2938a845088d89f280b8e3e864103",
    "dest": "/etc/hosts",
    "gid": 0,
    "group": "root",
    "mode": "0644",
    "owner": "root",
    "path": "/etc/hosts",
    "size": 229,
    "state": "file",
    "uid": 0
}
[root@k8s_node1 ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.1.18 k8s_master1
192.168.1.19 k8s_node1
192.168.1.20 k8s_node2
[root@k8s_node2 ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.1.18 k8s_master1
192.168.1.19 k8s_node1
192.168.1.20 k8s_node2
相关实践学习
通过Ingress进行灰度发布
本场景您将运行一个简单的应用,部署一个新的应用用于新的发布,并通过Ingress能力实现灰度发布。
容器应用与集群管理
欢迎来到《容器应用与集群管理》课程,本课程是“云原生容器Clouder认证“系列中的第二阶段。课程将向您介绍与容器集群相关的概念和技术,这些概念和技术可以帮助您了解阿里云容器服务ACK/ACK Serverless的使用。同时,本课程也会向您介绍可以采取的工具、方法和可操作步骤,以帮助您了解如何基于容器服务ACK Serverless构建和管理企业级应用。 学习完本课程后,您将能够: 掌握容器集群、容器编排的基本概念 掌握Kubernetes的基础概念及核心思想 掌握阿里云容器服务ACK/ACK Serverless概念及使用方法 基于容器服务ACK Serverless搭建和管理企业级网站应用
相关文章
|
2月前
|
弹性计算 运维 监控
|
1天前
|
运维 安全 开发工具
GitHub 热门开源运维工具 Websoft9:如何实现服务器管理效率翻倍?
Websoft9 提供 200+ 开源应用一键部署,支持容器化隔离、GitOps 自动化和企业级安全防护,助力服务器管理效率提升 80%。
24 1
|
11天前
|
人工智能 运维 Kubernetes
运维联盟 SOMA 评测系统正式发布,让你的运维工具跑分更丝滑
通过对特定的业务系统进行故障注入,将不同的运维工具放在一起进行评估和评测。
|
2月前
|
运维 自然语言处理 Ubuntu
解锁高效运维新姿势!操作系统智能助手OS Copilot新功能实战测评
阿里云OS Copilot经过多轮迭代,现已支持多端操作系统(包括Ubuntu、CentOS、Anolis OS等)及aarch64架构,极大扩展了其适用范围。新特性包括阿里云CLI调用、系统运维及调优工具的直接调用、Agent模式实装以及复杂任务处理能力。这些更新显著提升了用户体验和效率,特别是在处理紧急情况时,OS Copilot能快速查找并执行命令,节省大量时间和精力。此外,通过自然语言交互,用户可以轻松完成如系统健康检查、文件操作及日志分析等任务。总之,OS Copilot已从内测时的辅助工具进化为合格的贴身管家,极大地简化了日常运维工作。
|
3月前
|
弹性计算 运维 监控
云资源运维难?阿里云免费工具来帮忙
阿里云推出免费运维工具——云服务诊断,帮助用户提升对云资源的运维效率、降低门槛、减轻负担。其核心功能包括「健康状态」和「诊断」。通过「健康状态」可实时查看云资源是否正常;「诊断」功能则能快速排查网络、配置、安全等问题,并提供修复建议,助您迅速恢复业务。体验评测活动火热进行中,参与即有机会赢取索尼头戴耳机、小米背包等好礼。活动链接:https://developer.aliyun.com/topic/cloud-health。
702 24
|
4月前
|
运维 应用服务中间件 网络安全
自动化运维的新篇章:使用Ansible进行服务器配置管理
【10月更文挑战第34天】在现代IT基础设施的快速迭代中,自动化运维成为提升效率、确保一致性的关键手段。本文将通过介绍Ansible工具的使用,展示如何实现高效的服务器配置管理。从基础安装到高级应用,我们将一步步揭开自动化运维的神秘面纱,让你轻松掌握这一技术,为你的运维工作带来革命性的变化。
|
4月前
|
运维 应用服务中间件 Linux
自动化运维的利器:Ansible在配置管理中的应用
【10月更文挑战第39天】本文旨在通过深入浅出的方式,向读者展示如何利用Ansible这一强大的自动化工具来优化日常的运维工作。我们将从基础概念讲起,逐步深入到实战操作,不仅涵盖Ansible的核心功能,还会分享一些高级技巧和最佳实践。无论你是初学者还是有经验的运维人员,这篇文章都会为你提供有价值的信息,帮助你提升工作效率。
|
3月前
|
运维 Ubuntu 应用服务中间件
自动化运维之路:使用Ansible进行服务器管理
在现代IT基础设施中,自动化运维已成为提高效率和可靠性的关键。本文将引导您通过使用Ansible这一强大的自动化工具来简化日常的服务器管理任务。我们将一起探索如何配置Ansible、编写Playbook以及执行自动化任务,旨在为读者提供一条清晰的路径,从而步入自动化运维的世界。
|
3月前
|
运维 网络安全 Python
自动化运维:使用Ansible实现批量服务器配置
在快速迭代的IT环境中,高效、可靠的服务器管理变得至关重要。本文将介绍如何使用Ansible这一强大的自动化工具,来简化和加速批量服务器配置过程。我们将从基础开始,逐步深入到更复杂的应用场景,确保即使是新手也能跟上节奏。文章将不包含代码示例,而是通过清晰的步骤和逻辑结构,引导读者理解自动化运维的核心概念及其在实际操作中的应用。
|
3月前
|
运维 Ubuntu 网络协议
自动化运维:使用Ansible进行服务器配置管理
在现代IT架构中,自动化运维已成为提升效率、减少人为错误的关键。本文将介绍如何使用Ansible这一强大的自动化工具来简化和标准化服务器的配置管理过程。通过具体的代码示例和操作步骤,我们将展示如何快速部署应用、管理配置以及自动化日常任务,从而确保环境的一致性和可靠性。

热门文章

最新文章