1、作业
[root@test1 opt]# vim test2.yaml - name: this is muluhosts: 192.168.168.22gather_facts: falsevars: ##定义变量testtest:- /opt/test1 ##对变量进行赋值- /opt/test2- /opt/test3- /opt/test4tasks:- name: create mulufile:path: "{{item}}"state: directorywith_items: "{{test}}"- name: test2hosts: 192.168.168.22gather_facts: falsetasks:- name: create mulufile:path: "{{item}}"state: directorywith_items: [/opt/test1,/opt/test2,/opt/test3,/opt/test4][root@test1 opt]# ansible-playbook test2.yaml
2、tags模块 可以给任务定义标签,可以根据标签来运行指定的任务
[root@test1 opt]# vim test3.yaml#标签的类型:
#always:设定了标签名为always,除非指定跳过这个标签,否则该任务将始终运行,即使指
定了标签还会运行
#never:始终不允许的任务,指定标签名,never可以运行。
#debug:用于调试
#setup:收集主机的信息
#标签名也可以自定义:tags
- hosts: 192.168.168.22gather_facts: falsetasks:- name: debug-test1debug:msg: "cow"tags:- debug- name: always-test1debug:msg: "always-run" ##不指定也会运行tags:- always- name: setup-test1debug:msg: "setup"tags:- setup- name: never-test1 ##除非指定,否则不运行debug:msg: "never-run"tags:- never[root@test1 opt]# ansible-playbook test3.yaml --tags=never[root@test1 opt]# ansible-playbook test3.yaml --skip-tags=always
[root@test1 opt]# ansible-playbook test3.yaml --tags="debug","setup"
[root@test1 opt]# ansible-playbook test3.yaml --tags="debug","setup" --skip-tags=always
3、自定义标签
[root@test1 opt]# vim test4.yaml- hosts: 192.168.168.22gather_facts: falseremote_user: roottasks:- name: fuzhiwenjiancopy: src=/etc/hosts dest=/opt/hoststags:- zlm- name: touch filefile: path=/opt/test1 state=touchtags:hpc
[root@test1 opt]# ansible-playbook test4.yaml --tags=hpc ##指定标签执行
[root@test1 opt]# yum -y install httpd[root@test1 opt]# cd /etc/httpd/conf
[root@test1 conf]# ls
httpd.conf magic
[root@test1 conf]# cp httpd.conf /opt/httpd.conf.j2
[root@test1 conf]#
[root@test1 conf]# cd /opt/
[root@test1 opt]# ls
123 345 httpd.conf.j2 test2.yaml test3.yaml test4.yaml[root@test1 opt]# vim test5.yaml#模板,对应用的配置文件初始化:templates模块,jinja组件,把编译过的模板文件传送给
目标文件。
- hosts: 192.168.168.22gather_facts: falseremote_user: rootvars:- pg: httpd- sv: httpdtasks:- name: install httpdyum: name={{pg}}- name: editon conftemplate: src=/opt/httpd.conf.j2 dest=/etc/httpd/conf/httpd.confnotify:- restart httpdhandlers:- name: restart httpdservice: name={{sv}} state=restarted[root@test1 opt]# vim httpd.conf.j2 42 Listen {{http_port}}95 ServerName {{server_name}}119 DocumentRoot "{{root_dir}}"[root@test1 ansible]# vim hosts [web]
## alpha.example.org
## beta.example.org
192.168.168.22 http_port=192.168.168.22:80 server_name=www.xy.com:80 root_dir=/etc/httpd/htdocs
192.168.168.23 ansible_port=22 ansible_user=root ansible_password=123[root@test2 httpd]# mkdir htdocs[root@test2 httpd]# yum -y remove httpd[root@test1 opt]# ansible-playbook test5.yaml
[root@test2 httpd]# netstat -antp | grep httpd
[root@test2 httpd]# netstat -antp | grep 80
[root@test2 httpd]# curl 192.168.168.22
[root@test2 htdocs]# cd /var/www/html/
[root@test2 html]# ls
index.html
[root@test2 html]# ls
index.html
[root@test2 html]# cat index.html
this is httpd
[root@test2 html]# find / -type f -name index.html
/usr/share/httpd/noindex/index.html
[root@test2 html]# cd /usr/share/httpd/noindex/
[root@test2 noindex]# ls
css images index.html
[root@test2 noindex]# cat index.html
[root@test2 noindex]# echo 123 > index.html
[root@test2 noindex]# curl 192.168.168.22
123
nginx 传参的方式,端口8080 servername:www.xy.com
4、nsible为了层次化,结构化的组织playbook,使用roles(角色)通过层次化自动装载变量,任务和处理器等等。
roles把变量,任务和模块的文件单独放在各个不同的目录中,通过rolse一键的编排。
mkdir /etc/ansible/roles/httpd/{files,templates,tasks,handlers,vars,defaults,meta} -p
mkdir /etc/ansible/roles/mysql/{files,templates,tasks,handlers,vars,defaults,meta} -p
mkdir /etc/ansible/roles/php/{files,templates,tasks,handlers,vars,defaults,meta} -p[root@test1 ansible]# yum -y install tree
[root@test1 ansible]# cd roles/
[root@test1 roles]# treeroles:
├── httpd ##角色名称,自定义
│ ├── defaults ##存放配置文件的目录,可以不写
│ ├── files ##存放copy模块或者script
│ ├── handlers ##存放处理器文件的目录
│ ├── meta ##保存角色源信息的文件
│ ├── tasks ##保存任务的文件
│ ├── templates ##保存模板的文件
│ └── vars ##保存变量的文件
就是把原来写一个yaml的配置,分开--------》不同的目录----------》保存在一个名字的yaml里面。
执行的时候调用不同目录的同一个yaml的文件
main.yaml
[root@test1 roles]# touch /etc/ansible/roles/php/{files,templates,tasks,handlers,vars,defaults,meta}/main.yaml
[root@test1 roles]# touch /etc/ansible/roles/mysql/{files,templates,tasks,handlers,vars,defaults,meta}/main.yaml
[root@test1 roles]# touch /etc/ansible/roles/httpd/{files,templates,tasks,handlers,vars,defaults,meta}/main.yaml
[root@test1 roles]# tree
总结:
ansible:14个模块必须掌握,熟练。
playbook:剧本能够定义一般的远程部署相关的配置即可
了解条件判断,循环
tags的作用 标签的 系统标签:always never 自定义
templates:了解即可
roles:了解即可。
作业、
配置主机清单,实现免密钥对登录。声明ip地址列表
1、在目标主机批量创建目录:/opt/test1 /opt/test2 /opt/test3
2、从主机批量复制文件,123 456 789,分别输出到指定的123–>test1 456---->test2 789---->test3
指定主机为192.168.233.20.
3、创建一个nginx.conf文件,改配置文件可以实现upstream反向代理 复制到nginx1
4、分别在nginx2和nginx3上配置页面: test1 test2
5、在主机访问目标主机nginx1,实现负载均衡。
以上步骤全部用ansible远程完成!
[root@test1 opt]# vim test6.yaml#配置主机清单,实现免密钥对登录。声明ip地址列表
#1、在目标主机批量创建目录:/opt/test1 /opt/test2 /opt/test3
#2、从主机批量复制文件,123 456 789,分别输出到指定的
123-->test1 456---->test2 789---->test3
#指定主机为192.168.168.22.
#3、创建一个nginx.conf文件,改配置文件可以实现upstream反向代理 复制到nginx1
#4、分别在nginx2和nginx3上配置页面: test1 test2
#5、在主机访问目标主机nginx1,实现负载均衡。
#以上步骤全部用ansible远程完成!
- name: this is ifhosts: allremote_user: roottasks:- name: create mulufile:path: "{{item}}"state: directorywith_items: [/opt/test1,/opt/test2,/opt/test3]when: ansible_default_ipv4.address == "192.168.168.22"- name: copy wenjiancopy:src: "{{ item.src }}"dest: "/opt/{{ item.dest }}"with_items:- { src: '/opt/test/123', dest: 'test1' }- { src: '/opt/test/456', dest: 'test2' }- { src: '/opt/test/789', dest: 'test3' }when: ansible_default_ipv4.address == "192.168.168.22"- name: nginx1yum: name=nginx state=latest- name: nginx.confcopy: 'src=/opt/nginx.conf dest=/etc/nginx/nginx.conf'when: ansible_default_ipv4.address == "192.168.168.22"notify: start nginx- name: test2 connectionping:- name: close selinuxcommand: '/sbin/setenforce 0'ignore_errors: true- name: close firewalldservice: name=firewalld state=stopped- name: install nginxyum: name=nginx state=latest- name: interviewshell: echo "this is test1" > /usr/share/nginx/html/index.htmlwhen: ansible_default_ipv4.address == "192.168.168.23"notify: start nginx- name: test3 connectionping:- name: close2 selinuxcommand: '/sbin/setenforce 0'ignore_errors: true- name: close2 firewalldservice: name=firewalld state=stopped- name: install nginx2yum: name=nginx state=latest- name: interview2shell: echo "this is test2" > /usr/share/nginx/html/index.htmlwhen: ansible_default_ipv4.address == "192.168.168.24"notify: start nginxhandlers:- name: start nginxservice: name=nginx state=started[root@test1 ansible]# yum -y install nginx
[root@test1 ansible]# cp /etc/nginx/nginx.conf /opt/nginx.conf
[root@test1 ansible]# cd /opt/
[root@test1 opt]# ls
123 345 httpd.conf.j2 nginx.conf test test2.yaml test3.yaml test4.yaml test5.yaml test6.yaml
[root@test1 opt]# vim nginx.conf upstream xy102 {server 192.168.168.23;server 192.168.168.24;}server {listen 80;listen [::]:80;server_name _;root /usr/share/nginx/html;location / {root html;index index.html index.htm;proxy_pass http://xy102;}[root@test1 opt]# curl 192.168.168.22
this is test1
[root@test1 opt]# curl 192.168.168.22
this is test1
[root@test1 opt]# curl 192.168.168.22
this is test2
[root@test1 opt]# curl 192.168.168.22
this is test2
[root@test1 opt]# curl 192.168.168.22
this is test1
[root@test1 opt]# curl 192.168.168.22
this is test2
[root@test1 opt]# curl 192.168.168.22
this is test1
[root@test1 opt]# curl 192.168.168.22
this is test1