> 文章列表 > 基于ansible初始化linux服务器基础环境。

基于ansible初始化linux服务器基础环境。

基于ansible初始化linux服务器基础环境。

大家好,今天我要和大家分享一个关于搭建centos环境的新方法。

以前我们经常会看到一些文章介绍如何搭建centos环境,但很多时候都会出现一些问题。不过现在有了一种新的方法,就是使用ansible脚本来实现。

虽然这种方法仅适用于centos7,但只要稍作修改就可以应用到其他的环境中。使用ansible脚本可以让搭建环境更加简单、快速、稳定。

 GIT 项目访问地址

https://gitee.com/haofeng82/basic-host-machine--init.git

目录

介绍

软件架构

安装教程

使用说明

初始化操作用户

     2. 主机基本功能初始化。

    3.为机器设置host映射。

        可能存在的问题

脚本摘要

1 初始化操作账户

2 初始化centos系统

3 安装docker

4 设置hosts映射

 GIT 项目访问地址


介绍

这是一套ansbile脚本,实现了始化基于centos7系统的服务器主机基础环境的功能。 目前仅用于测试和教学环境使用,并仅保证centos77版本有效。

软件架构

软件基于ansible脚本对服务器进行初初始化工作,主要工作如下: 1 增加ops做操作账户,并为其增加ssh证书登录,此用户可以切换至root。 2 并且禁用了root登录。所有需要root的行为,都会通过ops 进行become 3 禁用了防火墙等功能。统一使用外部防火墙。 4 安装了必要的系统包,并进行了系统优化。 5 安装了docker,因为后续的一些其他项目的安装,会用到docker。 6 提供了统一设置主机host的功能

安装教程

1需要找一台服务器,安装ansible环境,然后把当前项目复制到相应的目录下。

2依据后面的使用步骤执行脚本即可。 PS:主机目录文件需要自行进行修改。 证书使用了预先生成好的证书,也可以自行生成配置。

使用说明

  1. 初始化操作用户。

        当拿到一台新机器时,首先需要对其进行用户限制。 前提:将主机设置为可以使用root登录。密码统一初始化为XXX.

        主要操作如下: 1 禁用root的远程登录。 2 禁用密码登录。 3 创建operator操作用户,及operator组。并为其设置登录密码、上传登录共公钥。

        上面的所有操作,均在init-user角色中,并通过下面的脚本调用

        上传整个目录到ansible主机的/ansible/basic-host-machine目录下,并对host文件中的to-init-user-host分组中的主机ip进行设置。指定为要操作的机器。 cd /ansible/basic-host-machine cp -rf /ansible/basic-host-machine/.ssh /root ansible-playbook -vvv --check -i hosts/nat/host-basic init-basic-host-user.yml ansible-playbook -vvv -i hosts/nat/host-basic init-basic-host-user.yml

     2. 主机基本功能初始化。

        此步骤必须依赖第一步 在执行完初始化用户之后。会以operator用户进行登录。 此时的root用户已经被禁用掉了。

        上传整个目录到ansible主机的/ansible/basic-host-machine目录下,并对host文件中的to-init-env-host分组中的主机ip进行设置。指定为要操作的机器。

        主要工作包括: 安装必要的工具包 开启或者关闭防火墙以及selinux 安装docker环境

        cd /ansible/basic-host-machine

        ansible-playbook -vvv --check -i hosts/nat/host-basic init-basic-host-env.yml ansible-playbook -vvv -i hosts/nat/host-basic init-basic-host-env.yml

    3.为机器设置host映射。

        此步骤必须依赖第一步 统一对主机的hosts进行配置

        上传整个目录到ansible主机的/ansible/basic-host-machine目录下,并对host文件中的to-init-user-host分组中的主机ip进行设置。指定为要操作的机器。 cd /ansible/basic-host-machine

        ansible-playbook -vvv --check -i hosts/nat/host-basic set-host-mapping

        ansible-playbook -vvv -i hosts/nat/host-basic set-host-mapping.yml

        可能存在的问题

变量的配置还是有一些不合适的地方。需要再进行修改。目前仅是提交了初始版本。

代码放到了git上,需要的小伙伴直接拿来用就好。如果有不严谨的地方,也希望能够指正出来。

脚本摘要

1 初始化操作账户

---

#关闭防火墙

