1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654
| #执行ansible普通模块的方式 例子mount模块: #1.ansible zznn -m mount -a "src=10.0.0.10:/data path=/opt fstype=nfs opts=defaults state=present" -i hosts #检查语法有没有错误 #1.ansible-playbook -i hosts --syntax http1.yml #2.执行 #3.ansible-playbook -i hosts http1.yml #4.playbook分为play和task 1.play记录主机清单 2.task记录任务 #一个name里面描述只能写一个模块 只能包含一个模块 #执行方式:ansible-playbook zznn -i hosts --syntax http.yaml - hosts: zznn #play(主机清单)只有一种写法- 空格 hosts: tasks: #任务是什么下面另外开启一行
#任务1安装 - name: Install Httpd Server #任务描述 yum: name=httpd state=present #yum模块安装httpd #任务2copy配置文件 - name: Configure Httpd weibsite #任务描述 copy: src=./http.conf.j2 dest=/etc/httpd/conf/httpd.conf backup=yes #copy远程配置文件 #任务3开启httpd服务 - name: Configure Httpd Server #任务描述 copy: src=./tt.j2 dest=/var/www/html/tt.html owner=root group=root mode=644 backup=yes #copy远程配置文件 #任务3开启httpd服务 - name: Service Httpd Server #任务描述 service: name=httpd state=started enabled=yes #打开httpd服务并且加入开机自启动 #任务3防火墙 - name: Service Firewall Server #任务描述 service: name=firewalld state=started enable=yes #service模块防火墙开机自启动 #任务4 - name: Cofigure Firewall server #firewalld: zone=public service=http permanent=yes immediate=yes state=enabled #firewalld: zone=public service=http permanent=yes immediate=yes state=enabled shell: firewall-cmd --permanent --zone=public --add-port=80/tcp
#经典1自动挂载 #准备环境 #通过控制端控制这两台机器 - hosts: 10.0.0.11 tasks: #任务1 - name: Install nfs Server yum: name=nfs-utils state=present - name: install rpcbind server yum: name=rpcbind state=present #- name: confogure nfs server 此种会报错原因content有空格 #copy: src=./exports dest=/etc/exports backup=yes #copy: content=/data 10.0.0.12(rw,insecure,no_root_squash,no_all_squash,sync,anonuid=666,anongid=666) dest=/etc/exports backup=yes
- name: Create nfs Group group: name=nfs1 gid=666 - name: Create nfs user user: name=nfs1 uid=666 group=666 shell=/sbin/nologin create_home=no
- name: Create file directory file: path=/data state=directory owner=nfs1 group=nfs1 mode=777 recurse=yes
- name: service nfs server service: name=nfs state=started enabled=yes
#构建挂载 ------------->NFS---GUAZAI #模块二 - hosts: 10.0.0.12 tasks:
- name: create nfs_tt directory file: path=/nfs_tt state=directory mode=777
- name: create nfs server yum: name: nfs state: present
- name: create rpcbind yum: name: rpcbind state: present - name: service nfs server service: name: nfs state: started enabled: yes
- name: client mount nfs server mount: src: 10.0.0.11:/data path: /nfs_tt fstype: nfs opts: defaults state: mounted
#利用ansible书写自动配置httpd服务实现网络访问显示 welcome ---------------888----------> #需要用到的模块1.yum(安装) 2.copy(改配置文件) 3.service(开启服务) 4.fiirewall(开放防火墙80端口) #卸载httpd 原因改了配置需要重启现在的状态不会重启 #ansible zznn -i hosts -m yum -a "name=httpd state=absent" #Ansible安装并配置httpd服务 根据不同的主机配置不同的网址信息。(使用多个play的方式 #但不是生产推荐(了解)即可,生产推荐使用循环方式。 - hosts: 10.0.0.12 tasks: - name: install httpd Server yum: name=httpd state=present enablerepo=epel
- name: change index.html copy: src=./httpd.conf dest=/etc/httpd/conf/httpd.conf owner=root group=root mode=644 backup=yes
- name: create tt group group: name=tt gid=7788 state=present
- name: create tt user user: name=tt uid=7788 group=7788 shell=/sbin/nologin create_home=no
- name: systemctl start firewall service: name=firewalld state=started
- name: systemctl start httpd service: name=httpd state=started
- name: kaifang firewall port80 firewalld: zone: public port: 80/tcp permanent: yes immediate: yes state: enabled
#不同点-----------------> - hosts: 10.0.0.11 tasks:
- name: configure welcome-------666--------> > site copy: content: "welcome-------666--------> > " dest: /var/www/html/tt.html owner: root group: root mode: 644
- hosts: 10.0.0.12 tasks: - name: configure welcome-------888--------> > site copy: content: "welcome-------888--------> > " dest: /var/www/html/tt.html owner: root group: root mode: 644
#自检测lamp-----------------> php搭建可道云 -------------> ##使用yum安装httpd,php,mariadb,firewall,php.mysql等 ##添加防火墙规则放行httpd流量,并永久生效 ##先不考虑配置文件的管理 ##启动httpd,firewalld 服务 - hosts: 10.0.0.12 tasks:
- name: install httpd server yum: name: httpd,php,php-pdo,php-mbstring,php-gd,firewalld state: present
- name: cofigure file server file: path: /home/gegewu/php state: directory owner: gegewu group: gegewu mode: 644
- name: cofigure firewall port80 firewalld: zone: public permanent: yes immediate: yes port: 80/tcp state: enabled
- name: service start httpd service: name=httpd state=started
- name: configure game.html copy: src=./game.html dest=/var/www/html/game.html owner=root group=root mode=777 backup=yes
- name: copy: src=./index.php dest=/var/www/html/index.php owner=root group=root mode=777 backup=yes
- name: file mode+777 file: path=/var/www/html owner=gegewu group=gegewu mode=777 recurse=yes
- name: get_url: url: http://fj.xuliangwei.com/public/ip.txt #http://fj.xuliangwei.com/public/index.php dest: /var/www/html/ip.txt backup: yes
- name: file: path=/var/www/html owner=apache group=apache mode=777 recurse=yes
- name: copy kod cloud code unarchive: src=./kodexplorer4.47.zip dest=/var/www/html/ mode=0777
- name: systemctl restart httpd service: name: httpd state: restarted #- name: copy kod html5.zip code #unarchive: src=./html5.zip dest=/var/www/html/ mode=0777
#yml变量学习----------------------------> #在脚本里面定义变量下发直接调用
- hosts: 10.0.0.12 vars: #定义变量vars关键字 web_packages: httpd ftp_packages: vsftpd
tasks: #task(干什么)写在vars下面 #使用变量{{ 变量名 }} - name: install rpm packages yum: name={{ web_packages }} state=present
- name: install rpm packages yum: name={{ ftp_pack }} state=present #-------------------------------------------------->/或者 #在脚本里面定义变量下发直接调用
- hosts: 10.0.0.12 vars: #定义变量vars关键字 - web_packages: httpd-2.4.6 - ftp_packages: vsftpd-3.0.2
tasks: #task(干什么)写在vars下面 #使用变量{{ 变量名 }} - name: install {{ web_packages }} {{ ftp_packages }} yum: name: - "{{ web_packages }}" - "{{ ftp_packages }}" state: present
#-------------------------------------------------------------->
#调用变量文件 : vars_files: ./vars_public.yml #将变量定义在一个文件内通过:“vars_files:+文件名调用” -----> tasks - hosts: 10.0.0.12 vars_files: ./vars_public.yml
tasks: #task(干什么)写在vars下面 #使用变量{{ 变量名 }} - name: install {{ web_packages }} {{ ftp_packages }} yum: name: - "{{ web_packages }}" - "{{ ftp_packages }}" state: present
#检查语法有没有错误 #1.ansible-playbook -i hosts --syntax http1.yml #2.执行 #3.ansible-playbook -i hosts http1.yml #定义公用变量文件:vars_public.yml web_packages: httpd ftp_packages: vsftpd
#ansible变量注册(register) - hosts: zznn tasks: - name: install httpd server yum: name=httpd state=present
- name: service start httpd service: name=httpd state=started
- name: check httpd status shell: ps -ef |grep httpd register: check_httpd
- name: status Httpd debug: msg: "{{ check_httpd.stdout_lines }}"
#facts实例采集ip地址信息 - hosts: zznn tasks: - name: output ansible facts debug: msg: ip address "{{ ansible_fqdn }}" is "{{ ansible_default_ipv4.address }}"
#ansible利用facts批量管理配置文件自动下发符合主机的相关配置 #例子:自动匹配目标主机的ip地址自动填入配置文件:1.txt里 #步骤:1. 书写playbook 2. 将需要更改的配置文件处写为facts里面的对应变量 - hosts: zznn tasks:
- name: copy 1.txt template: src: ./1.txt dest: /home/gegewu # 2. 将需要更改的配置文件处写为facts里面的对应变量 #hostname={{ ansible_fqdn }}
#例2:使用facts变量根据不同的内存生成不同的memcached配置文件 #配置文件目录:/etc/sysconfig/memcached #[root@localhost project1]# cat /etc/sysconfig/memcached #PORT="11211" #此服务监听的端口 #USER="memcached" #服务启动的用户 #MAXCONN="1024" #最大连接数 #CACHESIZE="64" #使用内存的大小 #OPTIONS=""
#. 启动服务:systemctl start memcached
#facts批量管理配置文件yml文件 #运用变量的计算CACHESIZE="64"使用内存的大小 {{ }} # cp -a /etc/sysconfig/memcached ./memcached.j2 #facts对应变量名:ansible_memtotal_mb 要一半内存所以为:{{ ansible_memtotal_mb //2 }} - hosts: zznn vars: check_mem: memcached MAXCONN: 2048 tasks: - name: yum yum: name: "{{ check_mem }}" state: present - name: template=copy
template: src: ./memcached.j2 dest: /etc/sysconfig/memcached
- name: service service: name: "{{ check_mem }}" state: restarted
- name: shell shell: ps -ef |grep "{{ check_mem }}" register: check_ps
- name: debug debug: msg: "{{ check_ps.stdout_lines }}"
#注:在写任何新的服务之前,请先手动测试一遍,提取安装的命令\配置文件路径
#作业: #--------------使用变量的方式------------------> #1. nginx+php完成kod搭建 #2. 通过facts变量中的cpu核心数渲染出不同主机的nginx配置文件(worker_processes) #3. 完成渲染zabbix的配置文件 #4. 完成渲染memcached的配置文件a. 端口\最大连接数设定成变量 - hosts: zznn tasks:
- name: shell shell: echo $RANDOM |md5sum |cut -c 5-12 register: get_random
- name: debug debug: msg: "{{ get_random.stdout_lines }}"
- name: hostname hostname: name: "{{ get_random.stdout_lines[0] }}" # "{{ get_random.stdout }}" #注意[0]:表示取第一行0代表第一个 #方法2 用facts批量修改主机名称用: facts变量改为ip地址--->利用shell模块取尾数 - hosts: zznn vars: check_mem: 利用fascts批量修改主机名称 tasks:
- name: hostname hostname: name: "web_{{ ansible_default_ipv4.address }}"
- name: dubug debug: msg: "{{ check_mem }}" - name: debug2 debug: msg: "{{ ansible_default_ipv4.address.split('.')[-1] }}" #1. - name: 两个随机数facts变量组合 #hostname: name={{ A }}_{{ B }} #2. 获取ip地址切片的方式 #hostname: name={{ ansible_default_ipv4.address.split('.')[-1] }} #nginx+php # 1.卸载php低版本 # 2.安装nginx1.12----------->epel # 3.安装php5.4 ------------->base # 4.创建组用户www # 5.配置nginx ---->nginx.conf 指定运行的用户身份www\ # 6.配置nginx.conf.d/kod.conf 虚拟主机 # a. 根据虚拟主机的配置创建存放代码的目录 # b. 拷贝可到云代码 解压 # c. 授权目录权限 # 7.配置php-fpm 管理php的用户身份 # 8.配置php程序,php.ini 调整可到云上传的大小 # 9.测试nginx配置文件语法/配置文件是否正确 # 10.测试php配置文件语法/配置文件是否正确 ---->php-fpm -t - hosts: 10.0.0.11 vars: install_nginx: nginx
tasks: - name: debug debug: msg={{ install_nginx }}
- name: yum yum: name={{ install_nginx }},php,php-pdo,php-gd,php-mbstring,php-fpm state=present
- name: group user: name=www gid=666 state=present
- name: user user: name=www uid=666 gid=666 shell=/sbin/nologin create_home=no - name: copy nginx.conf copy: src=./nginx.j2 dest=/etc/nginx/nginx.conf bakup=yes
- name: copy nginx.conf copy: src=./kod.conf.j2 dest=/etc/nginx/conf.d/kod.conf bakup=yes
- name: create file file: path=ansible_code state=directory
- name: unzip kod date directory unarchive: src=./backup/kodexplorer4.40.zip dest=/ansible_code
- name: chmod kod date www file: path=/ansible_code owner=www group=www recurse=yes mode=0777 - name: cofigure php-FPM server php进程启动个数及每个进程能够接受的链接和请求 copy: src=./php_www.conf.j2 dest=/etc/php-fpm.d/www.cnf
- name: cofigur e php server调整可到云上传大小 copy: src=./php.ini.j2 dest=/etc/php.ini - name: service nginx Server service: name=nginx state=started enable=yes
- name: service nginx Server service: name=php-fpm state=started enable=yes #-------------------------------------------------->
- hosts: 10.0.0.12 vars: #定义变量vars关键字 - install_1: /home/gegewu/prometheus.sh - install_2: /home/gegewu/telegraf.sh - install_3: /home/gegewu/alertmanager.sh - tg_alert: /home/gegewu/alertmanager.serivce - tg_n9eserver: /home/gegewu/n9eserver.service - tg_n9eweb: /home/gegewu/n9eweb.service - linux_dir: /opt
tasks:
- name: copy prometheus #unarchive: src=./prometheus.zip dest=/opt mode=0777 copy: src="{{ install_1 }}" dest="{{ linux_dir }}" mode=0777
- name: copy telegraf #unarchive: src=./telegraf.sh dest=/opt mode=0777 copy: src="{{ install_2 }}" dest="{{ linux_dir }}" mode=0777
- name: copy n9e_5.6 unarchive: src=./n9e.5.6.zip dest="{{ linux_dir }}" mode=0777
- name: copy kod alertmanger #unarchive: src=./alertmanager.zip dest=/opt mode=0777 copy: src="{{ install_3 }}" dest="{{ linux_dir }}" mode=0777
- name: install mysql shell: apt-get install -y mysql-server-5.7
- name: daoru sql shell: mysql -uroot -p1234 < /opt/n9e-5.6/docker/initsql/a-n9e.sql
- name: restart mysql service: systemctl start mysql - name: systemctl restart mysql service: name: mysql state: restarted
- name: systemctl restart prometheus service: name=prometheus state=restarted
- name: systemctl restart telegraf service: name=telegraf state=restarted
- name: systemctl restart alertmanager service: name=alertmanager state=restarted
- name: start n9eserver shell: nohup ./n9e server &> server.log &
- name: start n9eweb shell: nohup ./n9e webapi &> webapi.log &
- name: firewall prometheus shell: ufw allow 8091
- name: firewall mysql shell: ufw allow 3306
- name: firewall n9eserver shell: ufw allow 19000
- name: firewall n9eweb shell: ufw allow 18000
- name: firewall alertmanager shell: ufw allow 9093
- name: shell shell: echo "构建完成访问:"http://localhost:8091"" register: dh - name: debug debug: msg="{{ web_ip }}"
#ansible 搭建smartping #1. 将文件传输到远端机器 #2. 文件为:smartping-v0.8.0.tar.gz anaconda-ks.cfg - hosts: 10.0.0.128 vars: web_server: httpd linux_dir: /home/gegewu/smartping
tasks: - name: file file: path="{{ linux_dir }}" state=directory owner=gegewu group=gegewu mode=777 recurse=yes - name: unarchive unarchive: src=./smartping-v0.8.0.zip dest="{{ linux_dir }}" mode=0777
- name: copy copy: src=./anaconda-ks.cfg dest="{{ linux_dir }}" mode=0777
- name: 8899 firewalld: zone: public permanent: yes immediate: yes port: 8899/tcp state: enabled - name: start shell: /"{{ linux_dir }}/control start"
- name: service service: name=firewalld state=started enabled=yes
- name: web shell: echo "浏览器访问本机ip:8899" register: web_ip - name: debug debug: msg: "{{ web_ip }}"
#搭建--------------------------------code #ansible搭建kcloud ##使用yum安装httpd,php,mariadb,firewall,php.mysql等 ##添加防火墙规则放行httpd流量,并永久生效 ##先不考虑配置文件的管理 ##启动httpd,firewalld 服务 - hosts: 10.0.0.128 tasks:
- name: install httpd server yum: name: httpd,php,php-pdo,php-mbstring,php-gd,firewalld state: present
- name: cofigure file server file: path: /home/gegewu/php state: directory owner: gegewu group: gegewu mode: 644
- name: cofigure firewall port80 firewalld: zone: public permanent: yes immediate: yes port: 80/tcp state: enabled
- name: service start httpd service: name=httpd state=started
#- name: configure game.html #copy: src=./game.html dest=/var/www/html/game.html owner=root group=root mode=777 backup=yes
- name: copy: src=./index.php dest=/var/www/html/index.php owner=root group=root mode=777 backup=yes
- name: file mode+777 file: path=/var/www/html owner=gegewu group=gegewu mode=777 recurse=yes
- name: get_url: url: http://fj.xuliangwei.com/public/ip.txt #http://fj.xuliangwei.com/public/index.php dest: /var/www/html/ip.txt backup: yes
- name: file file: path=/var/www/html owner=apache group=apache mode=777 recurse=yes
- name: unarche unarchive: src=./kodexplorer4.47.zip dest=/var/www/html/ mode=0777
- name: systemctl restart httpd service: name: httpd state: restarted
ansible 10.0.0.12 -m mount -a "src=10.0.0.11:/data path=/nfs_tt fstype=nfs opts=defaults state=mounted " -i hosts
|