大家好,今天我要和大家分享一個(gè)關(guān)于搭建centos環(huán)境的新方法。
以前我們經(jīng)常會(huì)看到一些文章介紹如何搭建centos環(huán)境,但很多時(shí)候都會(huì)出現(xiàn)一些問(wèn)題。不過(guò)現(xiàn)在有了一種新的方法,就是使用ansible腳本來(lái)實(shí)現(xiàn)。
雖然這種方法僅適用于centos7,但只要稍作修改就可以應(yīng)用到其他的環(huán)境中。使用ansible腳本可以讓搭建環(huán)境更加簡(jiǎn)單、快速、穩(wěn)定。
?GIT 項(xiàng)目訪問(wèn)地址
https://gitee.com/haofeng82/basic-host-machine--init.git
目錄
介紹
軟件架構(gòu)
安裝教程
使用說(shuō)明
初始化操作用戶。
? ? ?2.?主機(jī)基本功能初始化。
?? ?3.為機(jī)器設(shè)置host映射。
????????可能存在的問(wèn)題
腳本摘要
1 初始化操作賬戶
2 初始化centos系統(tǒng)
3 安裝docker
4 設(shè)置hosts映射
?GIT 項(xiàng)目訪問(wèn)地址
介紹
這是一套ansbile腳本,實(shí)現(xiàn)了始化基于centos7系統(tǒng)的服務(wù)器主機(jī)基礎(chǔ)環(huán)境的功能。 目前僅用于測(cè)試和教學(xué)環(huán)境使用,并僅保證centos77版本有效。
軟件架構(gòu)
軟件基于ansible腳本對(duì)服務(wù)器進(jìn)行初初始化工作,主要工作如下: 1 增加ops做操作賬戶,并為其增加ssh證書(shū)登錄,此用戶可以切換至root。 2 并且禁用了root登錄。所有需要root的行為,都會(huì)通過(guò)ops 進(jìn)行become 3 禁用了防火墻等功能。統(tǒng)一使用外部防火墻。 4 安裝了必要的系統(tǒng)包,并進(jìn)行了系統(tǒng)優(yōu)化。 5 安裝了docker,因?yàn)楹罄m(xù)的一些其他項(xiàng)目的安裝,會(huì)用到docker。 6 提供了統(tǒng)一設(shè)置主機(jī)host的功能
安裝教程
1需要找一臺(tái)服務(wù)器,安裝ansible環(huán)境,然后把當(dāng)前項(xiàng)目復(fù)制到相應(yīng)的目錄下。
2依據(jù)后面的使用步驟執(zhí)行腳本即可。 PS:主機(jī)目錄文件需要自行進(jìn)行修改。 證書(shū)使用了預(yù)先生成好的證書(shū),也可以自行生成配置。
使用說(shuō)明
-
初始化操作用戶。
????????當(dāng)拿到一臺(tái)新機(jī)器時(shí),首先需要對(duì)其進(jìn)行用戶限制。 前提:將主機(jī)設(shè)置為可以使用root登錄。密碼統(tǒng)一初始化為XXX.
????????主要操作如下: 1 禁用root的遠(yuǎn)程登錄。 2 禁用密碼登錄。 3 創(chuàng)建operator操作用戶,及operator組。并為其設(shè)置登錄密碼、上傳登錄共公鑰。
????????上面的所有操作,均在init-user角色中,并通過(guò)下面的腳本調(diào)用
????????上傳整個(gè)目錄到ansible主機(jī)的/ansible/basic-host-machine目錄下,并對(duì)host文件中的to-init-user-host分組中的主機(jī)ip進(jìn)行設(shè)置。指定為要操作的機(jī)器。 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.?主機(jī)基本功能初始化。
????????此步驟必須依賴(lài)第一步 在執(zhí)行完初始化用戶之后。會(huì)以operator用戶進(jìn)行登錄。 此時(shí)的root用戶已經(jīng)被禁用掉了。
????????上傳整個(gè)目錄到ansible主機(jī)的/ansible/basic-host-machine目錄下,并對(duì)host文件中的to-init-env-host分組中的主機(jī)ip進(jìn)行設(shè)置。指定為要操作的機(jī)器。
????????主要工作包括: 安裝必要的工具包 開(kāi)啟或者關(guān)閉防火墻以及selinux 安裝docker環(huán)境
????????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.為機(jī)器設(shè)置host映射。
????????此步驟必須依賴(lài)第一步 統(tǒng)一對(duì)主機(jī)的hosts進(jìn)行配置
????????上傳整個(gè)目錄到ansible主機(jī)的/ansible/basic-host-machine目錄下,并對(duì)host文件中的to-init-user-host分組中的主機(jī)ip進(jìn)行設(shè)置。指定為要操作的機(jī)器。 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
????????可能存在的問(wèn)題
變量的配置還是有一些不合適的地方。需要再進(jìn)行修改。目前僅是提交了初始版本。
代碼放到了git上,需要的小伙伴直接拿來(lái)用就好。如果有不嚴(yán)謹(jǐn)?shù)牡胤?,也希望能夠指正出?lái)。
腳本摘要
1 初始化操作賬戶
---
#關(guān)閉防火墻
- name: close fire wall
? shell: "{{ item}}"
? with_items:
? ? - "systemctl stop firewalld.service"
? ? - "systemctl disable firewalld.service"
#關(guān)閉selinux
- name: Disabled SELinux
? selinux: state=disabled
#下次啟動(dòng)也不會(huì)再起selinux
- name: set selinux disabled
? replace:
? ? path: /etc/selinux/config
? ? regexp: '^SELINUX=enforcing'
? ? replace: 'SELINUX=disabled'
##創(chuàng)建用戶分組
- name: Create group
? group: ?name={{group}} state=present
? when: add_user
#當(dāng)add_user變量為true,創(chuàng)建用戶。此用戶用于運(yùn)行項(xiàng)目。
- name: Add sudo user
? user:
? ? name: "{{ user }}"
? ? password: "{{ operation_user_password }}"
? ? state: present
? when: add_user
?文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-426390.html
#將證書(shū)拷貝至遠(yuǎn)程機(jī)器,用戶為root,這一步是為了能夠通過(guò)證書(shū)ssh訪問(wèn)遠(yuǎn)程主機(jī)
- name: install ssh key
? authorized_key: user={{user}}
? ? ? ? ? ? ? ? ? key="{{ lookup('file','/root/.ssh/id_rsa.pub')}}"
? ? ? ? ? ? ? ? ? state=present
?
#將用戶臨時(shí)切換到為sudo root用戶時(shí),設(shè)置為不需要密碼。
- 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系統(tǒng)
---
#配置主機(jī)名
- name: Configure hostname
? hostname:
? ? name: "{{ host_name }}"
- name: install basic tools1
? yum:
? ? name: "{{ item }}"
? ? state: present
? with_items:
? ? ? - "wget"
? ? ?
? ?
#更換yum阿里數(shù)據(jù)源 ? ?
- 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"
#安裝基礎(chǔ)工具
#- 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"
#復(fù)制cronolog rpm安裝文件到服務(wù)器
- 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 ? ?
?
#臨時(shí)修改linux文件句柄數(shù)(用戶級(jí)別)
- 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 配置文件,優(yōu)化系統(tǒng)性能參數(shù)
- 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"
?
? ?
#將證書(shū)復(fù)制到用戶的.ssh目錄下,并更改權(quán)限。
- name: copy auth public ?key to host file
? copy:
? ? src: ?"/root/.ssh/id_rsa.pub"
? ? dest: "/home/{{ user }}/.ssh/"
? ? owner: "{{ user }}"
? ? group: "{{ group }}"
#私鑰的權(quán)限一定不要?jiǎng)e的用戶和組能夠讀取到
- 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端口,是否開(kāi)啟sshd_use_dns,是否開(kāi)啟sshd_gssapi_authentication是否開(kāi)啟,
#設(shè)置sshd協(xié)議 2,禁止root登錄(先不設(shè)置)是否開(kāi)啟密碼登錄(先不禁止密碼登錄)
- 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端口,是否開(kāi)啟sshd_use_dns,是否開(kāi)啟sshd_gssapi_authentication是否開(kāi)啟,
#設(shè)置sshd協(xié)議 2,禁止root登錄(先不設(shè)置)是否開(kāi)啟密碼登錄(先不禁止密碼登錄)
- 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
---
#配置主機(jī)名
#https://www.jianshu.com/p/b4a6239712bf
#還需要更新docker鏡像源?。?/p>
- 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: 創(chuàng)建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主機(jī)之上,的/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 設(shè)置hosts映射
#更新ip-host映射 ?
- name: update-ip-host-mapping
? template:
? ? src: host-file.j2
? ? dest: /etc/hosts
? ? mode: 0644
?GIT 項(xiàng)目訪問(wèn)地址
https://gitee.com/haofeng82/basic-host-machine--init.git
?
大家可以在評(píng)論區(qū)留言,分享自己使用ansible腳本搭建環(huán)境的經(jīng)驗(yàn)和技巧,讓我們一起交流吧!記得點(diǎn)贊和分享哦!文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-426390.html
到了這里,關(guān)于基于ansible初始化linux服務(wù)器基礎(chǔ)環(huán)境。的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!