Ansible自动化运维工具及其常用模块(2)

Ansible自动化运维工具及其常用模块(2)

由于组内成员过多,修改hosts配置文件

vim /etc/ansible/hosts

[web]

node1

[db]

node2

[wang]

node3

ping测试

[root@ansible ~]# ansible web -m ping

[root@ansible ~]# ansible db -m ping

[root@ansible ~]# ansible wang -m ping

4. user模块

用户管理的模块

4.1 列出指定模块的描述信息和操作动作

ansible-doc -s user

4.2 常用参数

常用参数

说明

name

用户名,必选参数

state=present/absent

创建账号或者删除账号,present表示创建,absent表示删除

system=yes/no

是否为系统账号

uid

用户uid

group

用户基本组

shell

默认使用的shell

move_home=yse/no

如果设置的家目录已经存在,是否将已经存在的家目录进行移动

password

用户的密码,建议使用加密后的字符串

comment

用户的注释信息

remove=yes/no

当state=absent时,是否删除用户的家目录

4.3 用户控制

创建用户test01
ansible dbservers -m user -a 'name="test01"'

创建用户时有的时候会出现如下报错

原因是db组成员的/var/spool/mail/目录下已经有了test01文件,删除即可

查看passwd

ansible db -a 'tail -n 1 /etc/passwd'或ansible db -a 'tail -1 /etc/passwd'

删除用户test01,最后带着家目录一起删除

ansible db -m user -a 'name="test01" remove=yes state=absent'

5. group模块

用户组管理的模块

5.1 列出指定模块的描述信息和操作动作

ansible-doc -s group

5.2 用户组管理

创建mysql组

ansible db -m group -a 'name=mysql gid=2222 system=yes'

验证是否创建

ansible db -a 'tail -1 /etc/group'

将test01用户添加到mysql组中

ansible db -m user -a 'name=test01 uid=2222 system=yes group=mysql'

ansible db -a 'tail -1 /etc/passwd'

ansible db -a 'id test01'

6. copy模块

用于复制指定主机文件到远程主机

ansible-doc -s copy

6.1 常用参数

常用参数

说明

dest

指出复制文件的目标及位置,使用绝对路径,如果是源目录,指目标也要是目录,如果目标文件已经存在会覆盖原有的内容

src

指出源文件的路径,可以使用相对路径或绝对路径,支持直接指定目录,如果源是目录则目标也要是目录

mode

指出复制时,目标文件的权限

owner

指出复制时,目标文件的属主

group

指出复制时,目标文件的属组

content

指出复制到目标主机上的内容,不能与src一起使用

6.2 复制管理

ansible db -m copy -a 'src=/etc/fstab dest=/opt/fstab.bak owner=root mode=640'

ansible db -a 'ls -l /opt'

ansible db -a 'cat /opt/fstab.bak'

将helloworld写入/opt/hello.txt文件中

ansible db -m copy -a 'content="helloworld" dest=/opt/hello.txt'

ansible db -a 'cat /opt/hello.txt'

7. file模块

设置文件属性

7.1 列出指定模块的描述信息和操作动作

ansible-doc -s file

7.2 文件属性管理

修改文件的属主属组权限等

ansible db -m file -a 'owner=test01 group=mysql mode=644 path=/opt/fstab.bak'

设置/opt/fstab.link为/opt/fstab.bak的链接文件

ansible db -m file -a 'path=/opt/fstab.link src=/opt/fstab.bak state=link'

创建一个文件

ansible db -m file -a "path=/opt/abc.txt state=touch"

删除一个文件

ansible db -m file -a "path=/opt/abc.txt state=absent"

8. hostname模块

用于管理远程主机上的主机名

ansible db -m hostname -a "name=mysql01"

9. ping模块 (一般用于刚开始测试),如果能ping通就能继续操作

检测远程主机的连通性

ansible all -m ping

10. yum模块

ansible-doc -s yum

安装服务

