首页 > 解决方案 > 使用 with_items 时错误项未定义

问题描述

我正在尝试创建一个新的剧本,它应该在现有文件中添加一些行。不幸的是,我的剧本不起作用。

---
- 
  name: 'Playbook to update limits in 20-nproc.conf'
  remote_user: lwa
  strategy: free
  gather_facts: no
  become: yes
  hosts: "{{ target }}"

  tasks:
    -
      name: 'Updating limits for oracle user for Oracle DB installation'
      lineinfile:
        path: /etc/security/limits.d/20-nproc.conf
        line: '{{ item }}'
        state: present
        insertafter: EOF
        backup: yes
        create: no
        owner: root
        group: root
        mode: '644'
        with_items:
          - '#<domain>      <type>  <item>         <value>'
          - 'oracle         soft    nofile         4096'
          - 'oracle         hard    nofile         63536'
          - 'oracle         soft    nproc          2047'
          - 'oracle         hard    nproc          16384'
          - 'oracle         soft    stack          10240'
          - 'oracle         hard    stack          32768'

标签: ansible

解决方案


我终于明白了。我使用了 blockinfile 模块而不是 lineinfile。

---
- 
  name: 'Playbook to update limits in 20-nproc.conf'
  remote_user: lwa
  strategy: free
  gather_facts: no
  become: yes
  hosts: "{{ target }}"

  tasks:
    -
      name: 'Insert/Update limits for oracle user for Oracle DB installation'
      blockinfile:
        path: /etc/security/limits.d/20-nproc.conf
        block: |
          #<domain>      <type>  <item>         <value>
          oracle         soft    nofile         4096
          oracle         hard    nofile         63536
          oracle         soft    nproc          2047
          oracle         hard    nproc          16384
          oracle         soft    stack          10240
          oracle         hard    stack          32768
        state: present
        insertafter: EOF
        backup: yes
        create: no
        owner: root
        group: root
        mode: '644'


推荐阅读