1.使用ansible配置nginx服務python
在安裝前瞭解rpm與yum的區別 rpm是壓縮包安裝依賴包須要本身手動安裝,yum安裝解決依賴包的問題nginx
安裝pip的前提是要給全部控制機配置epel源
ansible web -m shell -a 'wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo'
[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 安裝包組web
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 copy -a 'src=/etc/yun.repos.d/epel.repo dest=/etc/yun.repos.d/epel.repo'
給所有被控制機安裝pip
# 安裝python2-pip
ansible web -m yum -a 'name=wget state=absent' # 卸載軟件包
ansible web -m yum -a 'name="@Development Tools"' # 安裝包組
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
* * * * * 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 wusir 建立系統用戶, 從999倒序
useradd -s /sbin/nologin alexsb 建立的是普通用戶,從1000開始升序
useradd -d /opt/alexsb2 alexsb2 建立用戶時指定用戶的家目錄
useradd -u 3000 alexsb6 # 建立用戶並指定用戶的uid
userdel alex 刪除用戶
userdel -r alexsb2 刪除用戶並刪除用戶的家目錄
groupadd yuchao 建立用戶組
groupdel yuchao 刪除用戶組
group 組
groups 附加組
home 家目錄
name 用戶名
password 密碼
remove ?
shell 用戶登陸後使用的shell
system 建立一個系統用戶
uid 用來指定用戶的id
state 狀態
ansible db -m user -a 'name=wulaoshi uid=4000 home=/opt/wulaoshi groups=root shell=/sbin/nologin' #建立一個用戶,並指定用戶的id,用戶的家目錄,用戶的附加組,用戶的shell
ansible db -m user -a 'name=wulaoshi state=absent' #刪除用戶可是不刪除用戶的家目錄
ansible db -m user -a 'name=wulaoshi3 state=absent remove=yes' # 刪除用戶並刪除用戶的家目錄
gid 組的id
name 組名
system 系統組
state
ansible db -m group -a 'name=wulaoshi system=yes' #建立系統組
ansible db -m group -a 'name=wulaoshi state=absent' # 刪除組
webshell
建立一個用戶組alex10編程
ansible web -m group -a 'name=alex10'flask
建立一個用戶wusir10centos
ansible web -m user -a 'name=wusir10'服務器
把/etc/fstab文件複製到遠程主機上/tmp/f併發
ansible web -m copy -a 'src=/etc/fstab dest=/tmp/f'編程語言
安裝nginx,並啓動,設置開機自啓動
ansible web -m yum -a 'name=nginx'
ansible web -m service -a 'name=nginx enabled=yes'
yaml
是一個編程語言
xml 是用來寫配置文件的一個語言
ini
yaml
字典: key: value
列表: [] -
後綴名 yaml yml
執行順序: 從上往下
特性:冪等性 無論執行多少遍,結果都是同樣的
ansible-playbook [options] playbook.yml [playbook2 ...]
-C, --check # 檢查,白跑,幹跑
-f FORKS, --forks=FORKS #用來作併發
--list-hosts # 列出主機列表
--syntax-check # 語法檢查
簡單用法(格式比較嚴格)
等號後面不能有空格
- 後面也要有空格
嚴格對齊
實例:
- hosts: web tasks: - name: creategroup group: name=alex10 - name: cretaeuser user: name=wusir10
- hosts: web
tasks:
- name: create{{ user }}
user: name={{ user}}
第一種方式
ansible-playbook -e 'user=alexsb10' p2.yml
第二種方式
[db]
192.168.107.132 user=alexsb11
192.168.107.133 user=alexsb12
第三種方式
[db:vars] #表示組的參數
user=alexsb13
第四種方式
- hosts: db
vars:
- user: alexsb14
tasks:
- name: create{{ user }}
user: name={{ user}}
第五種傳參方式
- hosts: db
tasks:
- name: sum
shell: echo 7+8|bc
register: user
- name: createuser
user: name={{user.stdout}}
傳參方式的優先級
-e > playbook vars > hosts文件
模塊
yum 安裝包
@
pip 安裝python的模塊
requirements
service
enabled=yes
corn
disabled=yes
user
system=yes
group
system=yes
劇本
yaml 格式 (格式要求比較嚴格)
傳參
-e
hosts文件 主機列表後面
hosts文件[groupname:vars]
playbook裏面寫vars
register
傳參的優先級