Ansible安裝
阿新 • • 發佈:2020-12-27
安裝方式
##### yum yum install epel-release -y yum list ansible yum install ansible -y ansible --version
#####git
gitclonegit://github.com/ansible/ansible.git--recursive
cd./ansible
source./hacking/env-setup
ansible--version
配置優化
vim /etc/ansible/ansible.cfg host_key_checking = False #不檢查key log_path = /var/log/ansible.log #開啟日誌
主機列表中常用的引數說明
ansible_ssh_host #將要連線的遠端主機名.與你想要設定的主機的別名不同的話,可通過此變數設定. ansible_ssh_port #ssh埠號.如果不是預設的埠號,通過此變數設定.也可以使用 ip:埠 192.168.1.100:2222 ansible_ssh_user #預設的 ssh 使用者名稱 ansible_ssh_pass #ssh 密碼(這種方式並不安全,我們強烈建議使用 --ask-pass 或 SSH 金鑰) ansible_sudo_pass #sudo 密碼(這種方式並不安全,我們強烈建議使用 --ask-sudo-pass)
#Ansible 主要程式/usr/bin/ansible 主程式,臨時命令執行工具 /usr/bin/ansible-doc 檢視配置文件,模組功能檢視工具 /usr/bin/ansible-galaxy 下載/上傳優秀程式碼或Roles模組的官網平臺 /usr/bin/ansible-playbook 定製自動化任務,編排劇本工具 /usr/bin/ansible-pull 遠端執行命令的工具
ansible playbook 劇本例項
#批量下發公鑰至被控主機 --- - hosts: test remote_user: root tasks: - name: 將公鑰批量寫入被管理機 authorized_key: user: root key:"{{ lookup('file', '/root/.ssh/id_rsa.pub') }}" #/etc/ansible/hosts格式 [test] 10.10.10.1 10.10.10.2 10.10.10.[3:5] [test:vars] ansible_ssh_pass=test0123 #批量寫入公鑰後請刪除掉
#批量建立使用者 --- - hosts: test remote_user: root tasks: - name: 批量建立使用者 user: name={{ item.name }} group={{ item.group }} password={{ '123456' | password_hash('sha512') }} with_items: - { name: 'buster', group: 'buster' } - { name: 'app', group: 'app' }
#批量修改使用者密碼 --- - hosts: test remote_user: root tasks: - name: 批量修改使用者密碼 user: name={{ item.name }} password={{ item.chpass | password_hash('sha512') }} update_password=always with_items: - { name: 'buster', chpass: 'buster123' } - { name: 'app', chpass: 'app123@..!!' } #特殊的字元建議先單臺執行後檢查登入
#批量刪除使用者 --- - hosts: test remote_user: root tasks: - name: 批量刪除使用者 user: name={{ item.name }} group={{ item.group }} password={{ '123456'|password_hash('sha512') }} state=absent remove=yes with_items: - { name: 'buster', group: 'buster' } - { name: 'app', group: 'doordu' }
#優化ssh配置 --- - hosts: test remote_user: root tasks: - name: 批量修改ssh配置檔案的安全選項 lineinfile: path: /etc/ssh/sshd_config regexp: '{{ item.regexp }}' line: '{{ item.line }}' state: present with_items: - regexp: "^#UseDNS yes" line: "UseDNS no" - regexp: "^#Port 22" line: "Port 12222" notify: - restart sshd handlers: - name: restart sshd service: name: sshd state: restarted
- hosts: test user: root tasks: - name: 批量修改主機名 raw: "hostnamectl set-hostname {{ hostname|quote }}" #/etc/ansible/hosts格式 [test] 10.10.10.1 hostname=test01 10.10.10.2 hostname=test02 ...
#批量初始化新機器 - hosts: test remote_user: root tasks: - name: 關閉防火牆 service: name=firewalld state=stopped enabled=no tags: firewalld - name: 臨時關閉 selinux shell: "setenforce 0" failed_when: false - name: 永久關閉 selinux replace: dest: /etc/selinux/config regexp: ^SELINUX=enforcing replace: SELINUX=disabled tags: selinux - name: 新增114.114.114.114 DNS shell: grep 114 /etc/resolv.conf || sed -i '/NetworkManager/a\nameserver 114.114.114.114' /etc/resolv.conf tags: dns - name: 下載阿里雲yum源 get_url: url: http://mirrors.aliyun.com/repo/Centos-7.repo dest: /etc/yum.repos.d/CentOS-Base.repo backup: yes ignore_errors: true tags: 阿里雲yum源 - name: 替換阿里雲地址 replace: dest: /etc/yum.repos.d/CentOS-Base.repo regexp: mirrors.cloud.aliyuncs.com replace: mirrors.aliyuncs.com tags: sed aliyun yum - name: 清理yum快取 shell: yum clean all && yum makecache - name: 安裝常用軟體包 yum: name: "{{ item }}" state: installed with_items: - epel-release - vim - lrzsz - wget - ntpdate tags: 常用工具 - name: 同步伺服器時間 shell: ntpdate time.nist.gov && hwclock -w && ulimit -SHn 65535 ignore_errors: true tags: ntp - name: 建立同步時間計劃任務 cron: name: 'ntpdate time' hour: '0' minute: '10' day: '*' month: '*' weekday: '*' job: '/usr/sbin/ntpdate -u time.windows.com time-b.nist.gov ntp.api.bz;hwclock -w' user: 'root' tags: ntpdate time - name: 設定開啟的檔案描述符 lineinfile: dest: /etc/security/limits.conf line: "{{ item }}" with_items: - '* soft nofile 102400' - '* hard nofile 102400' - '* soft nproc 102400' - '* hard nproc 102400' tags: limits