• ansible之setup、条件判断、tags、循环handlers


    一、setup

    ansible all -m setup 查询出所有的信息

    过滤:ansible all -m setup -a "filter=ansible_os_family"

     1 ansible_all_ipv4_addresses # ipv4的所有地址
     2 ansible_all_ipv6_addresses # ipv6的所有地址
     3 ansible_date_time # 获取到控制节点时间
     4 ansible_default_ipv4 # 默认的ipv4地址
     5 ansible_distribution # 系统
     6 ansible_distribution_major_version # 系统的大版本
     7 ansible_distribution_version # 系统的版本号
     8 ansible_domain #系统所在的域
     9 ansible_env #系统的环境变量
    10 ansible_hostname #系统的主机名
    11 ansible_fqdn #系统的全名
    12 ansible_machine #系统的架构
    13 ansible_memory_mb #系统的内存信息
    14 ansible_os_family # 系统的家族
    15 ansible_pkg_mgr # 系统的包管理工具
    16 ansible_processor_cores #系统的cpu的核数(每颗)
    17 ansible_processor_count #系统cpu的颗数
    18 ansible_processor_vcpus #系统cpu的总个数=cpu的颗数*CPU的核数
    19 ansible_python # 系统上的python
    20 ansible cache -m setup -a 'filter=*processor*' # 用来搜索
    View Code

    二、条件判断

    1 - hosts: db
    2   remote_user: root
    3   tasks:
    4   - name: createfile
    5     copy: content="大弦嘈嘈如急雨" dest=/tmp/a.txt
    6     when: a=="3"
    7   - name: cratefile
    8     copy: content="小弦切切如私语" dest=/tmp/a.txt
    9     when: a=="4"

    三、tags 可以指定执行部分任务

     1 - hosts: web
     2   tasks:
     3   - name: installnginx
     4     yum: name=nginx
     5   - name: copyfile
     6     copy: src=/etc/nginx/nginx.conf dest=/etc/nginx/nginx.conf
     7     tags: copyfile
     8   - name: start
     9     service: name=nginx state=started
    10     

    四、循环 with_items

    1 - hosts: web
    2   tasks:
    3   - name: crateuser
    4     user: name={{item}}
    5     with_items:
    6     - user1
    7     - user2
    8     - user3

    嵌套循环

     1 - hosts: web
     2   tasks:
     3   - name: crategroup
     4     group: name={{item}}
     5     with_items:
     6     - wulaoshi30
     7     - wulaoshi31
     8     - wulaoshi32
     9   - name: createuser
    10     user: name={{item.name}} group={{item.group}} # 循环每个字典取值
    11     with_items:
    12     - {'name':alex40,'group':wulaoshi30} 
    13     - {'name':alex41,'group':wulaoshi31}
    14     - {'name':alex42,'group':wulaoshi32}

    template模块
    template模块和copy模块的区别

    • copy模块不替代参数

    • template模块替代参数

    1 - hosts: web
    2   tasks:
    3   - name: installredis
    4     yum: name=redis
    5   - name: copyfile
    6     template: src=redis.conf dest=/etc/redis.conf
    7   - name: start
    8     service: name=redis state=started

    在本机的redis的配置文件里bind 的ip随被控管机改变而改变

    bind {{ ansible_default_ipv4.address }}

     

  • 相关阅读:
    A bon chat, bon rat
    获取信息mysql
    Lua笔记3 表达式
    libevent2编译
    opencv环境搭建
    bash console
    Unix Notes.
    ubuntu vsftpd
    axis2客户端代码生成
    IDEA 快捷键
  • 原文地址:https://www.cnblogs.com/liaopeng123/p/10414510.html
Copyright © 2020-2023  润新知