ansible roles 自动化安装

时间:2024-07-18 21:35:08

例:  ansible roles 自动化安装memcached

文件目录结构如下:

ansible  roles 自动化安装

cat memcached_role.yml

- hosts: memcached
remote_user: root roles:
- memcached

cat roles/memcached/vars/main.yml

username: memcached
groupname: memcached
memcached_port:

cat roles/memcached/templates/memcached.j2

PORT="{{ memcached_port }}"
USER="{{ username }}"
MAXCONN=""
CACHESIZE="{{ ansible_memtotal_mb//4 }}"
OPTIONS=""

cat roles/memcached/handlers/main.yml

- name: restart service
service: name=memcached state=restarted

cat roles/memcached/tasks/main.yml

- import_tasks: group.yml
- import_tasks: user.yml
- import_tasks: yum.yml
- import_tasks: templ.yml
- import_tasks: copyfile.yml
- import_tasks: start.yml

cat roles/memcached/tasks/group.yml

- name: create group
group: name={{ username }} system=yes gid=

cat roles/memcached/tasks/user.yml

- name: create user
user: name={{ username }} group={{ groupname }} uid= system=yes shell=/sbin/nologin

cat roles/memcached/tasks/yum.yml

- name: install service
yum: name=memcached

cat roles/memcached/tasks/templ.yml

- name: copy templ
template: src=memcached.j2 dest=/etc/sysconfig/memcached
notify: restart service

cat roles/memcached/tasks/copyfile.yml

- name: copy file
copy: src=memcached.log dest=/data/ owner={{ username }}

cat roles/memcached/tasks/start.yml

- name: start service
service: name=memcached enabled=yes state=started

测试:

ansible-playbook -C memcached_role.yml

执行:

ansible-playbook  memcached_role.yml