ansible安装配置与17个常用模块

一、centos8.3安装ansible

安装epel源

sudo dnf install https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm -y

安装ansible:

yum install ansible

二、安装后的基本配置

1.主要配置文件

[root@localhost ansible]# pwd
/etc/ansible
[root@localhost ansible]# ll
总用量 24
-rw-r--r--. 1 root root 19983 7月  26 19:49 ansible.cfg
-rw-r--r--. 1 root root  1100 7月  26 19:41 hosts
drwxr-xr-x. 2 root root     6 6月  23 07:33 roles

2.修改ansible.cfg,打开日志
在这里插入图片描述
3.准备三台虚拟机,配置ansible的hosts文件,可分组,可连续ip
在这里插入图片描述
4.测试ansible的ping,看ansible是否可连接成功配置的机器

ansible all -m ping

这样会让确认连接,但只能确认一个主机,摒弃

ansible all -m ping -k

这样会要求输密码,前提是所有虚拟机密码相同,摒弃

密钥方式免密登录:

ssh-keygen
ssh-copy-id 192.168.44.11
ssh-copy-id 192.168.44.12
ssh-copy-id 192.168.44.13

机器关系的使用:
ansible 192.168.226.101 -m ping # 单独机器的ping
ansible 192.168.226.101,192.168.226.102 -m ping # 多个机器的ping
ansible all -m ping # 全部机器
ansible web -m ping # 单个的组
ansible web,db -m ping # 多个组的并集
ansible ‘web:&db’ -m ping # 多个组的交集 必须是单引号 双引号不行
ansible ‘web:!db’ -m ping # 多个组的差集,在前面但是不在后面

查看主机列表:

[root@localhost ansible]# ansible all --list
  hosts (3):
    192.168.44.12
    192.168.44.13
    192.168.44.11
[root@localhost ansible]# ansible appsrvs --list
  hosts (2):
    192.168.44.12
    192.168.44.13

查看模块帮助:
ansible-doc 模块名

ansible-doc setup

三、常用模块的使用

1.command–默认的默认模块

ansible 机器组名 [-m command] -a shell命令

ansible all -m command -a 'creates=/etc/centos-release chdir=/etc cat centos-release'

常用参数:
chdir参数:执行命令前切换到此目录
creates参数:当指定的文件存在时,就不执行对应命令
removes参数:当指定的文件不存在时,就不执行对应命令

command弊端,很多符号不支持,$,*,| 等都不支持

[root@localhost ansibleLearning]# ansible all -m command -a 'chdir=/etc ls -l|grep config'
192.168.44.12 | FAILED | rc=2 >>
ls:无效选项 -- |
Try 'ls --help' for more information.non-zero return code
192.168.44.13 | FAILED | rc=2 >>
ls:无效选项 -- |
Try 'ls --help' for more information.non-zero return code
192.168.44.11 | FAILED | rc=2 >>
ls:无效选项 -- |
Try 'ls --help' for more informati
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

运维小菜

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值