ansible 和 saltstack
都是為了同時在多台主機上執行相同的命令, 但是 salt配置麻煩,ansible基本不用配置, ansible 通過ssh來連接並控制被控節點
1. 安裝
第一步: 下載epel源
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
yum install -y ansible
2. 管控主機秘鑰登錄被控主機
ssh 秘鑰登錄
ssh-keygen # 用來生成ssh的密鑰對 ssh-copy-id 192.168.107.131 # 復制秘鑰到遠程主機
3. ansible 命令格式

-a MODULE_ARGS, --args=MODULE_ARGS # 模塊的參數 -C, --check # 檢查 -f FORKS, --forks=FORKS #用來做高並發的 --list-hosts #列出主機列表 -m MODULE_NAME #模塊名稱 --syntax-check # 語法檢查
4. ansible hosts
查看ansible 生成的文件

ansible/hosts 文件
# This is the default ansible 'hosts' file. # # It should live in /etc/ansible/hosts # # - Comments begin with the '#' character # 用#來表示注釋 # - Blank lines are ignored # 空白行被忽略 # - Groups of hosts are delimited by [header] elements # 主機組 需要在【】下面 # - You can enter hostnames or ip addresses #可以寫主機名或者ip地址 # - A hostname/ip can be a member of multiple groups # 一台主機可以在多個組里面
[web]
192.168.181.133
192.168.181.134
[db]
192.168.181.134
192.168.181.135
5. 模塊
ansible-doc 查看文檔
ansible-doc [-l|-F|-s] [options] [-t <plugin type> ] [plugin] -j #以json的方式返回ansible的所有模塊 -l, --list #列出所有的ansible的模塊 -s #以片段式顯示ansible的幫助信息

1. ping模塊
host-pattern格式

2. 系統默認模塊 command, 可以不指定-m
第一個command命令

ansible web -a 'chdir=/tmp pwd' # 切換目錄執行命令,使用場景是編譯安裝時使用 ansible web -a 'creates=/tmp pwd' # 用來判斷/tmp目錄是否存在,存在就不執行操作 ansible web -a 'creates=/data pwd' # 因為data不存在,所有才會執行pwd命令 ansible web -a 'removes=/tmp pwd' # 用來判斷tmp目錄是否存在,存在就執行操作 ansible web -a 'removes=/data pwd' # 因為data不存在,所有才不會執行
如果命令里包含特殊符號, 需要同shell模塊 : $ < > | will not work use shell module
3. shell 執行遠程文件
ansible web -m shell -a 'echo "123" | passwd --stdin alex' # 批量創建密碼 ansible 192.168.107.131 -m shell -a 'bash a.sh' # 執行遠程文件方式一 ansible 192.168.107.131 -m shell -a '/root/a.sh' # 執行遠程文件方式二,文件必須有執行權限, 需要 chmod +x ansible 192.168.107.131 -m shell -a '/root/a.py' # 執行遠端的Python腳本
被管控機192.168.107.131

管控機就會創建 bulijngbuling2文件夾
4.script 執行本地文件
ansible web -m script -a '/root/m.sh' # 執行本地的文件,即管控機上的腳本 ansible web -m script -a 'removes=/root/m.sh /root/m.sh' # 用來判斷被管控機上是不是存在文件,如果存在,存在就執行,不存在就不執行 ansible web -m script -a 'creates=/root/a.sh /root/m.sh' #用來判斷被管控機上是不是存在文件,如果存在,就不執行
管控機

db組群 就會創建 hhhhhh文件夾
5. copy :
ansible-doc -s copy:
backup # 備份,以時間戳結尾
src # 源文件 dest # 目的地址 group # 文件的屬組 mode # 文件的權限 r 4 w 2 x 1 owner #文件的屬主 # 通過md5碼來判斷是否需要復制 ansible web -m copy -a 'src=/root/m.sh dest=/tmp/a.sh' # 復制本地文件的到遠程主機 ansible web -m copy -a 'src=/root/m.sh dest=/tmp/a.sh mode=755' # 修改文件的權限 ansible web -m copy -a 'src=/root/m.sh dest=/tmp/a.sh mode=755 owner=ryan' # 修改文件的屬主 ansible web -m copy -a 'src=/etc/init.d dest=/tmp/ mode=755 owner=ryan' # 復制本地目錄到遠程主機,如果改變文件的屬性,則文件夾內的文件也會被改變 ansible web -m copy -a 'src=/etc/init.d/ dest=/tmp/ mode=755 owner=ryan' # 復制本地目錄內的所有文件到遠程主機 ansible web -m copy -a "content='白雲深處有人家\n' dest=/tmp/b.txt" # 直接將文本內容注入到遠程主機的文件中
6. fetch : Fetches a file from remote nodes 把遠程文件傳到管控機, 如各機的log日志等, 與copy相反
dest # 目的地址 (required) A directory to save the file into src # 源地址 (required) The file on the remote system to fetch,This `must' be a file, not a directory ansible web -m fetch -a 'src=/var/log/cron dest=/tmp' # 下載被控節點的文件,在管控機/tmp目錄下以每台機器ip為名創建一個文件夾,並保留原來的目錄結構

