因为自己笔记本性能问题,这里只用两台虚拟机做测试:
服务器名 | IP |
---|---|
ansible-server | 192.168.31.53 |
ansible-client | 192.168.31.167 |
因为之前已经写了基本的了,这里主要是测试为主。
测试1,基本使用:通过检测要执行主机的CPU核数,设置nginx启动的worker进程数(CPU核数+2),创建用户组及用户nginx指定uid和gid为80,通过yum安装nginx服务,拷贝配置文件nginx.conf.j2到/etc/nginx/nginx.conf,启动服务,并设为开机自启。
1.1 结构[root@ansible ansible_test2]# tree roles/nginx/
roles/nginx/
├── tasks
│ ├── group.yml
│ ├── main.yml
│ ├── restart.yml
│ ├── start.yml
│ ├── templ.yml
│ ├── user.yml
│ └── yum.yml
└── templates
└── nginx.conf.j2
[root@ansible ansible_test2]# cat roles/nginx/tasks/main.yml
include: group.yml include: user.yml include: yum.yml include: templ.yml include: start.yml[root@ansible ansible_test2]# cat roles/nginx/tasks/group.yml
name: create group[root@ansible ansible_test2]# cat roles/nginx/tasks/user.yml
name: create user[root@ansible ansible_test2]# cat roles/nginx/tasks/yum.yml
name: install package[root@ansible ansible_test2]# cat roles/nginx/tasks/templ.yml
name: copy conf[root@ansible ansible_test2]# cat roles/nginx/tasks/start.yml
name: start service[root@ansible ansible_test2]# cat nginx_role.yml
---
- hosts: web
remote_user: root
roles:
- role: nginx
1.3 模板文件改动
这里使用的是nginx的配置文件
[root@ansible ~]# egrep ‘{{’ /root/ansible_test2/roles/nginx/templates/nginx.conf.j2
worker_processes {{ ansible_processor_vcpus+2 }};
[root@ansible ansible_test2]# ansible-playbook nginx_role.yml
PLAY [web] *****************************************************************************************************************************************************************************************************
TASK [Gathering Facts] ******************************************************************************************************************************************************************************************
ok: [192.168.31.167]
TASK [nginx : create group] *************************************************************************************************************************************************************************************
changed: [192.168.31.167]
TASK [nginx : create user] **************************************************************************************************************************************************************************************
changed: [192.168.31.167]
TASK [nginx : install package] **********************************************************************************************************************************************************************************
changed: [192.168.31.167]
TASK [nginx : copy conf] ****************************************************************************************************************************************************************************************
changed: [192.168.31.167]
TASK [nginx : start service] ************************************************************************************************************************************************************************************
changed: [192.168.31.167]
PLAY RECAP ******************************************************************************************************************************************************************************************************
192.168.31.167 : ok=6 changed=5 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@ansible ~]# ansible web -m shell -a ‘rpm -q nginx ; ls -l /etc/nginx/nginx.conf ; ss -tnl| grep 80;ps aux | grep nginx | grep worker’
192.168.31.167 | CHANGED | rc=0 >>
nginx-1.16.1-1.el7.x86_64
-rw-r–r-- 1 root root 2468 Feb 10 20:02 /etc/nginx/nginx.conf
LISTEN 0 128 :80 :
LISTEN 0 128 :::80 :::
nginx 2086 0.0 0.0 121236 3512 ? S 13:53 0:00 nginx: worker process
nginx 2087 0.0 0.0 121236 3512 ? S 13:53 0:00 nginx: worker process
nginx 2088 0.0 0.0 121236 3512 ? S 13:53 0:00 nginx: worker process
nginx 2089 0.0 0.0 121236 3512 ? S 13:53 0:00 nginx: worker process
nginx 2090 0.0 0.0 121236 3512 ? S 13:53 0:00 nginx: worker process
nginx 2091 0.0 0.0 121236 3512 ? S 13:53 0:00 nginx: worker process
[root@ansible httpd]# tree
.
├── files
│ └── httpd.conf
├── tasks
│ ├── copy.yml
│ ├── main.yml
│ └── user.yml
└── templates
[root@ansible ansible_test2]# cat roles/httpd/tasks/main.yml
include: user.yml include: copy.yml[root@ansible ansible_test2]# cat roles/httpd/tasks/user.yml
name: create user[root@ansible ansible_test2]# cat roles/httpd/tasks/copy.yml
name: copy files[root@ansible ansible_test2]# cat httpd_role.yml
- hosts: web
remote_user: root
roles:
- httpd
2.3 执行
[root@ansible ansible_test2]# ansible-playbook httpd_role.yml
PLAY [web] ****************************************************************************************************************************
TASK [Gathering Facts] ****************************************************************************************************************
ok: [192.168.31.167]
TASK [httpd : create user] ************************************************************************************************************
changed: [192.168.31.167]
TASK [httpd : copy files] *************************************************************************************************************
changed: [192.168.31.167]
PLAY RECAP ****************************************************************************************************************************
192.168.31.167 : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@ansible ansible_test2]# ansible web -m shell -a ‘getent passwd apache; ls /root/httpd.conf’
192.168.31.167 | CHANGED | rc=0 >>
apache❌985:979::/home/apache:/sbin/nologin
/root/httpd.conf
[root@ansible ansible_test2]# cat some_role.yml
---
- hosts: all
remote_user: root
roles:
- { role: httpd, tags: [ 'web','httpd' ]}
- { role: nginx, tags: [ 'web','nginx' ], when ansible_distribution_major_version == "7" }
- { role: app, tags: "app" }
指定只运行web的:
[root@ansible ansible_test2]# ansible-playbook -t web some_role.yml
PLAY [web] ******************************************************************************************************************************************************************************************************
TASK [Gathering Facts] ******************************************************************************************************************************************************************************************
ok: [192.168.31.167]
TASK [httpd : create user] **************************************************************************************************************************************************************************************
ok: [192.168.31.167]
TASK [httpd : copy files] ***************************************************************************************************************************************************************************************
ok: [192.168.31.167]
TASK [nginx : create group] *************************************************************************************************************************************************************************************
ok: [192.168.31.167]
TASK [nginx : create user] **************************************************************************************************************************************************************************************
ok: [192.168.31.167]
TASK [nginx : install package] **********************************************************************************************************************************************************************************
ok: [192.168.31.167]
TASK [nginx : copy conf] ****************************************************************************************************************************************************************************************
ok: [192.168.31.167]
TASK [nginx : start service] ************************************************************************************************************************************************************************************
ok: [192.168.31.167]
PLAY RECAP ******************************************************************************************************************************************************************************************************
192.168.31.167 : ok=8 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@ansible ansible_test2]# tree
.
├── httpd_role.yml
├── nginx_role.yml
├── roles
│ ├── httpd
│ │ ├── files
│ │ │ └── httpd.conf
│ │ ├── tasks
│ │ │ ├── copy.yml
│ │ │ ├── main.yml
│ │ │ └── user.yml
│ │ └── templates
│ ├── memcache
│ ├── mysql
│ └── nginx
│ ├── tasks
│ │ ├── group.yml
│ │ ├── main.yml
│ │ ├── restart.yml
│ │ ├── start.yml
│ │ ├── templ.yml
│ │ ├── user.yml
│ │ └── yum.yml
│ └── templates
│ └── nginx.conf.j2
└── some_role.yml
对于跨项目去引用别的项目的tasks内动作的情况:
比如nginx项目内引用httpd内的copy.yml 可以在main.yml内include后加 roles/httpd/tasks/copy.yml 直接引用
[root@ansible roles]# tree
.
├── app
│ ├── files
│ │ └── vhosts.conf
│ ├── handlers
│ │ └── main.yml
│ ├── tasks
│ │ ├── copyfile.yml
│ │ ├── group.yml
│ │ ├── main.yml
│ │ ├── start.yml
│ │ ├── templ.yml
│ │ ├── user.yml
│ │ └── yum.yml
│ ├── templates
│ │ └── httpd.conf.j2
│ └── vars
│ └── main.yml
[root@ansible app]# cat tasks/main.yml
include: group.yml include: user.yml include: yum.yml include: templ.yml include: copyfile.yml include: start.yml[root@ansible app]# cat tasks/group.yml
name: create group[root@ansible app]# cat tasks/user.yml
name: create user[root@ansible app]# cat tasks/yum.yml
name: install package[root@ansible app]# cat tasks/templ.yml
name: copy conf[root@ansible app]# cat tasks/copyfile.yml
name: copy conf[root@ansible app]# cat tasks/start.yml
name: start service[root@ansible app]# cat handlers/main.yml
- name: restart service
service: name=httpd state=restarted
[root@ansible app]# cat vars/main.yml
username : app
groupname: app
4.3 模板文件内的变量
[root@ansible app]# egrep ‘{{’ templates/httpd.conf.j2
Listen {{ ansible_processor_vcpus*10 }}
User {{ username }}
Group {{ groupname }}
[root@ansible app]# cat handlers/main.yml
name: restart service[root@ansible ansible_test2]# cat app_role.yml
- hosts: web
remote_user: root
roles:
- app
[root@ansible ansible_test2]# ansible-playbook app_role.yml
PLAY [web] ****************************************************************************************************************************
TASK [Gathering Facts] ****************************************************************************************************************
ok: [192.168.31.167]
TASK [app : create group] *************************************************************************************************************
changed: [192.168.31.167]
TASK [app : create user] **************************************************************************************************************
changed: [192.168.31.167]
TASK [app : install package] **********************************************************************************************************
changed: [192.168.31.167]
TASK [app : copy conf] ****************************************************************************************************************
changed: [192.168.31.167]
TASK [app : copy conf] ****************************************************************************************************************
changed: [192.168.31.167]
TASK [app : start service] ************************************************************************************************************
changed: [192.168.31.167]
RUNNING HANDLER [app : restart service] ***********************************************************************************************
changed: [192.168.31.167]
PLAY RECAP ****************************************************************************************************************************
192.168.31.167 : ok=8 changed=7 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@ansible ansible_test2]# ansible web -m shell -a ‘getent passwd app ;getent group app; rpm -q httpd; ss -ntlp| grep httpd; ps -ef | grep httpd’
192.168.31.167 | CHANGED | rc=0 >>
app❌123:123::/home/app:/sbin/nologin
app❌123:
httpd-2.4.6-90.el7.centos.x86_64
LISTEN 0 128 :::80 ::