先介绍一下 gather_facts 参数,该参数用于指定在执行任务前,是否先执行 setup 模块获取主机相关信息,以便给后面的任务使用

[root@localhost ~]# ansible 192.168.119.134 -m setup    # 查看主机的facter信息
192.168.119.134 | SUCCESS => {
    "ansible_facts": {
        "ansible_all_ipv4_addresses": [
            "192.168.119.134"
......

条件判断语句写法:

[root@localhost ~]$ cat when.yml 
---
- hosts: 192.168.119.134
  user: root
  gather_facts: True                            # 必须先开启这个
  tasks:
    - name: use when
      shell: touch /tmp/when.txt
      when: ansible_date_time.year == "2019"    # 表示当 ansible_date_time.year 的值等于 2019 时,就使用 shell 模块执行 touch /tmp/when.txt 命令

 

 

 

 

 

     

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-02-17
  • 2022-12-23
  • 2022-12-23
  • 2021-09-06
  • 2021-05-17
猜你喜欢
  • 2022-01-12
  • 2021-08-24
  • 2022-12-23
  • 2021-12-24
  • 2021-12-03
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案