tags模块:

tags模块:

可以给任务定义标签,可以根据标签来运行指定的任务。

#标签的类型
#always:设定了标签名为always,除非指定跳过这个标签,否则该任务将始终会运行,即使指定了标签还会运行
#never:始终不运行的任务,指定标签名never可以运行。
#debug:用于调试
#setup:收集主机的信息
#标签名也可以自定义
[root@test7 opt]# vim test7.yaml
- hosts: 192.168.60.80
  gather_facts: false
  tasks:
    - name: debug-test1
      debug:
        msg: "cow"
      tags:
        - debug
    - name: always-test1
      debug:
        msg: "ALWAYS-RUN"
      tags:
        - always
    - name: setup-test1
      debug:
        msg: "SETUP-test1"
      tags:
        - setup
    - name: never-test1
      debug:
        msg: "Never-run"
      tags:
        - never
#指定标签执行
[root@test7 opt]# ansible-playbook test7.yaml --tags=setup
#跳过指定标签执行
[root@test7 opt]# ansible-playbook test7.yaml --skip-tags=always
#指定执行多个标签,always不指定也会执行
[root@test7 opt]# ansible-playbook test7.yaml --tags="debug","setup"
自定义标签
[root@test7 opt]# vim test8.yaml
​
- hosts: 192.168.60.90
  gather_facts: false
  remote_user: root
  tasks:
    - name: fuzhiwenjian
      copy: src=/etc/hosts dest=/opt/hosts
      tags:
        - zlm
    - name: touch file
      file:
        path: "/opt/test1.txt"
        state: touch
      tags:
        - hpc
[root@test7 opt]# ansible-playbook test8.yaml
#模板,对应用的配置文件初始化:templates模块,Jinja组件,把编译过的模板文件传送给目标文件。
[root@test7 opt]# yum -y install httpd
[root@test7 opt]# cd /etc/httpd/conf
[root@test7 conf]# ls
httpd.conf  magic
[root@test7 conf]# cp httpd.conf /opt/httpd.conf.j2
[root@test7 conf]# cd /opt/
[root@test7 opt]# vim httpd.conf.j2
 
 42 Listen {{http_port}}
 95 ServerName {{server_name}}
119 DocumentRoot "{{root_dir}}"
​
[root@test7 opt]# vim /etc/ansible/hosts
192.168.60.90 http_port=192.168.60.90:80 server_name=www.xy.com:80 root_dir=/etc/httpd/htdocs
​
[root@test7 opt]# vim test9.yaml
- hosts: 192.168.60.90
  gather_facts: false
  remote_user: root
  vars:
    - pg: httpd
    - sv: httpd
  tasks:
    - name: install httpd
      yum: name={{pg}}
    - name: editon conf
      template: src=/opt/httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf
      notify:
        - restart httpd
  handlers:
    - name: restart httpd
      service: name={{sv}} state=restarted
[root@test7 opt]# ansible-playbook test9.yaml 
​

roles模块(角色):

ansible为了层次化,结构化的组织playbook,使用rols(角色)通过层次化自动装载变量,任务和处理器等等

roles把变量,任务和模块的文件单独放置在各个不同目录的中,通过rolse一键的编排。

[root@test7 ansible]# mkdir /etc/ansible/roles/httpd/{files,templates,tasks,handlers,vars,defaults,meta} -p
[root@test7 ansible]# mkdir /etc/ansible/roles/mysql/{files,templates,tasks,handlers,vars,defaults,meta} -p
[root@test7 ansible]# mkdir /etc/ansible/roles/php/{files,templates,tasks,handlers,vars,defaults,meta} -p
[root@test7 ansible]# yum -y install tree
.
├── httpd                   角色名称   自定义
│   ├── defaults            存放配置文件的目录,可以不写
│   ├── files               存放copy模块或者script
│   ├── handlers            存放处理器文件的目录
│   ├── meta                保存角色信息的文件
│   ├── tasks               保存任务的文件
│   ├── templates           保存模板的文件
│   └── vars                保存变量的文件

就是把原来写一个yaml的配置,分开————>不同的目录————>保存在一个名字的yanl里面

执行的时候调用不同目录的一个同一个yaml文件。

main.yaml

总结:

ansible:14个模块必须掌握,熟练

剧本:能够定义一般意义的远程部署和相关的配置即可

了解条件判断,循环

tags的作用 标签的 系统标签:always never 自定义

tomplates:了解即可

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远程完成!

- name: this is if
  hosts: all
  remote_user: root
  tasks:
    - name: test when
      file:
        path: "{{ item }}"
        state: directory
      with_items: [/opt/test1,/opt/test2,/opt/test3]
      when: ansible_default_ipv4.address == "192.168.60.90"
    - name: copy_wenjian
      copy:
        src: "{{ item.src }}"
        dest: "{{ item.dest }}"
      with_items:
        - { src: '/opt/test/123', dest: '/opt/test1' }
        - { src: '/opt/test/456', dest: '/opt/test2' }
        - { src: '/opt/test/789', dest: '/opt/test3' }
      when: ansible_default_ipv4.address == "192.168.60.90"
    - name: install nginx
      yum: name=nginx state=latest
    - name: nginx.conf
      copy: 'src=/opt/nginx.conf dest=/etc/nginx/nginx.conf'
      when: ansible_default_ipv4.address == "192.168.60.80"
      notify: start nginx
    - name: interview
      shell: echo "this is test1" > /usr/share/nginx/html/index.html
      when: ansible_default_ipv4.address == "192.168.60.60"
      notify: start nginx
    - name: interview
      shell: echo "this is test2" > /usr/share/nginx/html/index.html
      when: ansible_default_ipv4.address == "192.168.60.90"
      notify: start nginx
  handlers:
    - name: start nginx
      service: name=nginx state=started
​
​
[root@test7 opt]# vim nginx.conf 
 37      upstream xy102 {
 38         server 192.168.60.60;
 39         server 192.168.60.90;
 40         }
 
 50         location / {
 51             root   html;
 52             index  index.html index.htm;
 53         proxy_pass http://xy102;
 54         }
​
[root@test7 opt]# curl 192.168.60.80
this is test1
[root@test7 opt]# curl 192.168.60.80
this is test2
[root@test7 opt]# curl 192.168.60.80
this is test1
[root@test7 opt]# curl 192.168.60.80
this is test2
[root@test7 opt]# curl 192.168.60.80
this is test1
[root@test7 opt]# curl 192.168.60.80
this is test2
  • 9
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值