Jenkins+Ansible+Gitlab自动化部署三剑客—Ansible playbooks入门和编写规范

发布于 2021-03-23  1241 次阅读


其实,j2就是一个修改基本的信息,不同为变量,而这个变量是已经被定义的,可变的,比如:server_name,端口,位置等

 

现在test测试机上准备环境

[root@node01 ~]# mkdir /etc/nginx
[root@node01 ~]# rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
获取http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
警告:/var/tmp/rpm-tmp.7jgCch: 头V4 RSA/SHA1 Signature, 密钥 ID 7bd9bf62: NOKEY
准备中... ################################# [100%]
正在升级/安装...
1:nginx-release-centos-7-0.el7.ngx ################################# [100%]
[root@node01 ~]# 
[root@node01 ~]# useradd deploy
(.py3-a2.5-env) [deploy@10.21.214.221 test_playbooks]$ vim inventory/testenv
[testservers]
test.example.com ansible_ssh_user=root ansible_ssh_port=1022

[testservers:vars]
server_name=test.example.com
user=root
output=/root/test.txt
port=80
user=deploy
worker_porcesses=4
max_open_file=65505
root=/www
~          
(.py3-a2.5-env) [deploy@10.21.214.221 test_playbooks]$ vim roles/testbox/templates/nginx.conf.j2
--写入基本的nginx文件配置
然后修改
(.py3-a2.5-env) [deploy@10.21.214.221 test_playbooks]$ vim roles/testbox/tasks/main.yml 
- name: Print server name and user to remote testbox
  shell: "echo 'Currently {{ user }} is logining {{ server_name }}' > {{ output }}"
- name: create a file
  file: 'path=/root/foo.txt state=touch mode=0755 owner=ftp group=ftp'
- name: copy a file
  copy: 'remote_src=no src=roles/testbox/tasks/main.yml dest=/root/main.yml mode=0644 force=yes'
- name: check if main.yml exists
  stat: 'path=/root/main.yml'
  register: script_stat
- debug: msg="main.yml exists"
  when: script_stat,stat,exists
- name: shell echo
  shell: "echo '123' > foo.sh"
- name: write the nginx config file
  template: src=roles/testbox/templates/nginx.conf.j2 dest=/etc/nginx/nginx.conf
- name: ensure nginx is at the lastest version
  yum: pkg=nginx state=latest
- name: start nginx service
  service: name=nginx state=started