ansible之模块 条件判断

一 setup
输入命令 ansible all -m setup

#setup的一些参数
ansible_all_ipv4_addresses # ipv4的所有地址
ansible_all_ipv6_addresses # ipv6的所有地址
ansible_date_time # 获取到控制节点时间
ansible_default_ipv4 # 默认的ipv4地址
ansible_distribution # 系统
ansible_distribution_major_version # 系统的大版本
ansible_distribution_version # 系统的版本号
ansible_domain #系统所在的域
ansible_env #系统的环境变量
ansible_hostname #系统的主机名
ansible_fqdn #系统的全名
ansible_machine #系统的架构
ansible_memory_mb #系统的内存信息
ansible_os_family # 系统的家族
ansible_pkg_mgr # 系统的包管理工具
ansible_processor_cores #系统的cpu的核数(每颗)
ansible_processor_count #系统cpu的颗数
ansible_processor_vcpus #系统cpu的总个数=cpu的颗数*CPU的核数
ansible_python # 系统上的python
ansible cache -m setup -a 'filter=*processor*' # 用来搜索

 

 

二 条件判断

不同条件  不同版本   不同环境   不同用户

boo4.yml

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


运行: ansible-playbook -e 'a=="3"' book4.yml
注意 -e 代表传参

 

三 tags

book5.yml

- hosts: web
  tasks:
  - name: installnginx
    yum: name=nginx
  - name: copyfile
    copy: src=/etc/nginx/nginx.conf dest=/etc/nginx/nginx.conf
    tags: copyfile
  - name: start
    service: name=nginx state=started


运行时:
ansible-playbook -t copyfile book5.yml 

 输入 ansible-playbook 可以查看它的参数

 

四 循环with_item

一次创建多个

book7.yml

- hosts: web
  tasks:
  - name: crateuser
    user: name={{item}}
    with_items:
    - mlh20
    - mlh21
    - mlh22


 ansible-playbook book7.yml 执行

 

book7.yml

- hosts: web
  tasks:
  - name: crateuser
    user: name={{item}}
    with_items:
    - alex30
    - alex31
    - alex32
  - name: crategroup
    group: name={{item}}
    with_items:
    - wulaoshi20
    - wulaoshi21
    - wulaoshi22

 

转载于:https://www.cnblogs.com/mlhz/p/10415642.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值