# yum install openssh-server -y
# sshd配置文件是:sudo vi /etc/ssh/sshd_config
# PasswordAuthentication yes # 是否允許使用基于密碼的認證。默認為"yes"。
# sudo systemctl start sshd
# sudo systemctl enable sshd
# 刪除用戶
userdel test
# 顯示所有用戶
cut -d : -f 1 /etc/passwd
ansible inventories
- available on github from:
- clone to control host with the command
部署
inventory 編寫
- mac 默認的ansible的hosts文件路徑:/etc/ansible/hosts
# 列舉host命令: allservers為分組的名稱
ansible allservers --list-hosts -v
- 組,子組,定義變量
- 靜態(tài)主機清單,可以通過文本文件來定義。
# /etc/ansible/hosts文件配置如下
[webservers]
web1.example.com
web2.example.com
[dbservers]
db1.example.com
db2.example.com
[allservers:children]
webservers
dbservers
# 測試清單命令
ansible allservers --list-hosts
# 列舉指定路徑下的host文件:/Users/kate/code/script/ansible/hosts
# -v 輸出ansible.cfg配置路徑
ansible allservers --list-hosts -i /Users/kate/code/script/ansible/hosts -v
配置文件
-
優(yōu)先級(高到低)
- 使用 $ANSIBLE_CONFIG 變量指定配置文件路徑,優(yōu)先級最高
- 放置執(zhí)行ansible命令的目錄下:
- 當前用戶主目錄下:~/.ansible.cfg
- 全局配置文件路徑:/etc/ansible/ansible.cfg
執(zhí)行 Ansible 命令時使用 -v 選項輸出配置的位置
Ansible 配置文件中的配置分組
# 配置文件中的大部分設置都在這個分組里
[dafaults]
# 服務器清單位置
# 命令行選項 -
inventory = ./inventory
# 用于建立與受管主機連接的控制主機用戶
# 命令行選項 -u
remote_user = root
# 以受管主機用戶連接時,是否要輸入密碼
ask_pass = true
# 定義如何對受管主機執(zhí)行需要特權(quán)升級的操作。
[privilege_escalation]
# 為受管主機上的操作啟用或禁用特權(quán)升級
# 命令行選項 --become、-b
become = true
# 要在受管主機上使用的特權(quán)升級方法
# 命令行選項 --become-method
become_method = sudo
# 在受管主機上升級特權(quán)的用戶
# 命令行選項 --become-user
become_user = root
# 定義受管主機上的特權(quán)升級是否提示輸入密碼
# 命令行選項 --ask-become-pass、 -K
become_ask_pass = false
# 用于優(yōu)化與受管主機的連接
[paramiko_connection]
[ssh_connection]
[accelerate]
# 定義如何配置 SELinux 交互
[selinux]
- Ansible 配置項
運行臨時命令
-
運行臨時命令范式
ansible host-pattern -m module [-a 'module arguments'] [-i inventory]- host-pattern - 用于指定應在其上運行臨時命令的受管主機。它可以是清單中的特定受管主機或主機組。
- -m - Ansible 應在目標主機上運行的 模塊的名稱取為參數(shù)。模塊是為了實施您的任務而執(zhí)行的小程序。
- -a - 以帶引號字符串形式取這些參數(shù)的列表。
- -i - 指定要使用的其他清單位置,取代當前 Ansible 配置文件中的默認位置。
-
列出所有模塊
ansible-doc -l
-
查看模塊文檔
- ansible-doc ping
# 生成本地ssh秘鑰
# 將本地秘鑰發(fā)給遠端服務器(例:用戶名為root ip地址為192.168.1.1)
ssh-keygen
ssh-copy-id vagrant@192.168.33.10
動態(tài)清單
- 如果清單文件可以執(zhí)行,它將被視為動態(tài)清單程序,Ansible 則將嘗試運行它來生成清單。如果文件不可執(zhí)行,它將被視為靜態(tài)清單。
Playbooks
ansible playbook VS 臨時命令
# 運行
ansible-playbook sample.yml
# 語法驗證
ansible-playbook --syntax-check sample.yml
# 執(zhí)行空運行
ansible-playbook -C sample.yml