ansible模块

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

复制代码
[root@test41 opt]# vim test1.yaml
#标签的类型
#always:设定标签名为always,除非指定跳过这个标签,否则该任务始终会运行,即使指定了标签还会运行
#never:从不运行的任务,指定标签名never可以运行
#debug:用于调试
#setup:收集主机的信息
#标签名也可以自定义:tags
​
- hosts: 192.168.65.10
  gather_facts: false
  tasks:
    - name: debug-test1
      debug:
        msg: 'cow'
      tags:
        - debug
​
    - name: always-test1
      debug:
        msg: 'ALWAYS-RUN'
      tags:
        - always
​
    - name: never-test1
      debug:
        msg: "Never-run"
      tags:
        - never
​
    - name: setup-test1
      debug:
        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.10
  remote_user: root
  tasks:
    - name: 复制文件
      copy: src=/etc/hosts dest=/opt/hosts
      tags:
        - cyp
    - name: touch file
      file: path=/opt/test1.txt state=touch
      tags:
        - 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.10
  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@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 if
  hosts: all
  remote_user: root
  tasks:
    - name: chuangjian
      file:
        path: "{{ item }}"
        state: directory
      with_items: [/opt/test1,/opt/test2,/opt/test3]
      when: ansible_default_ipv4.address == "192.168.65.50"
    - name: fuzhi
      copy:
        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.conf
  hosts: 192.168.65.50
  become: yes
  tasks:
    - name: nginx.conf 
      copy:
        src: nginx.conf
        dest: /etc/nginx/nginx.conf
      notify:
        - restart nginx
  handlers:
    - name: Restart nginx
      service:
        name: nginx
        state: started
     
​
#分别在nginx2和nginx3上配置页面: test1 test2
- name: Nginx servers
  hosts: all
  tasks:
    - name: yemian nginx1
      shell: echo "this is test1" > /usr/share/nginx/html/index.html
      when: ansible_default_ipv4.address == "192.168.65.20"
    - name: yemian nginx3
      shell: echo "this is test2" > /usr/share/nginx/html/index.html
      when: 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
http
    upstream 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

相关推荐
乘云数字DATABUFF4 小时前
5分钟部署开源APM Databuff:OpenTelemetry全链路追踪入门实战
运维·后端
悠然南风2 天前
Ansible常见模块总结及LDAP Role 编写与调试
ansible
荣--2 天前
一键部署不是为了省时间 —— 它是把"买来的 PaaS"变成"自己的平台"的拐点
运维·zabbix·工程化·一键部署·平台化·边界设计
江华森2 天前
动手实战学 Docker — 从零到集群编排完全指南
运维
Avan_菜菜3 天前
FRP 内网穿透完整实战:从 HTTP 映射到 HTTPS 自签代理
运维·nginx·https
SelectDB4 天前
Litefuse 开源并推出单进程轻量模式,25 秒就能跑起来的 Agent 可观测与评估平台
运维·后端·自动化运维
XIAOHEZIcode5 天前
Linux系统鼠标偏移常见原因以及修复方案
linux·运维·游戏
用户0328472220706 天前
如何搭建本地yum源(上)
运维
大树889 天前
金刚石散热越强,管路越先见顶
大数据·运维·服务器·人工智能·ai
摇滚侠9 天前
Linux CentOS7 rpm 安装 MySQL 5.7
linux·运维·mysql