企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
- 目录结构 ``` [root@ansible-20 ansible]# tree roles/nginx/ -L 2 roles/nginx/ ├── files │   └── conf ├── tasks │   └── main.yml └── templates ``` - hosts文件 ``` [root@ansible-20 ansible]# cat hosts [nginx] 192.168.47.21 ``` - 执行文件 ``` [root@ansible-20 ansible]# cat nginx_conf.yml - hosts: nginx vars: remote_conf_basedir: /opt/nginx/ remote_conf: /opt/nginx/conf remote_backup_basedir: /opt/nginx/conf_backup/ remote_backup: /opt/nginx/conf_backup/{{ lookup('pipe','date +%Y%m%d-%H-%M-%S') }} local_gitdir: /root/gitdir/push_conf/nginx_conf cmd_git_fetch: git fetch cmd_git_rebase: git rebase origin/master service_restart: "/opt/nginx/sbin/nginx -s reload" roles: - nginx ``` - task文件 ``` # git拉取最新版本的配置文件 - name: git fech command: chdir={{ local_gitdir }} {{ item }} connection: local with_items: - "{{ cmd_git_fetch }}" - "{{ cmd_git_rebase }}" # 创建备份目录 - name: mkdir backup basedir file: name: "{{ item }}" state: directory with_items: - "{{ remote_backup_basedir }}" # 备份配置文件到备份目录 - name: backup conf shell: cp -a "{{ remote_conf }}" "{{ remote_backup }}" # 同步最新的配置文件 - name: sync synchronize: src: "{{ local_gitdir }}/conf" dest: "{{ remote_conf_basedir }}" delete: yes recursive: yes register: service_status # 如果文件有改变就重启服务 - name: reload nginx shell: "{{ service_restart }}" when: service_status.changed ```