ansible web -m yum -a 'name=httpd'

……

卸载服务

ansible web -m yum -a 'name=httpd state=absent'

11. service/systemd 模块

用于管理远程主机上的管理服务的运行状态

ansible-doc -s service

11.1 常用参数

常用参数

说明

name

被管理的服务名称

state=started\stopped\restarted

动作包含启动关闭或者重启

enabled=yes\no

表示是否设置该服务开机自启

runlevel

如果设定了enabled开机自启去,则要定义在哪些运行目标下自启动

11.2 服务管理

查看web服务器httpd运行状态

ansible web -a 'systemctl status httpd'

启动httpd服务

ansible web -m service -a 'enabled=true name=httpd state=started'

12. script 模块

实现远程批量运行本地的 shell 脚本

ansible-doc -s script

12.1 准备脚本

[root@ansible ~]# vim test.sh

#!/bin/bash

echo "hello ansible from script" > /opt/script.txt

12.2 script执行脚本

ansible web -m script -a 'test.sh'

[root@ansible ~]# vim test.sh

[root@ansible ~]# chmod +x test.sh

[root@ansible ~]# ansible web -m script -a 'test.sh'

node1 | CHANGED => {

"changed": true,

"rc": 0,

"stderr": "Shared connection to node1 closed.\r\n",

"stderr_lines": [

"Shared connection to node1 closed."

],

"stdout": "",

"stdout_lines": []

}

[root@ansible ~]# ansible web -a 'cat /opt/script.txt'

node1 | CHANGED | rc=0 >>

hello ansible from script

13. setup 模块

获取指定主机的facts信息

facts组件是用来收集被管理节点信息的,使用 setup 模块可以获取这些信息

ansible-doc -s setup

13.1 获取指定主机的facts信息

ansible web -m setup

13.2 过滤获取指定主机的指定facts信息

使用filter可以筛选指定的facts信息

ansible web -m setup -a 'filter=*ipv4'

四、inventory 主机清单

Inventory支持对主机进行分组,每个组内可以定义多个主机,每个主机都可以定义在任何一个或多个主机组内。

1. 列表表示

如果是名称类似的主机,可以使用列表的方式标识各个主机。

[root@ansible ~]# vim /etc/ansible/hosts

 

[webservers]

node1:2222

#冒号后定义远程连接端口,默认是 ssh 的 22 端口

node2[2:5]

 

[dbservers]

db-[a:f].example.org

#支持匹配 a~f

[a:f][a:z][b:o][c:e]

2. inventory 中的变量

Inventory变量名

含义

ansible_host

ansible连接节点时的IP地址

ansible_port

连接对方的端口号,ssh连接时默认为22

ansible_user

连接对方主机时使用的主机名。不指定时,将使用执行ansible或ansible-playbook命令的用户

ansible_password

连接时的用户的ssh密码,仅在未使用密钥对验证的情况下有效

ansible_ssh_private_key_file

指定密钥认证ssh连接时的私钥文件

ansible_ssh_common_args

提供给ssh、sftp、scp命令的额外参数

ansible_become

允许进行权限提升

ansible_become_method

指定提升权限的方式,例如可使用sudo/su/runas等方式

ansible_become_user

提升为哪个用户的权限,默认提升为root

ansible_become_password

提升为指定用户权限时的密码

3. 变量

3.1 主机变量

vim /etc/ansible/hosts

[web]

node1 ansible_port=22 ansible_user=root ansible_password=abc123

3.2 组变量

[web:vars]

#表示为web组内所有主机定义变量

ansible_user=root

ansible_password=abc123

[all:vars]

#表示为所有组内的所有主机定义变量

ansible_port=22

3.3 组嵌套

[nginx]

192.168.10.2

192.168.10.4

[apache]

192.168.10.3[0:3]

[webs:children]

#表示为 webs 主机组中包含了 nginx 组和 apache 组内的所有主机

nginx

apache

posted @   wang-a  阅读(179)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示