blockinline模块不尊重insertafter

时间:2018-11-20 17:24:02

标签: ansible

我正在使用一个任务在〜/ .bash_profile中添加环境变量。第一个任务是添加java,ant和oracle home,然后添加PATH,然后添加不属于PATH的其余变量。

添加房屋的第一个任务:

func createLightBulbNode() -> SCNNode {
    let lightBulbNode = collada2SCNNode(filepath: "art.scnassets/cubeSolid.dae")

    lightBulbNode.position = SCNVector3(0, -0.5, 0)

    return lightBulbNode
}

func collada2SCNNode(filepath:String) -> SCNNode {
    let node = SCNNode()
    let scene = SCNScene(named: filepath, inDirectory: nil, options: [SCNSceneSource.LoadingOption.animationImportPolicy: SCNSceneSource.AnimationImportPolicy.doNotPlay])
    let nodeArray = scene!.rootNode.childNodes
    for childNode in nodeArray {
        node.addChildNode(childNode as SCNNode)
    }
    return node
}

func node(for annotation: Annotation) -> SCNNode? {    
    if annotation.calloutImage == nil {
        // Comment `createLightBulbNode` and add `return nil` to use the default node
        return createLightBulbNode()
    } else {
        let firstColor = UIColor(red: 0.0, green: 99/255.0, blue: 175/255.0, alpha: 1.0)
        return createSphereNode(with: 0.5, firstColor: firstColor, secondColor: UIColor.green)
    }
}

它将按预期部署变量,并在bash文件中已有的注释行之后插入

第二项任务专用于PATH,我在其中创建该行,然后删除原始PATH

- name: Add environment variables in bash_profile
  become: yes
  blockinfile:
    path: "{{ bash_profile }}"
    insertafter: "^# User specific environment and startup programs"
    content: export {{ item.env_name }}={{ item.env_dir }}
    marker: ""
    state: present
  with_items:
  - { env_name: "{{ ant_home }}", env_dir: "{{ ant_home_dir }}" }
  - { env_name: "{{ java_home }}", env_dir: "{{ java_home_dir }}" }
  - { env_name: "{{ oracle_home }}", env_dir: "{{ oracle_home_dir }}" }
  - { env_name: "{{ lib_path_home }}", env_dir: "{{ lib_path_home_dir }}" }

这将获得PATH行,添加声明的房屋和垃圾箱,并删除原始路径,因为它不会在原始路径之前添加任何内容,而是创建第二行

最后一项任务是我遇到了blockinfile模块的问题,特别是在- name: Add PATH variable become: yes lineinfile: path: "{{ bash_profile }}" regexp: '^\PATH=$PATH:$HOME/.local/bin:$HOME/bin' line: 'PATH=$PATH:$HOME/.local/bin:$HOME/bin:{{ ant_path_dir }}:{{ java_path_dir }}:{{ oracle_path_dir }}' - name: remove PATH variable become: yes lineinfile: path: "{{ bash_profile }}" line: 'PATH=$PATH:$HOME/.local/bin:$HOME/bin' state: absent 参数中

insertafter

通过这3次任务,我需要编辑bash_profile以添加所有需要的必需变量,并且文件的最后一个方面是:

- name: Add the rest of the environment variables in bash_profile
  blockinfile:
    path: "{{ bash_profile }}"
    insertafter: "^PATH=$PATH:$HOME/.local/bin:$HOME/bin:$ANT_HOME/bin:$JAVA_HOME/bin:$ORACLE_HOME/bin"
    content: export {{ item.env_name_1 }}={{ item.env_dir_1 }}
    marker: ""
  with_items:
  - { env_name_1: "{{ dynamo_home }}", env_dir_1: "{{ dynamo_home_dir }}" }
  - { env_name_1: "{{ atgjre_home }}", env_dir_1: "{{ atgjre_home_dir }}" }
  - { env_name_1: "{{ weblogic_home }}", env_dir_1: "{{ weblogic_home_dir }}" }
  - { env_name_1: "{{ node_env_home }}", env_dir_1: "{{ node_env_home_dir }}" }
  - { env_name_1: "{{ endeca_home }}", env_dir_1: "{{ endeca_home_dir }}" }

实际结果是:

ANT_HOME
JAVA_HOME
ORACLE_HOME

PATH:

REST OF THE HOMES

1 个答案:

答案 0 :(得分:1)

我建议进行一些更改:

1)使用lineinfile而不是blockinfile,因为您并未完全插入文本块。您不需要为最终任务使用insertafter,因为此模块的默认行为是追加。

2)使用regexp选项。这样可以确保幂等,即,如果所需的行存在,则不会更改或在重新运行播放时添加这些行。您无需担心bash_profile被大量条目污染。

3)对于任务2(“添加PATH变量”),添加insertbefore选项。只是要确保您在export PATH语句之前有新路径。

有关lininfile ansible文档,请参见:https://docs.ansible.com/ansible/latest/modules/lineinfile_module.html?highlight=lineinfile

这是重构的游戏

---
- name: Bash profile play
  hosts: 127.0.0.1
  connection: local
  become_user: root
  become: yes
  tasks:
    - name: Add environment variables in bash_profile
      become: yes
      lineinfile:
        path: "{{ bash_profile }}"
        insertafter: "^# User specific environment and startup programs"
        regexp: "^export {{ item.env_name }}={{ item.env_dir }}"
        line: export {{ item.env_name }}={{ item.env_dir }}
        state: present
      with_items:
        - { env_name: "{{ java_home }}", env_dir: "{{ java_home_dir }}" }
        - { env_name: "{{ maven_home }}", env_dir: "{{ maven_home_dir }}" }

    - name: Add PATH variable
      become: yes
      lineinfile:
        path: "{{ bash_profile }}"
        insertbefore: "^export PATH"
        regexp: '^PATH=.+'
        line: 'PATH=$PATH:$HOME/bin:{{ java_path_dir }}:{{ maven_path_dir }}'

    - name: Add the rest of the environment variables in bash_profile
      lineinfile:
        path: "{{ bash_profile }}"
        regexp: "^export {{ item.env_name_1 }}={{ item.env_dir_1 }}"
        line: export {{ item.env_name_1 }}={{ item.env_dir_1 }}
        state: present
      with_items:
        - { env_name_1: "{{ dynamo_home }}", env_dir_1: "{{ dynamo_home_dir }}" }
        - { env_name_1: "{{ atgjre_home }}", env_dir_1: "{{ atgjre_home_dir }}" }
...