ansible条件不匹配第一个条件

时间:2018-05-23 10:53:20

标签: ansible

我是安赛的首发。我必须自动化需要检查两件事的安装

1)是否是redhat系统(如果没有则退出) 2)如果1)为真,则检查RAM是否大于8GB(如果没有则打印消息并退出)

这是我的剧本

 - name: getting RAM details
   shell: free -m | grep Mem | awk  '{ print $2 }'
   register: ram
   when: ansible_distribution == "RedHat"

 - name: Checking OS
   debug: msg="current OS is {{ ansible_distribution }} which is not 
   supported. Should be redhat"
   when: (ansible_distribution!="RedHat")

 - name: Checking RAM
   debug: msg="current RAM is {{ ram.stdout }} GB. Should be 8 GB"
   when: (ram.stdout | int < 7698 )

问题是第一个场景匹配时游戏没有退出。它仍然是第二种情况。我是ansible世界的新手。

3 个答案:

答案 0 :(得分:2)

使用assert module(或fail module)代替debug

- name: Checking OS
  assert:
    msg: "current OS is {{ ansible_distribution }} which is not supported. Should be redhat"
    that: "ansible_distribution == 'RedHat'"

- name: Checking RAM
  assert:
    msg: "current RAM is {{ ram.stdout }} GB. Should be 8 GB"
    that: "ram.stdout | int > 7698"

答案 1 :(得分:0)

任务结构存在问题,请参阅下面的内容:

- name: getting RAM details
  shell: free -m | grep Mem | awk  '{ print $2 }'
  register: ram
  when: ansible_distribution == "RedHat"

- name: Checking OS
  debug: msg="current OS is {{ ansible_distribution }} which is not supported. Should be redhat"
  when: (ansible_distribution!="RedHat")

- name: Checking RAM
  debug: msg="current RAM is {{ ram.stdout }} GB. Should be 8 GB"
  when: (ram.stdout | int < 7698 )

关于退出,ansible和when条件的想法是它将处理所有任务,并将跳过when条件不正确的任务。上面的代码中没有“退出”方式。

我理解你想要实现的目标,但它不会那样工作。条件importing tasks可能会对when条件有用,例如:

- include_tasks: "{{ hostvar }}.yml"
  when: hostvar is defined

希望有所帮助

答案 2 :(得分:0)

当满足这两个条件中的任何一个时,您可以使用meta: end_play来停止播放执行。

- name: getting RAM details
  shell: free -m | grep Mem | awk  '{ print $2 }'
  register: ram
  when: ansible_distribution == "RedHat"

- name: abort if not Redhat or RAM less than 8G
  meta: end_play
  when: (ansible_distribution!="RedHat") or (ram.stdout | int < 7698 )