[Ansible自动化]之批量推送文件
Ansible中文权威指南 Ansible
Ansible-Playbooks中文指南 Ansible-Playbooks
剧本正文
请注意:(以下剧本存在一些变量,请根据实际情况使用或修改,请根据YML格式进行修改)
剧本目录详情
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| /etc/ansible/ ├── ansible.cfg ├── hosts ├── inventory │ └── aliyun │ ├── zabbix │ └── webapps │ └── hosts ├── keys │ └── ssh │ ├── centos_id_rsa.pub │ └── centos-key.pem ├── roles │ ├── sync │ │ ├── files │ │ │ └── music.mp3 │ │ ├── tasks │ │ │ ├── main.yml │ │ │ └── sync.yml │ │ └── templates │ │ └── webapps.j2 │ └── vars └── sync.yml
|
/etc/sync.yml
1 2 3 4 5 6 7
| - hosts: "{{ hosts }}" user: "{{ user }}" become: yes become_user: root become_method: sudo roles: - sync
|
/etc/roles/sync/tasks/sync.yml
1 2 3 4
| - name: Sync webapps config template: src=/etc/ansible/roles/sync/templates/webapps.j2 dest=/etc/nginx/conf.d/webapps.conf owner=root group=root mode=0644 follow=yes - name: Sync webapps music copy: src=/etc/ansible/roles/sync/files/music.mp3 dest=/data/servers/webapps/static/music/music.mp3 owner=nginx group=nginx mode=0644 follow=yes
|
/etc/ansible/inventory/aliyun/webapps/hosts
1 2
| [frontend] 192.168.1.100 ansible_ssh_port=22 ansible_ssh_user=centos ansible_ssh_private_key_file=/etc/ansible/keys/ssh/centos-key.pem
|
webapps.j2
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
| server { listen 80; server_name {{ frontendip }}; index index.html index.htm;
location / { root /data/servers/webapps/; index index.html index.htm; try_files $uri $uri/ @rewrites; }
location @rewrites { rewrite ^(.+)$ /index.html last; }
location /api/ { proxy_pass http://{{ backendip }}:8080/; client_max_body_size 300m; client_body_buffer_size 128k; proxy_connect_timeout 6000; proxy_read_timeout 6000; proxy_send_timeout 6000; proxy_buffer_size 128k; proxy_buffers 4 128k; proxy_busy_buffers_size 128k; proxy_set_header Host $host; proxy_set_header X-Real-Ip $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } }
|
剧本详情讲解
1 2 3 4 5 6 7 8
| $ ansible -i /etc/ansible/inventory/aliyun/webapps/hosts frontend -a 'uptime' $ ansible-playbook /etc/ansible/sync.yml -i /etc/ansible/inventory/aliyun/webapps/hosts --extra-vars "hosts=frontend user=centos frontendip=192.168.1.100 backendip=192.168.1.200"
|
执行结果
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
| music.mp3传输目标主机的/data/servers/webapps/static/music/music.mp3下面 webapps.j2传输目标主机的/etc/nginx/conf.d/下并改名为webapps.conf 然后通过传输的变量使配置文件内容变为: server { listen 80; server_name 192.168.1.100; index index.html index.htm;
location / { root /data/servers/webapps/; index index.html index.htm; try_files $uri $uri/ @rewrites; }
location @rewrites { rewrite ^(.+)$ /index.html last; }
location /api/ { proxy_pass http://192.168.1.200:8080/; client_max_body_size 300m; client_body_buffer_size 128k; proxy_connect_timeout 6000; proxy_read_timeout 6000; proxy_send_timeout 6000; proxy_buffer_size 128k; proxy_buffers 4 128k; proxy_busy_buffers_size 128k; proxy_set_header Host $host; proxy_set_header X-Real-Ip $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } }
|
More Info: Ansible