- name: close fire wall

  shell: "{{ item}}"

  with_items:

    - "systemctl stop firewalld.service"

    - "systemctl disable firewalld.service"

#关闭selinux

- name: Disabled SELinux

  selinux: state=disabled

#下次启动也不会再起selinux

- name: set selinux disabled

  replace:

    path: /etc/selinux/config

    regexp: '^SELINUX=enforcing'

    replace: 'SELINUX=disabled'

##创建用户分组

- name: Create group

  group:  name={{group}} state=present

  when: add_user

#当add_user变量为true,创建用户。此用户用于运行项目。

- name: Add sudo user

  user:

    name: "{{ user }}"

    password: "{{ operation_user_password }}"

    state: present

  when: add_user

 

#将证书拷贝至远程机器,用户为root,这一步是为了能够通过证书ssh访问远程主机

- name: install ssh key

  authorized_key: user={{user}}

                  key="{{ lookup('file','/root/.ssh/id_rsa.pub')}}"

                  state=present

 

#将用户临时切换到为sudo root用户时,设置为不需要密码。

- name: Add configured user accounts to passwordless sudoers.

  lineinfile: >

    dest=/etc/sudoers

    regexp='^{{ user }}'

    line='{{ user }} ALL=(ALL) NOPASSWD: ALL'

    state=present

    validate='visudo -cf %s'

2 初始化centos系统

---

#配置主机名

- name: Configure hostname

  hostname:

    name: "{{ host_name }}"

- name: install basic tools1

  yum:

    name: "{{ item }}"

    state: present

  with_items:

      - "wget"

     

   

#更换yum阿里数据源    

- name: change yum source to ali

  shell: "{{ item}}"

  with_items:

    - "mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup"

    - "wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo"

    - "yum clean all"

    - "yum makecache"

#安装基础工具

#- name: update yum

#  shell: yum -y update

- name: install basic tools2

  yum:

    name: "{{ item }}"

    state: present

  with_items:

      - "wget"

      - "curl"

      - "unzip"

      - "vim"

      - "net-tools"

      - "nfs-utils"

      - "pcre-devel"

      - "openssl-devel"

      - "gcc"

      - "perl"

      - "perl-devel"

#复制cronolog rpm安装文件到服务器

- name: copy cronolog rpm install file

  copy:

    src:  "{{ playbook_dir }}/data/archive/cronolog-1.6.2-14.el7.x86_64.rpm"

    dest: "/tools/cronolog/"

# 安装 cronolog

- name: install cronolog

  shell: "{{ item }}"

  with_items:

    - "rpm -ivh /tools/cronolog//cronolog-1.6.2-14.el7.x86_64.rpm"

  ignore_errors: True    

 

#临时修改linux文件句柄数(用户级别)

- name: modify linux user ulimit temp

  shell: "{{ item}}"

  with_items:

    - "ulimit -SHn 65535"

# 永久修改 linux user soft limit

- name: modify linux root user soft  ulimit per

  lineinfile: >

    dest=/etc/security/limits.conf

    regexp='^root soft nofile'

    line='root soft nofile 65535'

    state=present

# 永久修改 linux user hard limit

- name: modify linux user ulimit per

  lineinfile: >

    dest=/etc/security/limits.conf

    regexp='^root hard nofile'

    line='root hard nofile 65535'

    state=present

# 永久修改 linux user soft limit

- name: modify linux ops user soft  ulimit per

  lineinfile: >

    dest=/etc/security/limits.conf

    regexp='^ops soft nofile'

    line='ops soft nofile 65535'

    state=present

# 永久修改 linux user hard limit

- name: modify linux ops user ulimit per

  lineinfile: >

    dest=/etc/security/limits.conf

    regexp='^ops hard nofile'

    line='ops hard nofile 65535'

    state=present

# 上传 linux system 配置文件,优化系统性能参数

- name: upload linux system   ulimit temp

  shell: "echo  655350 > /proc/sys/fs/file-max"

- name: Copy sysctl.conf to server

  template:

    src: sysctl.conf.j2

    dest: /etc/sysctl.conf

    mode: 0740

# 重启sysctl

- name: restart sysctl

  shell: "sysctl -p"

 

   

#将证书复制到用户的.ssh目录下,并更改权限。

- name: copy auth public  key to host file

  copy:

    src:  "/root/.ssh/id_rsa.pub"

    dest: "/home/{{ user }}/.ssh/"

    owner: "{{ user }}"

    group: "{{ group }}"

