随笔 - 58  文章 - 0  评论 - 1  阅读 - 4861

主机清单,ansible模块(上)

1、主机清单

1、基于密码的方式
	[root@localhost ~]# vim /etc/ansible/hosts 
	[web01]
	192.168.13.20 ansible_ssh_user=root ansible_ssh_port=22 ansible_ssh_pass='1'
	[web02]
	192.168.15.100 ansible_ssh_user=root ansible_ssh_port=22 ansible_ssh_pass='1'

	[root@localhost ~]# ssh 192.168.13.100
	[root@localhost ~]# ansible web02 -m ping
	ping所有
	[root@localhost ~]# ansible all -m ping

	ansible_ssh_user	:用户名
	ansible_ssh_port	:端口
	ansible_ssh_pass	:密码

2、基于变量密码的方式
	[root@localhost ~]# vim /etc/ansible/hosts 
    [web01]
    192.168.13.20 ansible_ssh_user=root ansible_ssh_port=22 
    [web01:vars]
    ansible_ssh_pass='1'

    
3、一个分组配置多主机
    [root@localhost ~]# cat /etc/ansible/hosts 
    [web01]
    192.168.13.20 ansible_ssh_user=root ansible_ssh_port=22 
    192.168.15.100 ansible_ssh_user=root ansible_ssh_port=22 
    [web01:vars]
    ansible_ssh_pass='1'
    
4、基于密钥的方式登录
	生成密钥:
	[root@localhost ~]# ssh-keygen
	[root@localhost ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.13.20
	[root@localhost ~]# ssh root@192.168.13.20
	[root@localhost ~]# cat /etc/ansible/hosts 
    [web01]
    192.168.13.20 ansible_ssh_user=root ansible_ssh_port=22 
    192.168.15.100 ansible_ssh_user=root ansible_ssh_port=22 
5、分组组合
	[root@localhost ~]# cat /etc/ansible/hosts
    [web01]
    192.168.13.20 ansible_ssh_user=root ansible_ssh_port=22
    [web02]
    192.168.15.100 ansible_ssh_user=root ansible_ssh_port=22 ansible_ssh_pass='1'
    [web:children]
    web01
    web02
    [root@localhost ~]# ansible web -m ping

2、ansible的模块

1、ansible-hoc 临时命令

2、ansible-hoc 返回结果
    绿色: 代表被管理端主机没有被修改
    黄色: 代表被管理端主机发现变更
    红色: 代表出现了故障,注意查看提示

2.1、command模块

command模块是ansible中默认模块。

参数:
	chdir  : 运行命令的目录
	creates:在执行命令之前,判断是否已经存在该路径

案例:
	使用 command 模块创建100个文件夹
	[root@localhost test]# ansible web02 -m command -a 'mkdir /root/test/{1..100}'

2.2、shell模块

shell 模块跟 command 模块功能非常相似,都是执行命令的模块;但是shell模块支持特殊符号,性能没有 command 模块高。

参数:
	chdir   : 运行命令的目录
	removes :判断一个文件是否存在,存在则运行。
		[root@localhost 1]# ansible all -m shell -a 'mkdir abc; removes=/root/test/1'
	creates:在执行命令之前,判断是否已经存在该路径

案例:
	将 /etc 目录中文件名称包含 root 的文件打包到 /tmp 目录
	[root@localhost ~]# ansible all -m shell -a "tar -czvPf /tmp/root.tar.gz `find /etc/ -name '*root*' | xargs`"

3.3、script模块

script模块主要是用来执行脚本文件的。

参数:
	chdir   : 运行命令的目录
	removes :判断一个文件是否存在,存在则运行。
	creates :在执行命令之前,判断是否已经存在该路径
	
	[root@localhost ~]# ansible all -m script -a './in.sh'

3.4、yum模块

安装软件包的模块。

参数:
	name : 软件包名称
	state :指定 yum 模块运行的状态

利用 yum 模块,安装部署mariadb
	[root@localhost ~]# ansible all -m yum -a 'name=mariadb* state=present'

3.5、yum_repository模块

yum仓库模块

参数:
	baseurl		: 仓库地址
	description	: 仓库描述
	enabled		: 是否启用
	gpgcheck	:是否验证gpg key
	gpgkey		:gpgkey 验证地址
	name		: 仓库名称 	

    [root@localhost yum.repos.d]# ansible all -m yum_repository -a 'name=nginx-stable description="nginx stable repo" baseurl=http://nginx.org/packages/centos/$releasever/$basearch/ gpgcheck=yes gpgkey=https://nginx.org/keys/nginx_signing.key enabled=yes'

案例:
    卸载所有版本的Nginx,使用官方仓库安装Nginx
    [root@localhost yum.repos.d]# ansible all -m yum -a 'name=nginx state=absent'
    [root@localhost yum.repos.d]# ansible all -m yum_repository -a 'name=nginx-stable description="nginx stable repo" baseurl=http://nginx.org/packages/centos/$releasever/$basearch/ gpgcheck=yes gpgkey=https://nginx.org/keys/nginx_signing.key enabled=yes'
    [root@localhost yum.repos.d]# ansible all -m yum_repository -a 'name=epel baseurl=http://mirrors.aliyun.com/epel/7/$basearch description=epel enabled=no'
    [root@localhost yum.repos.d]#\\192.168.13.28\linux14ansible all -m yum -a 'name=nginx state=latest'
posted on   婷婷妮子  阅读(60)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· AI与.NET技术实操系列(六):基于图像分类模型对图像进行分类
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示