五、aisible案例

5.1 说明
•安装好了Ansible 后就可以开始一些简单的任务了
•Ansible配置文件查找顺序
–首先检测ANSIBLE_CONFIG 变量定义的配置文件
–其次检查当前目彔下的./ansible.cfg 文件
–再次检查当前用户家目彔下~/ansible.cfg 文件
–最后检查/etc/ansible/ansible.cfg 文件
•/etc/ansible/ansible.cfg 默认配置文件路径
注:解释顺序的意思是如果前面有后面就不生效了,如果前面都没有最后查找/etc/ansible/ansible.cfg 文件。
5.2 讲解与操作
[root@ansible ~]# vim /etc/ansible/ansible.cfg #一般不需要修改配置
14 #inventory = /etc/ansible/hosts #定义主机的分组不修改以此路径为默认路径
[root@ansible ~]# vim /etc/ansible/hosts #定义主机分组
[web] 组名
web1
web2

[db]
db[1:2] #多台连续的主机可以这样写

[other]
cache

注:ansible管理主机有一个要求必须ssh连接得上去
5.3 配置完成后测试
1.查看web组有哪些主机
[root@ansible ~]# ansible web --list-hosts
hosts (2):
web1
web2
[root@ansible ~]# ssh root@web1
Last login: Mon Aug 2 17:31:23 2021 from 192.168.0.10
[root@ansible ~]# ansible db --list-hosts
hosts (2):
db1
db2
[root@ansible ~]# ansible web -m ping # 检测web组是否能够ssh连接
web1 | SUCCESS => {
"changed": false,
"ping": "pong"
}
web2 | SUCCESS => {
"changed": false,
"ping": "pong"
}

实际案例一:如果没有连接主机的公钥我们可以通过配置 /etc/ansible/hosts文件,配置连接主机的账号跟密码
[root@ansible ~]# vim /etc/ansible/hosts
[other]
192.168.0.33 ansible_ssh_user="root" ansible_ssh_pass="123456"

[root@ansible ~]# ansible other --list-hosts
hosts (1):
192.168.0.33
[root@ansible ~]# ansible other -m ping
192.168.0.33 | SUCCESS => {
"changed": false,
"ping": "pong"
}
实际案例二:在实际工作做我们新装的ansible中,所有的web组的主机都没有公钥应该怎么操作:
如下:
[root@ansible ~]# ansible web -m ping
web2 | UNREACHABLE! => {
"changed": false,
"msg": "Failed to connect to the host via ssh: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).\r\n",
"unreachable": true
}
web1 | UNREACHABLE! => {
"changed": false,
"msg": "Failed to connect to the host via ssh: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).\r\n",
"unreachable": true
}
这个时候我们发现所有的web主机都不能ping通了,如果按照案例一配置的话会过于麻烦,我们采用以下办法。设置组密码
[root@ansible ~]# vim /etc/ansible/hosts
[web]
web1
web2

[web:vars] #组名
ansible_ssh_user="root" #用户
ansible_ssh_pass="123456" #密码

[root@ansible ~]# ansible web -m ping
web1 | SUCCESS => {
"changed": false,
"ping": "pong"
}
web2 | SUCCESS => {
"changed": false,
"ping": "pong"
}
实际案例三:清理缓存文件
[root@ansible cp]# rm -rf /root/.ansible/cp/*
实际案例四:根据公司实际情况或针对某个项目需要很多台主机,包括web,db等,可以采用子组定义的方式操作如下
[root@ansible cp]# vim /etc/ansible/hosts
[app:children]
web #小分组
db
[root@ansible cp]# ansible app:children --list-hosts
[WARNING]: Could not match supplied host pattern, ignoring: children

hosts (4):
web1
web2
db1
db2
[root@ansible cp]# ansible app:children -m ping
[WARNING]: Could not match supplied host pattern, ignoring: children

web2 | SUCCESS => {
"changed": false,
"ping": "pong"
}
web1 | SUCCESS => {
"changed": false,
"ping": "pong"
}
db1 | SUCCESS => {
"changed": false,
"ping": "pong"
}
db2 | SUCCESS => {
"changed": false,
"ping": "pong"
}
[root@ansible cp]#
实际案例五:自定义主机分组(ansible一般不改)
[root@ansible ~]# mkdir /var/csansible
[root@ansible ~]# cd /var/csansible/
[root@ansible csansible]# touch ansible.cfg
[root@ansible csansible]# vim ansible.cfg
[defaults]
inventory = myhosts #【名字可以自定义】
[root@ansible csansible]# vim myhosts
[app1]
web1
db1

[app2]

[app2]
web2
db2
cache

[app:children]
app1
app2

[root@ansible csansible]# ansible app --list-hosts
hosts (5):
web1
db1
web2
db2
cache

 

posted @ 2021-08-10 10:41  落樰兂痕  阅读(72)  评论(0编辑  收藏  举报