ansible是一个适用于中小型环境的自动化运维工具。主机总数在500台左右试用。超过500台不建议使用ansible,可以考虑Saltstack、Puppet等。
ansible作为一个管理控制主机的工具,他不需要在被管理的主机上安装插件或者代理软件。这一优点叫做agentless。他可以直接通过ssh服务来管理其他的主机,因此提前把控制主机的pub key导入到所有被控制主机很有必要的。但是同时agentless也是一个缺点,不适合管理大型的环境,通过ssh管理的效率低,这时候就需要其他的有agent的管理软件了。
ansible命令执行过程
执行状态:
前提条件:
[srvs]
192.168.38.100
142.11.227.114:80
[kvm]
142.11.227.114:80
vim /etc/ansible/ansible.cfg 修改ansible配置文件建议启用日志功能
log_path = /var/log/ansible.log
取消首次连接被控制服务器验证
host_key_checking = False
修改默认模块为shell 避免不支持某些命令,方便今后的操作不用输入-m shell
module_name = shell
[root@centos7 .ssh]#ansible all -m user -a 'name=test001 comment="test user" shell="/sbin/nologin"' 192.168.38.100 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"comment": "test user",
"create_home": true,
"group": 11212,
"home": "/home/test001",
"name": "test001",
"shell": "/sbin/nologin",
"state": "present",
"system": false,
"uid": 11212
}
142.11.227.114 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"comment": "test user",
"create_home": true,
"group": 11212,
"home": "/home/test001",
"name": "test001",
"shell": "/sbin/nologin",
"state": "present",
"system": false,
"uid": 11212
}
验证添加成功
[root@centos7 .ssh]#ansible all -a 'id test001'
192.168.38.100 | CHANGED | rc=0 >>
uid=11212(test001) gid=11212(test001) groups=11212(test001)
142.11.227.114 | CHANGED | rc=0 >>
uid=11212(test001) gid=11212(test001) groups=11212(test001)
[root@centos7 .ssh]#ansible all -a 'getent passwd test001'
192.168.38.100 | CHANGED | rc=0 >>
test001:x:11212:11212:test user:/home/test001:/sbin/nologin
142.11.227.114 | CHANGED | rc=0 >>
test001:x:11212:11212:test user:/home/test001:/sbin/nologin
httpd与nginx默认都使用的是80端口,自己觉得是否卸载主机上的httpd或修改端口
确保所有主机的epel源代可用
- hosts: kvm
remote_user: root
vars:
- Nport: 8080
tasks:
- name: install
yum: name=nginx
- name: template
template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf
notify: restart
- name: service
service: name=nginx state=started enabled=yes
handlers:
- name: restart
service: name=nginx state=restarted
server {
listen {{Nport}} default_server;
listen [::]:{{Nport}} default_server;
[root@centos7 playbook]#ansible-playbook nginx.yml
PLAY [kvm] *********************************************************************
TASK [Gathering Facts] *********************************************************
ok: [142.11.227.114]
TASK [install] *****************************************************************
changed: [142.11.227.114]
TASK [template] ****************************************************************
changed: [142.11.227.114]
TASK [service] *****************************************************************
changed: [142.11.227.114]
RUNNING HANDLER [restart] ******************************************************
changed: [142.11.227.114]
PLAY RECAP *********************************************************************
142.11.227.114 : ok=5 changed=4 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@centos7 playbook]#ansible kvm -a 'ps aux | grep nginx'
142.11.227.114 | CHANGED | rc=0 >>
root 17677 0.0 0.2 125116 2176 ? Ss 11:49 0:00 nginx: master process /usr/sbin/nginx
nginx 17678 0.0 0.6 125504 6968 ? S 11:49 0:00 nginx: worker process
root 17955 0.0 0.2 113184 2708 pts/1 S+ 11:51 0:00 /bin/sh -c ps aux | grep nginx
root 17957 0.0 0.2 112716 2312 pts/1 R+ 11:51 0:00 grep nginx
[root@centos7 playbook]#ansible kvm -a 'ss -ntlp | grep 8080'
142.11.227.114 | CHANGED | rc=0 >>
LISTEN 0 128 *:8080 *:* users:(("nginx",pid=17678,fd=6),("nginx",pid=17677,fd=6))
LISTEN 0 128 :::8080 :::* users:(("nginx",pid=17678,fd=7),("nginx",pid=17677,fd=7))
一般的域名劫持都是因为DNS指向为默认的DNS,其对查询过程进行限制或修改导致发生域名劫持。还有可能本地hosts被篡改。
解决方法一般为手动指定公共DNS服务器,例如8.8.8.8,可有效避免DNS域名劫持。查看本地hosts是否被篡改。
例如在家中访问 www.example.com.,我们dns服务是用的小区的dns服务器,我向小区dns服务器询问www.example.com.的IP是多少。如果小区的dns服务器上有记录,直接将IP地址给家里的电脑,家中的电脑和小区的dns这种关系就是迭代查询。
如果小区的dns服务器没有记录该网址的IP地址,那么小区的dns就会自己去互联网上通过.根返回.com的dns服务器的地址,通过.com的dns服务器获得.example的dns服务器地址等等,像这样一级一级的查询到该网站的IP最终将IP传到小区的dns服务器上,这样的查询就是递归查询。
一次完整的查询请求经过的流程:
Client -->hosts文件 -->DNS Service Local Cache --> DNS Server (recursion) --> Server Cache --> iteration(迭代) --> 根–> 顶级域名DNS–>二级域名DNS…
client读取本机hosts文件看是否有对应的解析记录,如果有,则直接访问IP,如果没有,则向本地DNS服务器进行递归查询,若本地DNS服务器中有对应的解析记录,则查询结束,若无记录,则进行下面步骤
本地DNS服务器采用迭代查询,向一个根DNS服务器进行查询
根DNS服务器告诉本地DNS服务器,数据库中没有你所请求查询的记录,返回对应顶级域DNS服务器的IP地址
本地DNS服务器向顶级域DNS服务器请求查询
顶级域DNS服务器告诉本地DNS服务器,数据库中没有你所请求查询的记录,返回二级域DNS服务器的IP地址
本地DNS服务器向二级域DNS服务器请求查询
…
直至查到对应的解析记录,本地DNS服务器返回给客户机对应的IP地址,至此,查询结束。(递归查询)
一. 实现区域转发
dnssec-enable no;
dnssec-validation no;
zone "ZONE_NAME" IN {
type forward;
forward {first|only};
forwarders { SERVER_IP; };
};
only:转发查询后没结果看接受转发的服务器是否再向外查询返回结果。
first:转发查询后没结果自己向互联网再查询。
zone "ZONE_NAME" IN {
type master;
file "ZONE_NAME.zone";
};
二. 实现全局转发
options {
listen-on port 53 { localhost; };
listen-on-v6 port 53 { ::1; };
directory "/var/named";
dump-file "/var/named/data/cache_dump.db";
statistics-file "/var/named/data/named_stats.txt";
memstatistics-file "/var/named/data/named_mem_stats.txt";
recursing-file "/var/named/data/named.recursing";
secroots-file "/var/named/data/named.secroots";
allow-query { any; };
forward only ;
forwarders {192.168.38.100;};
dnssec-enable no;
dnssec-validation no;
};
接收转发的服务器设置同上。
rndc reload
[root@centos7 named]# vim /etc/named.conf
acl beijing {
192.168.2.0/24;
};
acl shanghai {
192.168.38.0/24;
};
acl other {
any;
};
options {
listen-on port 53 { localhost; };
listen-on-v6 port 53 { ::1; };
directory "/var/named";
dump-file "/var/named/data/cache_dump.db";
statistics-file "/var/named/data/named_stats.txt";
memstatistics-file "/var/named/data/named_mem_stats.txt";
recursing-file "/var/named/data/named.recursing";
secroots-file "/var/named/data/named.secroots";
allow-query { any;};
dnssec-enable no;
dnssec-validation no;
};
#zone "." IN {
# type hint;
# file "named.ca";
#};
//include "/etc/named.rfc1912.zones"; #此处注销
include "/etc/named.root.key";
view view_beijing {
match-clients { beijing; };
zone "." IN {
type hint;
file "named.ca";
};
zone "test.com.beijing" {
type master;
file "test.com.zones.beijing";
};
view view_shanghai {
match-clients { shanghai; };
zone "." IN {
type hint;
file "named.ca";
};
zone "test.com.shanghai" {
type master;
file "test.com.zones.shanghai";
};
view view_other {
match-clients { other; };
zone "." IN {
type hint;
file "named.ca";
};
zone "test.com" {
type master;
file "test.com.zones";
};
[root@centos7 named]#cp -p /etc/named.rfc1912.zones /etc/test.com.zones.beijing
[root@centos7 named]#cp -p /etc/named.rfc1912.zones /etc/test.com.zones.shanghai
[root@centos7 named]#cp -p /etc/named.rfc1912.zones /etc/test.com.zones
[root@centos7 named]vim /etc/test.com.zones.beijing
zone "test.com" IN {
type master;
file "test.com.zone.beijing";
};
[root@centos7 named]vim /var/named/test.com.zone.beijing
$TTL 1D
@ IN SOA ns1.test.com. admin.test.com. (
20190721
1H
5M
7D
1D )
IN NS ns1.test.com.
ns1 IN A 192.168.2.6
www IN A 192.168.2.6
@ IN A 192.168.2.6
[root@centos7 named]vim /etc/test.com.zones.shanghai
zone "test.com" IN {
type master;
file "test.com.zone.shanghai";
};
[root@centos7 named]vim /var/named/test.com.zone.shanghai
$TTL 1D
@ IN SOA ns1.test.com. admin.test.com. (
20190721
1H
5M
7D
1D )
IN NS ns1.test.com.
ns1 IN A 192.168.38.128
www IN A 192.168.38.128
@ IN A 192.168.38.128
rndc reload