使用ansible维护linux集群 10

2020-05-21  本文已影响0人  张力的程序园

Ansible常用来对集群进行维护和管理,它只需要在管理端安装,被管理端主要提供sshd服务即可。本节讲述ansible的安装和使用。

1、操作环境

2、前提约束

3、操作步骤

3.1 安装配置ansible

# 新增epel-release第三方套件来源。
yum install -y epel-release
# 安装 Ansible
yum install -y ansible
# 配置免密登陆,执行以下语句
ssh-keygen  #一路回车
ssh-copy-id 192.168.100.101  # 回车之后,需要输入192.168.100.101的密码
ssh-copy-id 192.168.100.102  # 回车之后,需要输入192.168.100.102的密码
# 在管理端服务器安装python
yum install -y openssh-server python
# 连接到被管理端服务器
ssh 192.168.100.102
# 在被管理端服务器安装python
yum install -y openssh-server python
# 退出102,继续回到101
exit
[defaults]
inventory = /etc/ansible/hosts
forks = 5
become = root
remote_port  = 22
host_key_checking = False
timeout = 10
log_path = /var/log/ansible.log
private_key_file = /root/.ssh/id_rsa
[servers]
192.168.100.101
192.168.100.102

3.2 通过ad-hoc管理服务器

ansible servers -m ping
# 在管理端创建可执行文件
echo "hello world">test.sh
# 推送test.sh到被管理端
ansible servers -m copy -a "src=/root/test.sh dest=/root/ owner=root group=root mode=0755"
# 执行被管理端的test.sh
ansible servers -m shell -a "/root/test.sh"
# 安装httpd
ansible servers -m yum -a "name=httpd state=latest disable_gpg_check=yes enablerepo=epel"
# 启动httpd
ansible servers -m service -a "name=httpd state=restarted" 
# 查看服务状态
ansible servers -a " systemctl status httpd"  

3.3 自动部署Nginx

#nginx.yml
---
- hosts: servers
  tasks:
  - name: Add repo
    yum_repository:
      name: nginx
      description: nginx repo
      baseurl: http://nginx.org/packages/centos/7/$basearch/
      gpgcheck: no
      enabled: 1
  - name: Install nginx
    yum:
      name: nginx
      state: latest
  - name: Start nginx
    service:
      name: nginx
      state: started
ansible-playbook nginx.yml

以上就是使用ansible维护linux集群的过程。

上一篇 下一篇

猜你喜欢

热点阅读