tags模块 可以给任务定义标签,可以根据标签来运行指定的任务
[root@test41 opt]# vim test1.yaml #标签的类型 #always:设定标签名为always,除非指定跳过这个标签,否则该任务始终会运行,即使指定了标签还会运行 #never:从不运行的任务,指定标签名never可以运行 #debug:用于调试 #setup:收集主机的信息 #标签名也可以自定义:tags - hosts: 192.168.65.10gather_facts: falsetasks:- name: debug-test1debug:msg: 'cow'tags:- debug - name: always-test1debug:msg: 'ALWAYS-RUN'tags:- always - name: never-test1debug:msg: "Never-run"tags:- never - name: setup-test1debug:msg: "SETUP-1"tags:- setup [root@test41 opt]# ansible-playbook test1.yaml --tags=setup #指定执行哪个 [root@test41 opt]# ansible-playbook test1.yaml --tags="debug","setup" #跳过指定标签 [root@test41 opt]# ansible-playbook test1.yaml --skip-tags=always
创建自定义任务
[root@test41 opt]# ansible-playbook test1.yaml --skip-tags=always [root@test41 opt]# vim test2.yaml - hosts: 192.168.65.10remote_user: roottasks:- name: 复制文件copy: src=/etc/hosts dest=/opt/hoststags:- cyp- name: touch filefile: path=/opt/test1.txt state=touchtags:- dly [root@test41 opt]# ansible-playbook test2.yaml --tags=cyp [root@test41 opt]# ansible-playbook test2.yaml --tags=dly [root@test10 opt]# ll 总用量 4 -rw-r--r--. 1 root root 158 8月 23 10:39 hosts -rw-r--r--. 1 root root 0 8月 23 10:38 test1.txt
[root@test41 opt]# cd /etc/httpd/ [root@test41 httpd]# ls conf conf.d conf.modules.d logs modules run [root@test41 httpd]# cd conf [root@test41 conf]# cp httpd.conf /opt/httpd.conf.j2 [root@test41 conf]# cd /opt [root@test41 opt]# ls 1 httpd.conf.j2 test1.yaml test2.yaml test3.yaml [root@test41 opt]# vim httpd.conf.j2 42 Listen {{http_port}} 95 ServerName {{server_name}} 119 DocumentRoot "{{root_dir}}" [root@test41 opt]# vim test3.yaml #模板,对引用的配置文件初始化:templates模块,jinja组件,把编译过的模板文件传送给目标文件 - hosts: 192.168.65.10gather_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@test41 ansible]# ls ansible.cfg hosts roles roles:为了层次化,结构化的组织playbook,使用roles通过层次化自动装载变量,任务和处理器等等 roles把变量,任务和模块的文件单独放置在不同的中,通过roles一键编排 [root@test41 ansible]# mkdir /etc/ansible/roles/httpd/{files,templates,tasks,handlers,vars,defaults,meta} -p [root@test41 ansible]# mkdir /etc/ansible/roles/mysql/{files,templates,tasks,handlers,vars,defaults,meta} -p [root@test41 ansible]# mkdir /etc/ansible/roles/php/{files,templates,tasks,handlers,vars,defaults,meta} -p [root@test41 ansible]# yum -y install tree 已安装:tree.x86_64 0:1.6.0-10.el7 完毕!
roles: ├── httpd 角色名称 自定义│ ├── defaults 存放配置文件的目录,可以不写│ ├── files 存档copy模块或者script│ ├── handlers 存放处理器文件的目录│ ├── meta 保存角色源信息的文件│ ├── tasks 保存任务的文件│ ├── templates 保存模板的文件│ └── vars 保存变量的文件 就是把原来写在一个yanl的配置,分开---->不同目录---->保存在一个名字的yanl里面 执行的时候调用不同目录的同一个yaml文件 文件名必须是:main.yaml
ansible:14个模块必须掌握,熟练。
剧本:能够定义一般意义的远程部署和相关的配置即可
了解条件判断,循环,
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@test41 opt]# vim test1.yaml #在目标主机批量创建目录:/opt/test1 /opt/test2 /opt/test3 #从主机批量复制文件,123 456 789,分别输出到指定的123-->test1 456---->test2 789---->test3 指定主机为192.168.233.20. - name: this is ifhosts: allremote_user: roottasks:- name: chuangjianfile:path: "{{ item }}"state: directorywith_items: [/opt/test1,/opt/test2,/opt/test3]when: ansible_default_ipv4.address == "192.168.65.50"- name: fuzhicopy:src: "{{ item.src }}"dest: "{{ item.dest }}"loop:- { src: '123', dest: '/opt/test1' }- { src: '456', dest: '/opt/test2' }- { src: '789', dest: '/opt/test3' }when: ansible_default_ipv4.address == "192.168.65.50" #创建一个nginx.conf文件,改配置文件可以实现upstream反向代理 复制到nginx1- name: fuzhi nginx.confhosts: 192.168.65.50become: yestasks:- name: nginx.conf copy:src: nginx.confdest: /etc/nginx/nginx.confnotify:- restart nginxhandlers:- name: Restart nginxservice:name: nginxstate: started #分别在nginx2和nginx3上配置页面: test1 test2 - name: Nginx servershosts: alltasks:- name: yemian nginx1shell: echo "this is test1" > /usr/share/nginx/html/index.htmlwhen: ansible_default_ipv4.address == "192.168.65.20"- name: yemian nginx3shell: echo "this is test2" > /usr/share/nginx/html/index.htmlwhen: ansible_default_ipv4.address == "192.168.65.30" #在主机访问目标主机192.168.65.50,实现负载均衡。 [root@test50 nginx]# curl 192.168.65.50 this is test2 [root@test50 nginx]# curl 192.168.65.50 this is test1 [root@test50 nginx]# curl 192.168.65.50 this is test2 [root@test50 nginx]# curl 192.168.65.50 this is test2 [root@test50 nginx]# curl 192.168.65.50 this is test1 [root@test50 nginx]# curl 192.168.65.50 this is test1 [root@test50 nginx]# curl 192.168.65.50 this is test1
[root@test41 opt]# vim nginx.conf httpupstream xy102 {server 192.168.65.20;server 192.168.65.30;} server {listen 80; location / {root html;index index.html index.htm;proxy_pass http://xy102;}
1