- 在这之前首先配置ssh免交互登录
ansible生成秘钥对,将公钥复制到远端
[root@localhost yum.repos.d]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:Ynr41WxUvOLeW6Vd7r1wIOBDwclRjnC+cZ5NQIZksoM root@localhost.localdomain
The key's randomart image is:
+---[RSA 2048]----+
| o+**= |
| . B==.. |
| E o * oo. |
| + *.+. |
| o S+o+.o o|
| + . =... .=.|
| o . . = .o.o|
| o . o . .o..|
| . . o. .+|
+----[SHA256]-----+
[root@localhost yum.repos.d]# ssh-copy-id root@192.168.19.181
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.19.181's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'root@192.168.19.181'"
and check to make sure that only the key(s) you wanted were added.
[root@localhost yum.repos.d]# ssh 192.168.19.181
Last login: Sun Oct 18 19:55:13 2020 from 192.168.19.1
[root@localhost ~]# ifconfig ens33
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.19.181 netmask 255.255.255.0 broadcast 192.168.19.255
inet6 fe80::d00f:1122:99ef:9bfe prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:f2:95:37 txqueuelen 1000 (Ethernet)
RX packets 467287 bytes 692798714 (660.7 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 86756 bytes 5399270 (5.1 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
- 编辑hosts文件,批量管理主机
[root@localhost yum.repos.d]# cd /etc/ansible/
[root@localhost ansible]# ls
ansible.cfg hosts roles
[root@localhost ansible]# mv hosts hosts_backup
[root@localhost ansible]# ls
ansible.cfg hosts_backup roles
[root@localhost ansible]# vim hosts
[root@localhost ansible]# cat hosts
[web]
192.168.19.181
[root@localhost ansible]# ansible -i /etc/ansible/hosts web -m ping
192.168.19.181 | SUCCESS => { //检查所有主机是否存活
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
[root@localhost ansible]# ansible web --list //显示web组所有的主机
hosts (1):
192.168.19.181
ansible -i /etc/ansible/hosts web -m ping
success表示执行成功,=> {}表示检查的结果,false表示没有对主机作出更改,pong表示执行的ping命令返回的结果
- Playbook示例安装Apache服务
1.创建yaml文件
[root@localhost playbooks_yaml]# vim httpd.yml
[root@localhost playbooks_yaml]# cat httpd.yml
---
- hosts: web
remote_user: root
tasks:
- name: Install httpd
yum: name=httpd state=present
- name: start service
service: name=httpd state=started enabled=yes
...
[root@localhost playbooks_yaml]# ansible-playbook --syntax-check httpd.yml //语法检查没有任何提示就OK
playbook: httpd.yml
[root@localhost playbooks_yaml]# pwd
/etc/ansible/playbooks_yaml
2.执行playbook,在执行前先预测试,预测试没有问题的话,再执行
[root@localhost playbooks_yaml]# ansible-playbook -C httpd.yml
PLAY [web] *****************************************************************************************************************************************************************
TASK [Gathering Facts] *****************************************************************************************************************************************************
ok: [192.168.19.181]
TASK [Install httpd] *******************************************************************************************************************************************************
changed: [192.168.19.181]
TASK [start service] *******************************************************************************************************************************************************
changed: [192.168.19.181]
PLAY RECAP *****************************************************************************************************************************************************************
192.168.19.181 : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0