目录
一、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