想要在YAML中的多行文本中添加空格

时间:2019-04-11 23:05:26

标签: ansible yaml

目前,我有:

    - name: add function definition to file
      lineinfile:
        path: /Users/alexander/Documents/test_ac.txt
        # regexp: 'before_action :check_password_expiration'
        insertbefore: 'def redirect_back_or_default*'
        line: >
          def force_authenticated_user!(*args)
            if (!current_user) and (["/explore", "/help"].include?(request.path))
              redirect_to new_user_session_path and return
            end
          end

将哪个添加为


def force_authenticated_user!(*args)
  if (!current_user) and (["/explore", "/help"].include?(request.path))
    redirect_to new_user_session_path and return
  end
end

  def redirect_back_or_default(default: root_path, options: {})
    redirect_back(fallback_location: default, **options)
  end

  def not_found
    render_404
  end

我想对它进行格式化,以便函数定义正确地隔开,有人会知道吗?

1 个答案:

答案 0 :(得分:1)

来自http://yaml-multiline.info/

  

缩进指示符:通常,用于缩进块的空格数将自动从其第一行开始猜测。如果块的第一行以多余的空格开头,则可能需要block indentation indicator。在这种情况下,只需将用于缩进的空格数(1到9之间)放在标题的末尾。

因此,您应该可以:

- name: add function definition to file
  lineinfile:
    path: /Users/alexander/Documents/test_ac.txt
    # regexp: 'before_action :check_password_expiration'
    insertbefore: 'def redirect_back_or_default*'
    line: >2
        def force_authenticated_user!(*args)
          if (!current_user) and (["/explore", "/help"].include?(request.path))
            redirect_to new_user_session_path and return
          end
        end

即添加指示器,并将代码缩进x + 2个空格。