从Ansible Role中的ssh命令行有条件时

时间:2019-07-24 13:10:28

标签: ansible

我是ansible的新手,所以我们将不胜感激。
在尝试将VMware工具推送到它们之前,我需要检查我的远程Centos服务器是否具有可写的/ boot。如果安装为只读,则安装将失败。如何为此原始Linux命令添加另一个WHEN?我知道是否必须使用寄存器或标准输出,但是我找不到示例来指导我。

RAW Linux Would be >  
 mount | grep boot

我需要抓住rw,目标不能像本例一样是ro

> 
/dev/sda1 on /boot type ext4 (ro,relatime,data=ordered)

我尝试在ansible文档中的块下添加任务。

- name: Catch Targets with read only boot

  tasks:

      - command: mount | grep boot
        register: boot_mode

      - shell: echo "motd contains the word hi"
        when: boot_mode.stdout.find('ro') != -1



---
- name: Wrapper for conditional tasks 
  block:  
  - name: Copy Files from Mirror to Remote Guest
    get_url:
      url: "{{ item }}"
      dest: /tmp
      owner: root
      group: root 
    with_items: 
      - http://mirror.compuscan.co.za/repo/vmwaretools65u2/CentOS7/VMwareTools-10.3.5-10430147.tar.gz

  - name: UnTAR the installer 
    unarchive:
      src: /tmp/VMwareTools-10.3.5-10430147.tar.gz
      dest: /tmp
      remote_src: yes

  - name: Run the PL install
    become: yes
    command: /tmp/vmware-tools-distrib/vmware-install.pl -d

  - name: Perform Clean Up 
    file:
      state: absent
      path: "{{ item }}"
    with_items: 
      - /tmp/vmware-tools-distrib/
      - /tmp/VMwareTools-10.3.5-10430147.tar.gz

  - name: Report on success or failure
    service:  
      name: vmware-tools
      state: started
      enabled: yes 

  when: ansible_distribution == 'CentOS' and ansible_distribution_major_version == '7'
  ignore_errors: yes

我希望角色/剧本在只读/ boot模式下忽略目标。

1 个答案:

答案 0 :(得分:3)

stat任务放在块的前面

- stat:
    path: /boot
  register: boot_mode

然后添加条件,以在/ boot可写的情况下执行该块

  when:
    - boot_mode.stat.writeable
    - ansible_distribution == 'CentOS'
    - ansible_distribution_major_version == '7'