ansible批量自动部署Nginx

六眼飞鱼酱① 提交于 2019-12-28 16:15:47

[root@node1 file]# tree
.
├── hosts
├── install.yml
└── roles
├── common
│ └── tasks
│ └── main.yml
└── nginx
├── files
│ └── nginx-1.12.2.tar.gz
├── tasks
│ └── main.yml
├── templates
└── vars
└── main.yml

8 directories, 6 files

]#vim hosts
[node]
node1 ansible_ssh_user=root ansible_ssh_pass=xxxx
node2 ansible_ssh_user=root ansible_ssh_pass=xxxx
node3 ansible_ssh_user=root ansible_ssh_pass=xxxx
node4 ansible_ssh_user=root ansible_ssh_pass=xxxx
node5 ansible_ssh_user=root ansible_ssh_pass=xxxx
node6 ansible_ssh_user=root ansible_ssh_pass=xxxx
]# vim install.yml

  • hosts: node
    gather_facts: no
    remote_user: root

    roles:

    • role: common
    • role: nginx
      ]# vim roles/common/tasks/main.yml

  • name: Install deps
    yum: name={{ item }} state=present
    #become: true
    #become_user: root
    #become_method: su
    with_items:
    • gcc
    • make
    • zlib-devel
    • openssl-devel
    • pcre-devel
    • pcre
    • zlib
    • openssl

]# vim roles/nginx/tasks/main.yml
`—

  • name: Copy Nginx
    copy: src=nginx-1.12.2.tar.gz dest=/tmp

  • name: tar nginx
    unarchive:
    src: /tmp/nginx-1.12.2.tar.gz
    dest: “{{ install_bashdir }}”
    copy: no

  • name: Install Nginx
    shell: “cd {{ install_bashdir }}/nginx-1.12.2 && ./configure --prefix={{ install_bashdir }}/nginx --user={{ nginx_user }} --group={{ nginx_group }} --with-http_stub_status_module --with-http_ssl_module && make && make install”

  • name: chown work Permission
    file: dest={{ install_bashdir }}/nginx owner={{ nginx_user }} group={{ nginx_user }} recurse=yes

  • name: remove nginx package
    file: dest={{ install_bashdir }}/nginx-1.12.2 state=absent

  • name: start nginx
    shell: cd “{{ install_bashdir }}” && nginx/sbin/nginx

]# vim roles/nginx/vars/main.yml
install_bashdir: /usr/local/
nginx_user: root
nginx_group: root

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!