- ansible安裝node
- 下載epel源python
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
- 安裝ansiblenginx
yum install -y ansible
- ansible 經過ssh來鏈接並控制被控節點web
- ssh 的認證方式shell
密碼鏈接json
祕鑰鏈接flask
- ssh密鑰登錄centos
ssh-keygen # 用來生成ssh的密鑰對 提示直接過 ssh-copy-id 192.168.33.132 # 複製祕鑰到遠程主機
- ansible命令格式bash
ansible <host-pattern> [options] -a MODULE_ARGS, --args=MODULE_ARGS #模塊的參數 -C, --check # 檢查 -f FORKS, --forks=FORKS #用來作高併發的 --list-hosts #列出主機列表 -m MODULE_NAME #模塊名稱 --syntax-check # 語法檢查 -k 輸入密碼
- 查看ansible生成的文件併發
rpm -ql ansible /etc/ansible /etc/ansible/ansible.cfg /etc/ansible/hosts /etc/ansible/roles
- 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 # 一臺主機能夠在多個組裏面 www[001:006].example.com #表示從www001到www006的機器
- host-pattern的格式
- 單個主機
ansible 192.168.33.132 -m ping
- 所有主機
ansible all -m ping
- 多個主機
ansible 192.168.33.132,192.168.33.133 -m ping
注意每一個主機都得密鑰登錄以後才能ping
- 分組
- 單個組
ansible web --list-hosts
- 多個組
- 並集
ansible web,db --list-hosts # 或者
ansible ‘web:db’ --list-hosts
- 交集
ansible 'web:&db' --list-hosts
- 差集
ansible 'web:!db' --list-hosts
-
ansible-doc [-l|-F|-s] [options] [-t <plugin type> ] [plugin] -j #以json的方式返回ansible的全部模塊 -l, --list#列出全部的ansible的模塊 -s#以片斷式顯示ansible的幫助信息
-
執行遠程命令
ansible web -a 'ls /'#第一個命令 ansible web -a 'pwd' 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不存在,全部纔不會執行
- 查看用戶是否建立成功
tail -1 /etc/passwd tail -1 /etc/shadow id alex echo '123' | passwd --stdin jam #設置密碼
- shell(執行遠程主機的shell/python腳本,支持管道
)
ansible web -m shell -a 'echo "123" | passwd --stdin jam' # 批量建立密碼 ansible 192.168.107.131 -m shell -a 'bash a.sh' # 執行遠程文件方式一 ansible 192.168.107.131 -m shell -a '/root/a.sh' #執行遠程文件方式二,文件必須有執行權限 ansible 192.168.107.131 -m shell -a '/root/a.py' # 執行遠端的Python腳本
- script(在遠程主機執行主控端的shell/python腳本
)
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' #用來判斷被管控機上是否是存在文件,若是存在,就不執行
- 文件相關模塊
- copy
backup 備份,以時間戳結尾 dest 目的地址 group 文件的屬組 mode 文件的權限 r 4 w 2 x 1 owner 文件的屬主 src 源文件 # 經過md5碼來判斷是否須要複製 ansible db -m copy -a 'src=/root/m.sh dest=/tmp/a.sh' #複製本地文件的到遠程主機 ansible db -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=jam1' 修改文件的屬主 ansible web -m copy -a 'src=/etc/init.d dest=/tmp/ mode=755 owner=jam1' # 複製本地目錄到遠程主機,若是改變文件的屬性,則文件夾內的文件也會被改變 ansible web -m copy -a 'src=/etc/init.d/ dest=/tmp/ mode=755 owner=jam1' # 複製本地目錄內的全部文件到遠程主機 ansible web -m copy -a "content='擡頭望明月,低頭思故鄉\n' dest=/tmp/b.txt" # 直接將文本內容注入到遠程主機的文件中
- file
inode 硬盤的地址 id 獲取到的是內存的地址 ln -s a.py b.py 建立軟鏈接 ln a.py c.py 建立硬連接 當 源文件變化時,軟鏈接和硬連接文件都會跟着變化
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' #用來刪除文件或者文件夾
- fetch
dest 目的地址 src 源地址 ansible web -m fetch -a 'src=/var/log/cron dest=/tmp' # 下載被控節點的文件,每臺機器建立一個文件夾,並保留原來的目錄結構
- 軟件相關模塊
- yum
# yum源配置
[epel] name=Extra Packages for Enterprise Linux 7 - $basearch #名字 baseurl=http://mirrors.aliyun.com/epel/7/$basearch #rpm源的地址,能夠寫http,https,ftp,Samba,file: failovermethod=priority enabled=1 # 是否開啓,1表明開啓,0表示關閉 gpgcheck=0 #是否校驗簽名,1表明校驗,0表示不校驗 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
# yum安裝包組 yum grouplist # 查看包組信息 yum groupinstall # 安裝包組
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"' # 安裝包組
- pip
pip install 安裝包 pip freeze > a.txt 將python的環境打包到文件中 pip install -r a.txt 安裝文件中的包 pip list 查看全部的以安裝成功的包
ansible web -m pip -a 'name=flask' # 安裝flask模塊
-
ps -ef|grep nginx #查看進程 ss -tnlp # 查看端口信息 systemctl start nginx # centos7 service nginx start # centos6 systemctl enabled nginx # centos7 開機自啓動 chkconfig nginx on # centos6開機自啓動
ansible web -m service -a 'name=nginx state=started' # 啓動nginx ansible web -m service -a 'name=nginx state=stopped' # 關閉nginx
- 計劃任務
- 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 # 刪除計劃任務
day 天
disabled 禁用
hour 小時
job 任務
minute 分鐘
month 月
name 任務名字
weekday 周
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' # 禁用計劃任務,以#表示禁用
用戶: 管理員 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 建立一個系統組 useradd -r jam 建立系統用戶, 從999倒序 useradd -s /sbin/nologin jam1 建立的是普通用戶,從1000開始升序 useradd -d /opt/jam2 jam2 建立用戶時指定用戶的家目錄 useradd -u 3000 jam3 # 建立用戶並指定用戶的uid userdel jam 刪除用戶 userdel -r jam 刪除用戶並刪除用戶的家目錄 groupadd jamlee 建立用戶組 groupdel jamlee 刪除用戶組
group 組 groups 附加組 home 家目錄 name 用戶名 password 密碼 remove ? shell 用戶登陸後使用的shell system 建立一個系統用戶 uid 用來指定用戶的id state 狀態 ansible db -m user -a 'name=canglaoshi uid=4000 home=/opt/wulaoshi groups=root shell=/sbin/nologin' #建立一個用戶,並指定用戶的id,用戶的家目錄,用戶的附加組,用戶的shell ansible db -m user -a 'name=canglaoshi state=absent' #刪除用戶可是不刪除用戶的家目錄 ansible db -m user -a 'name=canglaoshi state=absent remove=yes' # 刪除用戶並刪除用戶的家目錄
- group
gid 組的id name 組名 system 系統組 state ansible db -m group -a 'name=canglaoshi system=yes' #建立系統組 ansible db -m group -a 'name=canglaoshi state=absent' # 刪除組tail -l /etc/group # 查看系統組