【问题标题】:Unable to execute postgres sql script from ansible无法从ansible执行postgres sql脚本
【发布时间】:2017-07-07 15:33:00
【问题描述】:

我想执行一个从我的 git 资源中获取的 postgresql sql。 我想首先从 git 存储库中获取所有脚本,然后想从特定目录执行脚本,我将其作为 ansible 脚本的输入。这是我完整的 ansible 脚本

- hosts: "{{ HOST }}"
  become: true
  become_user: "admin"
  become_method: su
  gather_facts: true
  vars:
    app_path: "{{ app_path }}"
    app_dir: "{{ app_dir }}"

  tasks:
    - stat:
        path: "{{ app_path }}/{{ app_dir }}"
      register: exist

    - name: create "{{ app_path }}/{{ app_dir }}" directory
      file: dest="{{ app_path }}/{{ app_dir }}" state=directory 
      when: exist.stat.exists != True


    - git: 
        repo: http://git-url/postgres-demo.git
        dest: "{{ app_path }}/{{ app_dir }}"
        version: "{{ GIT_TAG }}"
        refspec: '+refs/heads/{{ GIT_TAG }}:refs/remotes/origin/{{ GIT_TAG }}'
        update: yes
        force: true
      register: cloned  

    - name: Execute postgres dump files
      shell: "/usr/bin/psql -qAtw -f  {{ item }}"
      with_fileglob:
          - "{{ app_path }}/{{ app_dir }}/{{ scripts_dir }}/*"
      register: postgres_sql
      become: true
      become_user: "postgres"
      become_method: su

上述脚本已成功执行,但 postgres 步骤在减弱后抛出了我:

[WARNING]: Unable to find '/home/admin/postgres-dev/test' in expected paths.

当我检查我的 postgresql 数据库时,我没有找到要使用此脚本创建的表。

【问题讨论】:

    标签: postgresql ansible


    【解决方案1】:

    Ansible 中的所有查找都在 本地 ansible 主机上执行。

    所以:

    with_fileglob:
      - "{{ app_path }}/{{ app_dir }}/{{ scripts_dir }}/*"
    

    使用fileglob 查找,它在本地主机上搜索文件!

    您应该重构此代码以首先使用find 模块来查找所有必需的脚本,注册其输出,然后使用with_items 在注册的变量上循环以执行脚本。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多