實驗環境:CentOS7php
ansible:是自動化運維工具,基於Python開發,實現批量系統設置、批量程序部署、批量執行命令等功能。其中,批量部署是立身於ansible的模塊進行工做的。html
首先給全部須要批量部署的設備安裝ansible:node
[root@~ localhost]#yum -y install ansible #在管理機/堡壘機上配置以下: [root@ansible localhost]#vim hosts ## db-[99:101]-node.example.com [web] #列表名 172.16.252.143 #堡壘機(本機) 172.16.254.47 172.16.253.177 172.16.252.182 #添加認證: [root@ansible localhost]#ssh-keygen -t rsa #無密碼的RSA加密方式 [root@ansible localhost]#ls /root/.ssh id_rsa id_rsa.pub known_hosts #其中,id_rsa是私鑰,id_rsa.pub是公鑰,將公鑰安全的發送到其餘主機,包括本身本機
[root@.ssh localhost]#ssh-copy-id root@172.16.253.177python
[root@.ssh localhost]#ls
authorized_keys known_hostsnginx
#authorized_keys是發過來的公鑰git
[root@ansible localhost]#vim ansible.cfg host_key_checking = False #啓用此項,不進行主機key檢查,防止出現以下信息 The authenticity of host '172.16.253.177 (172.16.253.177)' can't be
established. #使用幫助 [root@ansible localhost]#ansible -h
#web也能夠改成管理機的ip,是模式:-m是module模塊 [root@.ssh localhost]#ansible web -m ping 172.16.254.47 | SUCCESS => { "changed": false, "ping": "pong" } 172.16.252.182 | SUCCESS => { "changed": false, "ping": "pong" } 172.16.253.177 | SUCCESS => { "changed": false, "ping": "pong" } 172.16.252.143 | SUCCESS => { "changed": false, "ping": "pong" }
#列出列表
[root@.ssh localhost]#ansible web --list
hosts (4):
172.16.252.143
172.16.254.47
172.16.253.177
172.16.252.182github
#各類模塊的使用幫助,以下是command的模塊 [root@.ssh localhost]#ansible-doc command
#setup模塊:查看系統軟硬件信息;web
#只查看本發佈機的CPU核數: [root@ansible master-80]#ansible 172.16.252.143 -m setup |grep vcpus "ansible_processor_vcpus": 1, #其中,ansible_processor_vcpus是變量
#command模塊 #不支持`$ `"<"', `">"', `"|"',`";"' and `"&"';有這些符號需用shell模塊 [root@.ssh localhost]#cat /etc/passwd |grep tom 語法: ansible <host-pattern> [-m module_name] [-a args] [options] #批量添加tom用戶 [root@.ssh localhost]#ansible web -m command -a 'useradd tom' 172.16.253.177 | SUCCESS | rc=0 >> 172.16.254.47 | SUCCESS | rc=0 >> 172.16.252.143 | SUCCESS | rc=0 >> 172.16.252.182 | SUCCESS | rc=0 >> [root@.ssh localhost]#cat /etc/passwd |grep tom tom:x:1001:1001::/home/tom:/bin/bash #shell模塊: [root@.ssh localhost]#ansible web -m shell -a 'echo "123456" |passwd --stdin tom' 172.16.253.177 | SUCCESS | rc=0 >> Changing password for user tom. passwd: all authentication tokens updated successfully. 172.16.254.47 | SUCCESS | rc=0 >> Changing password for user tom. passwd: all authentication tokens updated successfully. 172.16.252.182 | SUCCESS | rc=0 >> Changing password for user tom. passwd: all authentication tokens updated successfully. 172.16.252.143 | SUCCESS | rc=0 >> Changing password for user tom. passwd: all authentication tokens updated successfully. #sudo相關的配置,先必須在其餘主機上都給予sudo的權限,即按以下配置: [root@.ssh localhost]#visudo ## Same thing without a password #啓用此項配置 %wheel ALL=(ALL) NOPASSWD: ALL #把tom添加到附加的wheel組 [root@.ssh localhost]#usermod -G wheel tom [root@.ssh localhost]#ansible web -m ping -u tom -b --become-user root
#copy模塊:redis
[root@.ssh localhost]#ansible web -m copy -a 'src=/etc/fstab dest=/tmp/ backup=yes mode=0644 owner=tom group=tom' #src:源文件所在路徑; #owner文件的屬主;group文件的屬組,屬主屬組必須存在 #dest:目的地路徑;mode文件的權限,backup是否備份 172.16.254.47 | SUCCESS => { "changed": true, "checksum": "e1c1246c3358f74ffb2090c9b4c9172974306084", "dest": "/tmp/fstab", "gid": 1001, "group": "tom", "md5sum": "f3e046a46def8356721f867aedfd97ad", "mode": "0644", "owner": "tom", "secontext": "unconfined_u:object_r:admin_home_t:s0", "size": 595, "src": "/root/.ansible/tmp/ansible-tmp-1497749953.26-144906199640828/source", "state": "file", "uid": 1001 } 172.16.253.177 | SUCCESS => { "changed": true, "checksum": "e1c1246c3358f74ffb2090c9b4c9172974306084", "dest": "/tmp/fstab", "gid": 1001, "group": "tom", "md5sum": "f3e046a46def8356721f867aedfd97ad", "mode": "0644", "owner": "tom", "secontext": "unconfined_u:object_r:admin_home_t:s0", "size": 595, "src": "/root/.ansible/tmp/ansible-tmp-1497749953.1-86517078793254/source", "state": "file", "uid": 1001 } 172.16.252.182 | SUCCESS => { "changed": true, "checksum": "e1c1246c3358f74ffb2090c9b4c9172974306084", "dest": "/tmp/fstab", "gid": 1001, "group": "tom", "md5sum": "f3e046a46def8356721f867aedfd97ad", "mode": "0644", "owner": "tom", "secontext": "unconfined_u:object_r:admin_home_t:s0", "size": 595, "src": "/root/.ansible/tmp/ansible-tmp-1497749953.52-203914743845832/source", "state": "file", "uid": 1001 } 172.16.252.143 | SUCCESS => { "changed": true, "checksum": "e1c1246c3358f74ffb2090c9b4c9172974306084", "dest": "/tmp/fstab", "gid": 1001, "group": "tom", "md5sum": "f3e046a46def8356721f867aedfd97ad", "mode": "0644", "owner": "tom", "size": 595, "src": "/root/.ansible/tmp/ansible-tmp-1497749952.25-164901742964830/source", "state": "file", "uid": 1001 } [root@ansible localhost]#ll /tmp/ -rw-r--r-- 1 tom tom 595 Jun 18 09:39 fstab
#fetch模塊:sql
[root@.ssh localhost]#ansible web -m fetch -a 'src=/etc/fstab dest=/tmp/' 172.16.253.177 | SUCCESS => { "changed": true, "checksum": "329f9159f37f8b2ab90d60e9da9025a7f1bb4a7c", "dest": "/tmp/172.16.253.177/etc/fstab", "md5sum": "6fc34eb32e5312f4b7631570eeb25fae", "remote_checksum": "329f9159f37f8b2ab90d60e9da9025a7f1bb4a7c", "remote_md5sum": null } 172.16.252.182 | SUCCESS => { "changed": true, "checksum": "055d967377645aee6c1956fe7109c293aa961251", "dest": "/tmp/172.16.252.182/etc/fstab", "md5sum": "5b681acdbe19e886c1532b97d79d7acb", "remote_checksum": "055d967377645aee6c1956fe7109c293aa961251", "remote_md5sum": null } 172.16.254.47 | SUCCESS => { "changed": true, "checksum": "167915186001b6ae17b5878f7ec50162635835d1", "dest": "/tmp/172.16.254.47/etc/fstab", "md5sum": "98caa9822182c0662a2153e066bb9542", "remote_checksum": "167915186001b6ae17b5878f7ec50162635835d1", "remote_md5sum": null } 172.16.252.143 | SUCCESS => { "changed": true, "checksum": "e1c1246c3358f74ffb2090c9b4c9172974306084", "dest": "/tmp/172.16.252.143/etc/fstab", "md5sum": "f3e046a46def8356721f867aedfd97ad", "remote_checksum": "e1c1246c3358f74ffb2090c9b4c9172974306084", "remote_md5sum": null } [root@.ssh localhost]#ls /tmp/ 172.16.252.143 172.16.252.182 172.16.253.177 172.16.254.47
#多個文件的獲取,在/tmp下新建同名的test文件 #先打包,再獲取 [root@~ localhost]#ansible web -m shell -a 'tar -cf fstab.gz /tmp/fstab /tmp/test' 172.16.252.143 | SUCCESS | rc=0 >> tar: Removing leading `/' from member names 172.16.254.47 | SUCCESS | rc=0 >> tar: Removing leading `/' from member names 172.16.253.177 | SUCCESS | rc=0 >> tar: Removing leading `/' from member names 172.16.252.182 | SUCCESS | rc=0 >> tar: Removing leading `/' from member names [root@~ localhost]#ls anaconda-ks.cfg fstab.gz hellodb.sql [root@~ localhost]#ansible web -m fetch -a 'src=/root/fstab.gz dest=/tmp/' 172.16.252.143 | SUCCESS => { "changed": true, "checksum": "e6fb8cdf9ef285c5955dca6f698c57ab58708c6b", "dest": "/tmp/172.16.252.143/root/fstab.gz", "md5sum": "2891f8e9b46aaedde7fe40deeb845085", "remote_checksum": "e6fb8cdf9ef285c5955dca6f698c57ab58708c6b", "remote_md5sum": null } 172.16.254.47 | SUCCESS => { "changed": true, "checksum": "166689817e5fbc45a9cad526c5fb36f4ee44550b", "dest": "/tmp/172.16.254.47/root/fstab.gz", "md5sum": "8effda0c3008f5ffec7b819c1a690c53", "remote_checksum": "166689817e5fbc45a9cad526c5fb36f4ee44550b", "remote_md5sum": null } 172.16.252.182 | SUCCESS => { "changed": true, "checksum": "a138abc36510beb3c8d12ced6004da666f3245c8", "dest": "/tmp/172.16.252.182/root/fstab.gz", "md5sum": "f7fc951a1631e50fe223f8edf08c5c89", "remote_checksum": "a138abc36510beb3c8d12ced6004da666f3245c8", "remote_md5sum": null } 172.16.253.177 | SUCCESS => { "changed": true, "checksum": "bb056a73f27d78daf06887b14184917e5c4cb9c7", "dest": "/tmp/172.16.253.177/root/fstab.gz", "md5sum": "6793b0ccba71b4c75a0d254f1898f5c3", "remote_checksum": "bb056a73f27d78daf06887b14184917e5c4cb9c7", "remote_md5sum": null } [root@~ localhost]#cd /tmp/172.16.252.143 [root@172.16.252.143 localhost]#ls etc root [root@172.16.252.143 localhost]#cd root [root@root localhost]#ls fstab.gz
#file模塊:
#批量添加軟鏈接 [root@root localhost]#ansible web -m file -a 'src=/tmp/fstab dest=/tmp/fstab.link state=link' 172.16.252.143 | SUCCESS => { "changed": true, "dest": "/tmp/fstab.link", "gid": 0, "group": "root", "mode": "0777", "owner": "root", "size": 10, "src": "/tmp/fstab", "state": "link", "uid": 0 } 172.16.253.177 | SUCCESS => { "changed": true, "dest": "/tmp/fstab.link", "gid": 0, "group": "root", "mode": "0777", "owner": "root", "secontext": "unconfined_u:object_r:user_tmp_t:s0", "size": 10, "src": "/tmp/fstab", "state": "link", "uid": 0 } 172.16.254.47 | SUCCESS => { "changed": true, "dest": "/tmp/fstab.link", "gid": 0, "group": "root", "mode": "0777", "owner": "root", "secontext": "unconfined_u:object_r:user_tmp_t:s0", "size": 10, "src": "/tmp/fstab", "state": "link", "uid": 0 } 172.16.252.182 | SUCCESS => { "changed": true, "dest": "/tmp/fstab.link", "gid": 0, "group": "root", "mode": "0777", "owner": "root", "secontext": "unconfined_u:object_r:user_tmp_t:s0", "size": 10, "src": "/tmp/fstab", "state": "link", "uid": 0 } [root@~ localhost]#ll /tmp -rw-r--r--. 1 tom tom 595 Jul 6 21:52 fstab lrwxrwxrwx. 1 root root 10 Jul 6 22:20 fstab.link -> /tmp/fstab #批量建立目錄:state是directory [root@root localhost]#ansible web -m file -a 'path=/tmp/tomtest/ state=directory mode=0755' 172.16.252.143 | SUCCESS => { "changed": true, "gid": 0, "group": "root", "mode": "0755", "owner": "root", "path": "/tmp/tomtest/", "size": 4096, "state": "directory", "uid": 0 } 172.16.254.47 | SUCCESS => { "changed": true, "gid": 0, "group": "root", "mode": "0755", "owner": "root", "path": "/tmp/tomtest/", "secontext": "unconfined_u:object_r:user_tmp_t:s0", "size": 6, "state": "directory", "uid": 0 } 172.16.253.177 | SUCCESS => { "changed": true, "gid": 0, "group": "root", "mode": "0755", "owner": "root", "path": "/tmp/tomtest/", "secontext": "unconfined_u:object_r:user_tmp_t:s0", "size": 6, "state": "directory", "uid": 0 } 172.16.252.182 | SUCCESS => { "changed": true, "gid": 0, "group": "root", "mode": "0755", "owner": "root", "path": "/tmp/tomtest/", "secontext": "unconfined_u:object_r:user_tmp_t:s0", "size": 6, "state": "directory", "uid": 0 } [root@~ localhost]#ll /tmp drwxr-xr-x. 2 root root 6 Jul 6 22:26 tomtest #批量建立文件: state=touch [root@root localhost]#ansible web -m file -a 'path=/tmp/ansible mode=0644 state=touch' 172.16.252.143 | SUCCESS => { "changed": true, "dest": "/tmp/ansible", "gid": 0, "group": "root", "mode": "0644", "owner": "root", "size": 0, "state": "file", "uid": 0 } 172.16.254.47 | SUCCESS => { "changed": true, "dest": "/tmp/ansible", "gid": 0, "group": "root", "mode": "0644", "owner": "root", "secontext": "unconfined_u:object_r:user_tmp_t:s0", "size": 0, "state": "file", "uid": 0 } 172.16.252.182 | SUCCESS => { "changed": true, "dest": "/tmp/ansible", "gid": 0, "group": "root", "mode": "0644", "owner": "root", "secontext": "unconfined_u:object_r:user_tmp_t:s0", "size": 0, "state": "file", "uid": 0 } 172.16.253.177 | SUCCESS => { "changed": true, "dest": "/tmp/ansible", "gid": 0, "group": "root", "mode": "0644", "owner": "root", "secontext": "unconfined_u:object_r:user_tmp_t:s0", "size": 0, "state": "file", "uid": 0 } [root@~ localhost]#ll /tmp total 8 -rw-r--r--. 1 root root 0 Jul 6 22:30 ansible
#group模塊:
#添加是present,刪除是absent - state Whether the group should be present or not on the remote hos (Choices: present, absent)[Default: present] [root@root localhost]#ansible web -m group -a 'name=ansi gid=1200 state=present' 172.16.252.143 | SUCCESS => { "changed": true, "gid": 1200, "name": "ansi", "state": "present", "system": false } 172.16.254.47 | SUCCESS => { "changed": true, "gid": 1200, "name": "ansi", "state": "present", "system": false } 172.16.253.177 | SUCCESS => { "changed": true, "gid": 1200, "name": "ansi", "state": "present", "system": false } 172.16.252.182 | SUCCESS => { "changed": true, "gid": 1200, "name": "ansi", "state": "present", "system": false } [root@~ localhost]#cat /etc/group|grep ansi ansi:x:1200: #刪除組; [root@root localhost]#ansible web -m group -a 'name=ansi state=absent' 172.16.252.143 | SUCCESS => { "changed": true, "name": "ansi", "state": "absent" } 172.16.253.177 | SUCCESS => { "changed": true, "name": "ansi", "state": "absent" } 172.16.254.47 | SUCCESS => { "changed": true, "name": "ansi", "state": "absent" } 172.16.252.182 | SUCCESS => { "changed": true, "name": "ansi", "state": "absent" } [root@~ localhost]#cat /etc/group|grep ansi
#hostname模塊:
#定義變量hostname,批量修改hostname; [root@ansible localhost]#cat hosts [web] 172.16.252.143 hostname=master 172.16.254.47 hostname=host1 172.16.253.177 hostname=host2 172.16.252.182 hostname=host3 #hostname變量兩邊有空格 [root@ansible localhost]#ansible web -m hostname -a 'name={{ hostname }}' 172.16.254.47 | SUCCESS => { "ansible_facts": { "ansible_domain": "", "ansible_fqdn": "host1", "ansible_hostname": "host1", "ansible_nodename": "host1" }, "changed": true, "name": "host1" } 172.16.253.177 | SUCCESS => { "ansible_facts": { "ansible_domain": "", "ansible_fqdn": "host2", "ansible_hostname": "host2", "ansible_nodename": "host2" }, "changed": true, "name": "host2" } 172.16.252.143 | SUCCESS => { "ansible_facts": { "ansible_domain": "", "ansible_fqdn": "master", "ansible_hostname": "master", "ansible_nodename": "master" }, "changed": true, "name": "master" } 172.16.252.182 | SUCCESS => { "ansible_facts": { "ansible_domain": "", "ansible_fqdn": "host3", "ansible_hostname": "host3", "ansible_nodename": "host3" }, "changed": true, "name": "host3" } #web組下添加變量,變量名必須是vars;組下的變量的優先級低於web組 [root@ansible localhost]#tail hosts ## db-[99:101]-node.example.com [web:vars] http_port=808 [web] 172.16.252.143 hostname=master http_port=80 172.16.254.47 hostname=host1 http_port=808 172.16.253.177 hostname=host2 http_port=8080 172.16.252.182 hostname=host3 http_port=8088 [root@ansible localhost]#ansible web -m hostname -a 'name={{ hostname }}-{{ http_port }}' 172.16.252.182 | SUCCESS => { "ansible_facts": { "ansible_domain": "", "ansible_fqdn": "host3-8088", "ansible_hostname": "host3-8088", "ansible_nodename": "host3-8088" }, "changed": true, "name": "host3-8088" } 172.16.253.177 | SUCCESS => { "ansible_facts": { "ansible_domain": "", "ansible_fqdn": "host2-8080", "ansible_hostname": "host2-8080", "ansible_nodename": "host2-8080" }, "changed": true, "name": "host2-8080" } 172.16.254.47 | SUCCESS => { "ansible_facts": { "ansible_domain": "", "ansible_fqdn": "host1-808", "ansible_hostname": "host1-808", "ansible_nodename": "host1-808" }, "changed": true, "name": "host1-808" } 172.16.252.143 | SUCCESS => { "ansible_facts": { "ansible_domain": "", "ansible_fqdn": "master-80", "ansible_hostname": "master-80", "ansible_nodename": "master-80" }, "changed": true, "name": "master-80" } [root@~ localhost]#hostname host3 [root@~ localhost]#hostname host3-8088 [root@ansible localhost]#hostname master-80 [root@tmp localhost]#hostname host1-808 [root@tmp localhost]#hostname host2-8080
#yum模塊
#批量安裝Nginx [root@ansible localhost]#ansible web -m yum -a 'name=nginx state=latest' [root@ansible localhost]#ansible web -m shell -a 'rpm -q nginx' 172.16.252.182 | SUCCESS | rc=0 >> nginx-1.10.2-1.el7.x86_64 172.16.252.143 | SUCCESS | rc=0 >> nginx-1.10.2-1.el7.x86_64 172.16.253.177 | SUCCESS | rc=0 >> nginx-1.10.2-1.el7.x86_64 172.16.254.47 | SUCCESS | rc=0 >> nginx-1.10.2-1.el7.x86_64
#service模塊:
#批量啓動服務; [root@ansible localhost]#ansible web -m service -a 'name=nginx state=started' #檢查是否啓動 [root@ansible localhost]#ansible web -m shell -a 'ps aux|grep nginx'172.16.253.177 | SUCCESS | rc=0 >> root 29223 0.0 0.2 122280 2072 ? Ss 23:25 0:00 nginx: master process /usr/sbin/nginx nginx 29224 0.0 0.3 122712 3104 ? S 23:25 0:00 nginx: worker process root 29266 0.0 0.1 113120 1192 pts/0 S+ 23:26 0:00 /bin/sh -c ps aux|grep nginx root 29268 0.0 0.0 112648 944 pts/0 S+ 23:26 0:00 grep nginx 172.16.254.47 | SUCCESS | rc=0 >> root 27782 0.0 0.2 122280 2072 ? Ss 23:25 0:00 nginx: master process /usr/sbin/nginx nginx 27783 0.0 0.3 122712 3104 ? S 23:25 0:00 nginx: worker process root 27826 0.0 0.1 113120 1196 pts/0 S+ 23:26 0:00 /bin/sh -c ps aux|grep nginx root 27828 0.0 0.0 112648 944 pts/0 S+ 23:26 0:00 grep nginx 172.16.252.182 | SUCCESS | rc=0 >> root 30744 0.0 0.2 122280 2068 ? Ss 23:25 0:00 nginx: master process /usr/sbin/nginx nginx 30745 0.0 0.3 122712 3100 ? S 23:25 0:00 nginx: worker process root 30787 0.0 0.1 113120 1196 pts/0 S+ 23:26 0:00 /bin/sh -c ps aux|grep nginx root 30789 0.0 0.0 112648 944 pts/0 S+ 23:26 0:00 grep nginx 172.16.252.143 | SUCCESS | rc=0 >> root 31512 0.0 0.2 122232 2068 ? Ss 11:12 0:00 nginx: master process /usr/sbin/nginx nginx 31513 0.0 0.3 122660 3092 ? S 11:12 0:00 nginx: worker process root 31526 54.0 2.5 341608 26008 pts/3 Rl+ 11:13 0:02 /usr/bin/python2 /usr/bin/ansible web -m shell -a ps aux|grep nginx root 31534 20.0 2.8 348404 28384 pts/3 S+ 11:13 0:00 /usr/bin/python2 /usr/bin/ansible web -m shell -a ps aux|grep nginx root 31610 0.0 0.1 113120 1200 pts/6 S+ 11:13 0:00 /bin/sh -c ps aux|grep nginx root 31612 0.0 0.0 112648 944 pts/6 R+ 11:13 0:00 grep nginx #中止服務: [root@ansible localhost]#ansible web -m service -a 'name=nginx state=stopped'
#user模塊:
#批量添加新用戶 [root@ansible localhost]#ansible web -m user -a 'name=wang comment=wang uid=1100 group=wang' [root@~ localhost]#id wang uid=1100(wang) gid=1000(wang) groups=1000(wang)
#script模塊:
#一個腳本在多臺主機一次性執行: [root@ansible localhost]#cat /tmp/echo.sh #!/bin/bash touch /tmp/a.txt [root@ansible localhost]#vim /tmp/echo.sh [root@ansible localhost]#ansible web -m script -a '/tmp/echo.sh' 172.16.252.143 | SUCCESS => { "changed": true, "rc": 0, "stderr": "Shared connection to 172.16.252.143 closed.\r\n", "stdout": "", "stdout_lines": [] } 172.16.253.177 | SUCCESS => { "changed": true, "rc": 0, "stderr": "Shared connection to 172.16.253.177 closed.\r\n", "stdout": "", "stdout_lines": [] } 172.16.252.182 | SUCCESS => { "changed": true, "rc": 0, "stderr": "Shared connection to 172.16.252.182 closed.\r\n", "stdout": "", "stdout_lines": [] } 172.16.254.47 | SUCCESS => { "changed": true, "rc": 0, "stderr": "Shared connection to 172.16.254.47 closed.\r\n", "stdout": "", "stdout_lines": [] } [root@~ localhost]#ls /tmp ansible a.txt
#cron模塊:
#計劃任務模塊,每週六1點執行 [root@ansible localhost]#ansible web -m cron -a 'name=daycron minute="0" hour="1" weekday="5" job="ls /tmp >>/tmp/a.log"' 172.16.252.143 | SUCCESS => { "changed": true, "envs": [], "jobs": [ "daycron" ] } 172.16.254.47 | SUCCESS => { "changed": true, "envs": [], "jobs": [ "daycron" ] } 172.16.253.177 | SUCCESS => { "changed": true, "envs": [], "jobs": [ "daycron" ] } 172.16.252.182 | SUCCESS => { "changed": true, "envs": [], "jobs": [ "daycron" ] }
[root@ansible localhost]#crontab -l
#Ansible: daycron
0 1 * * 5 ls /tmp >>/tmp/a.log
#ansible-console:控制檯式的批量交互執行
[root@ansible localhost]#ansible-console Welcome to the ansible console. Type help or ? to list commands. root@all (4)[f:5]$ pwd 172.16.253.177 | SUCCESS | rc=0 >> /root 172.16.252.182 | SUCCESS | rc=0 >> /root 172.16.254.47 | SUCCESS | rc=0 >> /root 172.16.252.143 | SUCCESS | rc=0 >> /root
root@all (4)[f:5]$ list
172.16.252.143
172.16.254.47
172.16.253.177
172.16.252.182
#ansible-galaxy:install 安裝模塊
官網: https://galaxy.ansible.com
[root@ansible localhost]#ansible-galaxy install DavidWittman.redis - downloading role 'redis', owned by DavidWittman - downloading role from https://github.com/DavidWittman/ansible-redis/archive/1.2.4.tar.gz - extracting DavidWittman.redis to /etc/ansible/roles/DavidWittman.redis - DavidWittman.redis was installed successfully
#下載完成以後在roles下:
[root@ansible localhost]#ls roles/
DavidWittman.redis
#ansible-galaxy用法幫助
[root@ansible localhost]#ansible-galaxy -h
Usage: ansible-galaxy [delete|import|info|init|install|list|login|remove|search|setup] [--help] [options] ...
Options:
-h, --help show this help message and exit
-v, --verbose verbose mode (-vvv for more, -vvvv to enable connection
debugging)
--version show program's version number and exit
[root@ansible localhost]#ansible-galaxy list
- DavidWittman.redis, 1.2.4
#ansible-playbook:
#批量安裝httpd,並批量作好配置文件 [root@ansible localhost]#vim web.yml --- #標準格式 - hosts: web # -表示定義一個變量 remote_user: root tasks: - name: install httpd yum: name=httpd state=latest #yum表示要用到的模塊 [root@ansible localhost]#ansible-playbook web.yml PLAY [web] ********************************************************************* TASK [setup] ******************************************************************* ok: [172.16.252.143] ok: [172.16.252.182] ok: [172.16.253.177] ok: [172.16.254.47] TASK [install httpd] *********************************************************** changed: [172.16.252.182] changed: [172.16.252.143] changed: [172.16.253.177] changed: [172.16.254.47] PLAY RECAP ********************************************************************* 172.16.252.143 : ok=2 changed=1 unreachable=0 failed=0 172.16.252.182 : ok=2 changed=1 unreachable=0 failed=0 172.16.253.177 : ok=2 changed=1 unreachable=0 failed=0 172.16.254.47 : ok=2 changed=1 unreachable=0 failed=0 [root@~ localhost]#rpm -q httpd httpd-2.4.6-45.el7.centos.4.x86_64
#配置文件 [root@ansible localhost]#vim web.yml - name: configure httpd copy: src=/etc/ansible/files/http.conf #使用模塊copy dest=/etc/httpd/conf/ backup=yes #啓動服務: [root@ansible localhost]#vim web.yml - name: start httpd service: name=httpd state=started #配置文件加入handler功能: #-C:是檢查語法錯誤 [root@ansible localhost]#cat web.yml --- - hosts: web remote_user: root tasks: - name: install httpd yum: name=httpd state=latest - name: configure httpd copy: src=/etc/ansible/files/httpd.conf dest=/etc/httpd/conf/ backup=yes notify: restart httpd #handlers以前須要有notify,表示當configure httpd任務發生改變時,才執行handler的操做 handlers: - name: restart httpd service: name=httpd state=restarted [root@ansible localhost]#ansible-playbook -C web.yml PLAY [web] ********************************************************************* TASK [setup] ******************************************************************* ok: [172.16.252.143] ok: [172.16.252.182] ok: [172.16.253.177] ok: [172.16.254.47] TASK [install httpd] *********************************************************** ok: [172.16.252.182] ok: [172.16.253.177] ok: [172.16.254.47] ok: [172.16.252.143] TASK [configure httpd] ********************************************************* ok: [172.16.253.177] ok: [172.16.254.47] ok: [172.16.252.182] ok: [172.16.252.143] PLAY RECAP ********************************************************************* 172.16.252.143 : ok=3 changed=0 unreachable=0 failed=0 172.16.252.182 : ok=3 changed=0 unreachable=0 failed=0 172.16.253.177 : ok=3 changed=0 unreachable=0 failed=0 172.16.254.47 : ok=3 changed=0 unreachable=0 failed=0
#templates:
#templates:需首先建立目錄;配置的是.jz的文件 [root@ansible master-80]#mkdir templates [root@ansible master-80]#cp -a /etc/nginx/nginx.conf templates/nginx.conf.jz [root@templates master-80]#cat nginx.conf.jz #批量建立配置文件,若是port沒有指定,則用默認的80;並循環指定server_name {% for node in webs %} server { {% if http_port is defined %} listen {{ http_port }}; {% endif %} server_name {{ node }}; root /usr/share/nginx/html; location / { } } {% endfor %} #再配置.yml文件: [root@ansible master-80]#vim web3.yml --- - hosts: web remote_user: root vars: - webs: - node1 - node2 - node3 - node4 tasks: - name: nginx conf sync template: src=/etc/ansible/templates/nginx.conf.jz dest=/etc/nginx/nginx.conf [root@ansible master-80]#ansible-playbook web3.yml #測試: [root@~ host3-8088]#cat /etc/nginx/nginx.conf server { listen 8088; server_name node1; root /usr/share/nginx/html; location / { } } server { listen 8088; server_name node2; root /usr/share/nginx/html; location / { } } server { listen 8088; server_name node3; root /usr/share/nginx/html; location / { } } server { listen 8088; server_name node4; root /usr/share/nginx/html; location / { } }
#批量安裝多個APP:
#item的用法: [root@ansible master-80]#vim multi.yml --- - hosts: web remote_user: root tasks: - name: install app yum: name={{ item }} state=latest with_items: - php - php-fpm - mariadb-server [root@ansible master-80]#ansible-playbook multi.yml #批量添加用戶和組: [root@ansible master-80]#vim useradd.yml --- - hosts: web remote_user: root tasks: -name: groupadd multi group: name={{ item }} with_items: - group5 - group6 - group7 - name: useradd multi user: name={{ item.username }} group={{ item.groupname }} with_items: - { username: "user5", groupname: "group5" } - { username: "user6", groupname: "group6" } - { username: "user7", groupname: "group7" } [root@ansible master-80]#ansible-playbook useradd.yml
#roles:
#建立roles所需的目錄; [root@roles master-80]#pwd /etc/ansible/roles [root@roles master-80]#mkdir nginx [root@roles master-80]#cd nginx/ [root@nginx master-80]#mkdir {tasks,templates,files,vars,handlers,meta,default} [root@nginx master-80]#ls default files handlers meta tasks templates vars #用roles批量安裝Nginx