【问题标题】:Error while using handlers in Ansible在 Ansible 中使用处理程序时出错
【发布时间】:2017-09-02 06:10:46
【问题描述】:
---
- name: Oracle DB Prereqs
  hosts: sandbox
  become: true
  gather_facts: yes
  vars:
      shmall_ent: 16384
      shmall_mid: 32768
      shmall_lar: 65536
      shmall_exlar: 262144

   tasks:
    - name: SHMALL value to set for memory size less than 16G
      when: ansible_memtotal_mb <= {{ shmall_ent }}
      notify:
        - SHMALL ent

    - name: SHMALL value to set for memory size between 16G and 32G
      when: (ansible_memtotal_mb > {{ shmall_ent }} and ansible_memtotal_mb <= {{ shmall_mid }})|int
      notify:
        - SHMALL mid

    - name: SHMALL value to set for memory size between 32G and 64G
      when: (ansible_memtotal_mb > {{ shmall_mid }} and ansible_memtotal_mb <= {{ shmall_lar }})|int
      notify:
        - SHMALL lar

    - name: SHMALL value to set for memory size between 64G and 256G
      when: (ansible_memtotal_mb > {{ shmall_lar }} and ansible_memtotal_mb <= {{ shmall_exlar }})|int
      notify:
        - SHMALL exlar
      handlers:
    - name: SHMALL ent
      sysctl:
        name: kernel.shmall
        value: 3670016
        sysctl_file: /etc/sysctl.d/99-sysctl.conf
    - name: SHMALL mid
      sysctl:
       name: kernel.shmall
       value: 7340032
       sysctl_file: /etc/sysctl.d/99-sysctl.conf
    - name: SHMALL lar
      sysctl:
       name: kernel.shmall
       value: 14680064
       sysctl_file: /etc/sysctl.d/99-sysctl.conf
    - name: SHMALL exlar
      sysctl:
       name: kernel.shmall
       value: 57671680
       sysctl_file: /etc/sysctl.d/99-sysctl.conf

运行播放时出错是:

ERROR! Syntax Error while loading YAML.


The error appears to have been in '/home/rjoy/ansible/roles/oracle/playbook/oraunix.yml': line 12, column 4, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:


   tasks:
   ^ here

【问题讨论】:

    标签: linux unix ansible redhat devops


    【解决方案1】:

    Yaml 的大多数语法错误是因为缩进不正确,因为 Yaml 依赖于空格。

    请参阅playbook documentation 中的此示例:

    ---
    - hosts: webservers
      vars:
        http_port: 80
        max_clients: 200
      remote_user: root
      tasks:
      - name: ensure apache is at the latest version
        yum: name=httpd state=latest
      - name: write the apache config file
        template: src=/srv/httpd.j2 dest=/etc/httpd.conf
        notify:
        - restart apache
      - name: ensure apache is running (and enable it at boot)
        service: name=httpd state=started enabled=yes
      handlers:
        - name: restart apache
          service: name=httpd state=restarted
    

    注意tasks 如何缩进到与hostsvarsremote_user 相同的级别?

    还可以查看 tasks 数组中的项目如何将破折号 (-) 缩进到与单词“tasks”相同的级别。

    这种缩进很重要,因为这就是 Yaml 确定哪些元素属于其他元素的方式。如果您没有正确缩进所有内容,则它不知道如何解析它。

    所以这应该适合你

    ---
    - name: Oracle DB Prereqs
      hosts: sandbox
      become: true
      gather_facts: yes
      vars:
        shmall_ent: 16384
        shmall_mid: 32768
        shmall_lar: 65536
        shmall_exlar: 262144
    
      tasks:
      - name: SHMALL value to set for memory size less than 16G
        when: ansible_memtotal_mb <= {{ shmall_ent }}
        notify:
          - SHMALL ent
    
      - name: SHMALL value to set for memory size between 16G and 32G
        when: (ansible_memtotal_mb > {{ shmall_ent }} and ansible_memtotal_mb <= {{ shmall_mid }})|int
        notify:
          - SHMALL mid
    
      - name: SHMALL value to set for memory size between 32G and 64G
        when: (ansible_memtotal_mb > {{ shmall_mid }} and ansible_memtotal_mb <= {{ shmall_lar }})|int
        notify:
          - SHMALL lar
    
      - name: SHMALL value to set for memory size between 64G and 256G
        when: (ansible_memtotal_mb > {{ shmall_lar }} and ansible_memtotal_mb <= {{ shmall_exlar }})|int
        notify:
          - SHMALL exlar
    
      handlers:
        - name: SHMALL ent
          sysctl:
            name: kernel.shmall
            value: 3670016
            sysctl_file: /etc/sysctl.d/99-sysctl.conf
    
        - name: SHMALL mid
          sysctl:
            name: kernel.shmall
            value: 7340032
            sysctl_file: /etc/sysctl.d/99-sysctl.conf
    
        - name: SHMALL lar
          sysctl:
            name: kernel.shmall
            value: 14680064
            sysctl_file: /etc/sysctl.d/99-sysctl.conf
    
        - name: SHMALL exlar
          sysctl:
            name: kernel.shmall
            value: 57671680
            sysctl_file: /etc/sysctl.d/99-sysctl.conf
    

    【讨论】:

    • 谢谢。你能帮我优化我上面提到的脚本吗?
    • 查看我的编辑。您所要做的就是在几行的开头删除几个额外的空格。
    • 脚本运行良好。现在我需要使用相同的条件语句添加 kernel.shmmax 值,它会在这些条件下与 sysctl 文件中的多个条目一起使用吗? @Yep_It's_Me
    • 对不起@Glaldiator 我不知道。我以前没有对 sysctl 文件做过任何工作。试一试,如果它给你一个错误的搜索 SO :)
    猜你喜欢
    • 1970-01-01
    • 2023-02-21
    • 1970-01-01
    • 2022-11-30
    • 1970-01-01
    • 2017-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多