playbook条件判断
案例1:
1,打开pbook.yml:
vim pbook.yml
2.写入以下文本
- hosts: web #指定hosts文件中web组所有机器
remote_user: root #远程主机以root用户执行
tasks:
- name: createfile #任务名随便
copy: content="i an your father" dest=/tmp/baba.txt # 将i an your father写入web组所有主机的/tmp/baba.txt里面
when: num=="1" # 这里的num是变量,一会执行文件从外面传入
- name: createfile
copy: content="i am your mather" dest=/tmp/mama.txt
when: num=="2" #这里的num是变量,一会执行文件从外面传入
保存退出
3,执行文件
ansible-playbook -e num=1 pbook.yml
案例2:
首先了解一个获取主机信息的指令,setup
ansible web -m setup #获取host文件中web组所有主机的详细信息
常用参数:
其他相关参数自行用以上命令获取查看
ansible_all_ipv4_addresses 所有的ipv4地址
ansible_all_ipv6_addresses 所有的ipv6地址
ansible_architecture 系统的架构
ansible_date_time 系统时间
ansible_default_ipv4 系统的默认ipv4地址
ansible_distribution 系统名称
ansible_distribution_file_variety 系统的家族
ansible_distribution_major_version 系统的版本
ansible_domain 系统所在的域
ansible_fqdn 系统的主机名
ansible_hostname 系统的主机名,简写
ansible_os_family 系统的家族
ansible_processor_cores cpu的核数
ansible_processor_count cpu的颗数
ansible_processor_vcpus cpu的个数
下面开始看案例:
1,打开pbook.yml文件
vim pbook.yml
2.写入:
- hosts: web #指定组
remote_user: root #远程主机执行榕湖
tasks:
- name: createfile #任务名
copy: content="i an your father" dest=/tmp/baba.txt
when: ansible_processor_vcpus==2 #ansible_processor_vcpus这个参数会自动读取远程主机相关信息,其他可选信息见本案例开头
保存退出
3,执行
ansible-playbook pbook.yml