标签:handler state jinja2 nginx配置 apach use url 系统 hand
ansible-playbook官方文档:https://docs.ansible.com/ansible/latest/user_guide/playbooks.html
$ mkdir ansible-demo && cd ansible-demo/
$ vim site.conf #配置文件
server {
listen 80;
server_name www.cropy.cn;
location / {
root /var/www/html/;
index index.html;
}
}
$ vim nginx.yml
---
- hosts: webservers
vars:
hello: Ansible
tasks:
- name: Add Nginx Repo
yum_repository:
name: nginx
description: nginx repo
baseurl: http://nginx.org/packages/centos/7/$basearch/
gpgcheck: no
enabled: 1
- name: install nginx
yum:
name: nginx
state: latest
- name: copy nginx config
copy:
src: ./site.conf
dest: /etc/nginx/conf.d/site.conf
- name: start nginx
service:
name: nginx
state: started
enabled: yes
- name: create wwwroot directory
file:
dest: /var/www/html
state: directory
- name: create test page
shell: "echo {{ hello }} > /var/www/html/index.html"
$ ansible-playbook nginx.yml --syntax-check #语法检查&调试
$ ansible-playbook nginx.yml #部署
---
开始#
代表注释,不执行---
- hosts: webservers
gather_facts: no
tasks:
- name: copy nginx config
copy:
src: ./site2.conf
dest: /etc/nginx/conf.d/site2.conf
notify:
- reload nginx
handlers:
- name: reload nginx
service: name=nginx state=reloaded
$ vim nginx.yml
---
- hosts: webservers
vars:
hello: Ansible
tasks:
- name: Add Nginx Repo
yum_repository:
name: nginx
description: nginx repo
baseurl: http://nginx.org/packages/centos/7/$basearch/
gpgcheck: no
enabled: 1
tags: addRepo
- name: install nginx
yum:
name: nginx
state: latest
tags: installNginx
- name: copy nginx config
copy:
src: ./site.conf
dest: /etc/nginx/conf.d/site.conf
- name: start nginx
service:
name: nginx
state: started
- name: create wwwroot directory
file:
dest: /var/www/html
state: directory
- name: create test page
shell: "echo {{ hello }} > /var/www/html/index.html"
tags:
- addHtml
- addTest
$ ansible-playbook nginx.yml --tags "addRepo" #指定tags
$ ansible-playbook nginx.yml --tags "addRepo,installNginx"
$ ansible-playbook nginx.yml --skip-tags "addRepo,installNginx"
$ vim debug.yml
---
- hosts: webservers
gather_facts: no
vars:
hello: Ansible
tasks:
- name: debug message
debug: msg={{ hello }}
$ ansible-playbook debug.yml
$ vim tomcat.yml
---
- hosts: webservers
gather_facts: no
vars:
tomcat_version: 8.5.56
tomcat_install_dir: /usr/local
tasks:
- name: install jdk
yum: name=java-1.8.0-openjdk state=present
- name: Download tomcat
get_url: url=http://mirrors.hust.edu.cn/apache/tomcat/tomcat-8/v{{ tomcat_version }}/bin/apache-tomcat-{{ tomcat_version }}.tar.gz dest=/tmp
- name: unarchive tomcat-{{ tomcat_version }}.tar.gz
unarchive:
src: /tmp/apache-tomcat-{{ tomcat_version }}.tar.gz
dest: "{{ tomcat_install_dir }}"
copy: no
- name: start tomcat
shell: cd {{ tomcat_install_dir }} &&
mv apache-tomcat-{{ tomcat_version }} tomcat8 &&
cd tomcat8/bin && nohup ./startup.sh &
$ ansible-playbook tomcat.yml --syntax-check #语法检查
$ ansible-playbook tomcat.yml
$ ansible-playbook nginx/nginx.yml --list-tags
$ ansible-playbook nginx/nginx.yml --list-tasks
$ vim demo1.yml
---
- hosts: webservers
gather_facts: no
remote_user: root
tasks:
- name: test var
debug: msg="{{ work_dir }}"
$ ansible-playbook demo1.yml -e work_dir="/root/dadda" #-e指定变量
$ mkdir /etc/ansible/group_vars
$ vim /etc/ansible/group_vars/webservers.yml
http_port: 8080
server_name: 222.baidu.com
$ vim /etc/ansible/hosts #去掉此处定义的vars
[webservers]
192.168.56.11 ansible_ssh_user=root ansible_ssh_pass=yeecallk8s http_port=80
192.168.56.12 ansible_ssh_user=root ansible_ssh_pass=yeecallk8s
192.168.56.13 ansible_ssh_user=root ansible_ssh_pass=yeecallk8s
192.168.56.14 ansible_ssh_user=root ansible_ssh_pass=yeecallk8s
vim demo2.yml
---
- hosts: webservers
gather_facts: no
vars:
- work_dir: /usr/local
- nginx_version: 1.18
tasks:
- name: create dir
shell: mkdir -p "{{ work_dir }}/{{ nginx_version }}"
---
- hosts: webservers
gather_facts: no
vars:
- work_dir: /usr/local
- nginx_version: 1.18
tasks:
- name: create dir
shell: mkdir -p "{{ work_dir }}/{{ nginx_version }}"
- name: register var
command: date +%F_%T
register: datetime
- name: touch file
file: dest=/tmp/r_{{ datetime.stdout }} state=touch
--list-tags
,--list-tasks
不会输出显示├── hosts #主机文件
├── lnmp.yml #lnmp入口
├── lnmt.yml #lnmt入口
├── mysql.yml #mysql入口
├── nginx.yml #nginx入口
├── php.yml #PHP入口
└── tomcat.yml #tomcat入口
$ cat hosts
[lnmp]
192.168.56.11
192.168.56.12
$ cat lnmp.yml
---
- import_playbook: nginx.yml
- import_playbook: mysql.yml
- import_playbook: php.yml
$ cat lnmt.yml
---
- import_playbook: nginx.yml
- import_playbook: mysql.yml
- import_playbook: tomcat.yml
$ cat mysq.yml
---
- hosts: lnmp
gather_facts: no
tasks:
- name: Install Mysql
debug: msg="install mysql..."
$ cat nginx.yml
---
- hosts: lnmp
gather_facts: no
tasks:
- name: Install Ngigx
debug: msg="install nginx..."
$ cat php.yml
---
- hosts: lnmp
gather_facts: no
tasks:
- name: Install PHP
debug: msg="install php..."
$ cat tomcat.yml
---
- hosts: lnmp
gather_facts: no
tasks:
- name: Install Tomcat
debug: msg="install tomcat..."
$ ansible-playbook -i hosts lnmt.yml #部署lnmt
$ ansible-playbook -i hosts lnmp.yml #部署lnmp
├── hosts #主机文件
├── main.yml #任务入口文件
├── task1.yml #task1
└── task2.yml #task2
$ cat hosts
[lnmp]
192.168.56.11
192.168.56.12
$ cat main.yml
---
- hosts: lnmp
gather_facts: no
tasks:
- include_tasks: task1.yml
- include_tasks: task2.yml
$ cat task1.yml
---
- name: task1
debug: msg="task1"
$ cat task2.yml
---
- name: task2
debug: msg="task2"
$ ansible-playbook -i hosts main.yml
$ vim if_case.yml
---
- hosts: webservers
gather_facts: yes
tasks:
- name: host 192.168.56.11 run this task
debug: msg={{ansible_default_ipv4.address}}
when: ansible_default_ipv4.address == ‘192.168.56.11‘
$ ansible-playbook if_case.yml
$ vim demo2.yml
---
- hosts: webservers
tasks:
- name: Update Apache Version - yum
yum: name=httpd state=present
when: ansible_pkg_mgr == ‘yum‘
notify: restart httpd
- name: Update Apache Version -apt
apt: name=httpd state=present
when: ansible_pkg_mgr == ‘apt‘
notify: restart apache2
handlers:
- name: restart httpd
service: name=httpd state=restarted
- name: restart apache2
service: name=apache2 stste=restarted
$ ansible-playbook demo2.yml
$ vim demo1.yml
---
- hosts: webservers
gather_facts: no
tasks:
- name: loop list
debug: msg={{ item }}
loop:
- one
- two
- three
$ ansible-playbook demo1.yml
使用场景: 循环序列,比如多个用户创建等
$ vim adduser.yml
---
- hosts: webservers
gather_facts: no
tasks:
- name: add user and group
user: name={{item.name}} group={{item.groups}} state=present
with_items:
- {name: ‘zhangsan‘,groups: ‘nginx‘}
- {name: ‘lisi‘, groups: ‘nginx‘}
$ vim test.yml
---
- hosts: webservers
gather_facts: no
vars:
hello: Ansible
tasks:
- template: src=f.j2 dest=/tmp/f.j2
$ vim f.j2
{% set list=[‘a‘,‘b‘,‘c‘] %}
{% for i in list %}
{% if i == ‘b‘ %}
-> b
{% elif loop.index == 3 %}
-> 3
{% else %}
{{ i }}
{% endif %}
{% endfor %}
{{ hello }}
{% for key,value in dict.iteritems() %}
{{ key }} ====> {{ value }}
{% endfor %}
$ ansible-playbook test.yml
$ cat /tmp/f.j2
a
-> b
-> 3
Ansible
lisi ====> 23
zhangsan ====> 19
$ cat site.j2
upstream {{ server_name }} {
{% set list=[10,12,15,18] %}
upstream {{ server_name }} {
{% for i in list %}
server 192.168.56.{{ i }}:80;
{% endfor %}
}
server {
listen {{ listen_port }};
server_name {{ server_name }};
location / {
root /var/www/html/;
index index.html;
}
}
$ cat add_config.yml
---
- hosts: webservers
gather_facts: no
vars:
listen_port: 80
server_name: www.cropy.com
tasks:
- name:
template: src=./site.j2 dest=/etc/nginx/conf.d/site.conf
notify: restart nginx
handlers:
- name: restart nginx
service: name=nginx state=restarted
$ ansible-playbook add_config.yml
标签:handler state jinja2 nginx配置 apach use url 系统 hand
原文地址:https://blog.51cto.com/13812615/2519148