ansible 和 saltstacknode
都是爲了同時在多臺主機上執行相同的命令, 可是 salt配置麻煩,ansible基本不用配置, ansible 經過ssh來鏈接並控制被控節點python
1. 安裝nginx
第一步: 下載epel源web
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
第二步: 安裝ansibleshell
yum install -y ansible
2. 管控主機祕鑰登陸被控主機json
ssh 祕鑰登陸flask
ssh-keygen # 用來生成ssh的密鑰對 ssh-copy-id 192.168.107.131 # 複製祕鑰到遠程主機
-a MODULE_ARGS, --args=MODULE_ARGS # 模塊的參數 -C, --check # 檢查 -f FORKS, --forks=FORKS #用來作高併發的 --list-hosts #列出主機列表 -m MODULE_NAME #模塊名稱 --syntax-check # 語法檢查
4. ansible hostscentos
查看ansible 生成的文件bash
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 : Copies files to remote locations 把文件批量拷到被管控機上
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' # 刪除組
其餘模塊遇到的時候再補充