#私钥的权限一定不要别的用户和组能够读取到

- name: copy auth private key to host file

  copy:

    src:  "/root/.ssh/id_rsa"

    dest: "/home/{{ user }}/.ssh/"

    owner: "{{ user }}"

    group: "{{ group }}"

    mode: 0600

#更新sshd文件配置,更改sshd端口,是否开启sshd_use_dns,是否开启sshd_gssapi_authentication是否开启,

#设置sshd协议 2,禁止root登录(先不设置)是否开启密码登录(先不禁止密码登录)

- name: Update SSH configuration to be standard setting.

  lineinfile: >

    dest={{ sshd_config_path }}

    regexp="{{ item.regexp }}"

    line="{{ item.line }}"

    state=present

  with_items:

    - { regexp: "^#?Port", line: "Port {{ sshd_port }}" }

    - { regexp: "^#?UseDNS", line: "UseDNS {{ sshd_use_dns }}" }

    - { regexp: "^GSSAPIAuthentication", line: "GSSAPIAuthentication {{ sshd_gssapi_authentication}}" }

    - { regexp: "^#?Protocol", line: "Protocol {{ sshd_protocol }}" }

    - { regexp: "^#?PermitRootLogin", line: "PermitRootLogin {{ sshd_permit_root_login }}" }

    - { regexp: "^PasswordAuthentication", line: "PasswordAuthentication {{ sshd_password_authentication }}" }

    - { regexp: "^#?RSAAuthentication", line: "RSAAuthentication yes" }

    - { regexp: "^#?PubkeyAuthentication", line: "PubkeyAuthentication yes" }

    - { regexp: "^#?RhostsRSAAuthentication", line: "RhostsRSAAuthentication yes" }

   

  notify: restart sshd

#更新sshd文件配置,更改sshd端口,是否开启sshd_use_dns,是否开启sshd_gssapi_authentication是否开启,

#设置sshd协议 2,禁止root登录(先不设置)是否开启密码登录(先不禁止密码登录)

- name: Update SSH configuration to be standard setting.

  lineinfile: >

    dest={{ ssh_config_path }}

    regexp="{{ item.regexp }}"

    line="{{ item.line }}"

    state=present

  with_items:

    - { regexp: "^#?StrictHostKeyChecking", line: "StrictHostKeyChecking no" }

   

  notify: restart sshd

3 安装docker

---

#配置主机名

#https://www.jianshu.com/p/b4a6239712bf

#还需要更新docker镜像源!!

- name: Configure hostname

  hostname:

    name: "{{ host_name }}"

- name: install yum-utils

  yum: name=yum-utils state=present

  ignore_errors: True

- name: add docker repo

  shell: yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

  ignore_errors: True

- name: install docer-ce

  yum:

    name: docker-ce

    state: present

  ignore_errors: True

- name: install docker-ce-cli

  yum:

    name: docker-ce-cli

    state: present

  ignore_errors: True

- name: install containerd.io

  yum:

    name: containerd.io

    state: present

  ignore_errors: True

- name: 创建docker目录

  shell: mkdir -p /etc/docker/

- name: config mirro

  copy: src=~/docker-daemon.json dest=/etc/docker/daemon.json

  tags: configmirro

- name: start enable docker

  service: name=docker state=started enabled=true

- name: restrat

  shell: sudo systemctl daemon-reload && sudo systemctl restart docker

  tags: restart

#必须在ansible主机之上,的/data/archive下存在docker-compose文件

- name: Copy docker compose file

  copy:

    src:  "{{ playbook_dir }}/data/archive/docker-compose"

    dest: "/usr/local/bin/docker-compose"

- name: install docker compose

  shell: "{{ item }}"

  with_items:

      - "sudo chmod +x /usr/local/bin/docker-compose"

      - "sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose"

      - "sudo docker-compose --version"

  ignore_errors: True

     

       

4 设置hosts映射

#更新ip-host映射  

- name: update-ip-host-mapping

  template:

    src: host-file.j2

    dest: /etc/hosts

    mode: 0644

 GIT 项目访问地址

https://gitee.com/haofeng82/basic-host-machine--init.git

 

大家可以在评论区留言,分享自己使用ansible脚本搭建环境的经验和技巧,让我们一起交流吧!记得点赞和分享哦!