7. file
path: # (required) Path to the file being managed. src: # path of the file to link to (applies only to `state=link' and `state=hard') state: # directory touch link absent owner: # chown'
ansible db -m file -a 'path=/lzmly2 state=directory' #在遠程機器上創建文件夾 ansible db -m file -a 'path=/root/q.txt state=touch' #用來在遠程機器上創建文件 ansible db -m file -a 'path=/tmp/f src=/etc/fstab state=link' #創建軟連接src是源地址,path是目標地址 ansible db -m file -a 'path=/tmp/f state=absent' #用來刪除文件或者文件夾 gruop /owner /mode
8. yum
yum和rpm 的區別: yum解決依賴關系
rpm -q 包 查詢是否安裝
yum 安裝包組
yum grouplist # 查看包組信息
yum groupinstall # 安裝包組
ansible 語法
disablerepo # 禁用源 enablerepo # 啟用源 name # 包名 state # install (`present' or `installed', `latest'), or remove (`absent' or `removed')
ansible web -m yum -a 'name=wget' # 安裝wget ansible web -m yum -a 'name=python2-pip' # 安裝python2-pip ansible web -m yum -a 'name=wget state=absent' # 卸載軟件包 ansible web -m yum -a 'name="@Development Tools"' # 安裝包組
9. pip
pip install # 安裝包 pip freeze > a.txt # 將python的環境打包到文件中 pip install -r a.txt # 安裝文件中的包 pip list # 查看所有的以安裝成功的包
ansible 語法
requirements # The path to a pip requirements file, which should be local to the remote system. 就是pip install -r 指定文件安裝 name # 名
ansible web -m pip -a 'name=flask' # 安裝flask模塊
10. service
systemctl start nginx # centos7 啟動nginx
service nginx start # centos6
systemctl enabled nginx # centos7 開機自啟動
chkconfig nginx on # centos6 開機自啟動
ansible:
enabled # Whether the service should start on boot. 開機啟動 name # (required) Name of the service state # started stopped restarted reloaded
ansible web -m service -a 'name=nginx state=started enabled=yes' # 啟動nginx, 開機啟動nginx ansible web -m service -a 'name=nginx state=stopped' # 關閉nginx
11. cron 計划任務
* * * * * job 分 時 日 月 周 任務 0 */2 * * * job 每隔兩個小時 0 12,13 * * * job 12點和13點 0 12-17 * * * job 12點到17點 0 12-17/2 * * 1,3,6,0 周1,周3,周6,周7 12點到17點每隔兩個小時 crontab -e # 編輯計划任務 crontab -l # 查看計划任務 crontab -r # 刪除計划任務
ansible
minute # 分鍾 hour # 小時 day # 天 month # 月 weekday # 周 name # 任務名字 job # 任務 disabled # 禁用
ansible db -m cron -a 'minute=26 job="touch /tmp/xzmly.txt" name=touchfile' # 新建一個計划任務 ansible db -m cron -a 'name=touchfile state=absent' # 刪除一個計划任務 ansible db -m cron -a 'minute=26 job="touch /tmp/xzmly.txt" name=touchfile disabled=yes' # 禁用計划任務,以#表示禁用 ansible web -m cron -a 'name=synchronus minute=00 job="ntpdate time.window.com"' # 每小時同步window時間
12 user
用戶: 管理員 root 0 普通用戶 系統用戶 不能登錄 1-999 centos7 1-499 centos6 登錄用戶 可以登錄 1000-65535 centos7 500-65535 centos6 用戶組: 管理員組 root 0 系統用戶組 1-999 centos7 1-499 centos6 登錄用戶組 1000-65535 centos7 500-65535 centos6 -d 指定用戶的家目錄 -g 指定用戶的組 -G 執行用戶的附加組 -s 指定登錄后使用的shell -r 創建一個系統組 -p password useradd -r ryan # 創建系統用戶, 從999倒序 useradd -s /sbin/nologin may # 創建的是普通用戶,從1000開始升序 useradd -d /opt/shey shey # 創建用戶時指定用戶的家目錄 useradd -u 3000 sheldon # 創建用戶並指定用戶的uid userdel sheldon # 刪除用戶 userdel -r shey # 刪除用戶並刪除用戶的家目錄
ansible
group # 組 groups # 附加組 home # 家目錄 name # 用戶名 password # 密碼 remove # 只有當state=absent 起作用, 刪除用戶家目錄 shell # 用戶登錄后使用的shell system # 創建一個系統用戶 uid # 用來指定用戶的id state # 狀態 ansible db -m user -a 'name=ryan uid=4000 home=/opt/wulaoshi groups=root shell=/sbin/nologin' #創建一個用戶,並指定用戶的id,用戶的家目錄,用戶的附加組,用戶的shell ansible db -m user -a 'name=ryan1 state=absent' #刪除用戶但是不刪除用戶的家目錄 ansible db -m user -a 'name=ryan2 state=absent remove=yes' # 刪除用戶並刪除用戶的家目錄
13 group
gid # 組的id name # 組名 system # 系統組 state ansible db -m group -a 'name=ryan3 system=yes' #創建系統組 ansible db -m group -a 'name=ryan4 state=absent' # 刪除組
其他模塊遇到的時候再補充
