1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Ansible PostgreSQL Serverに指定したdatabaseがあるかを判定

Posted at

環境

$ cat /etc/redhat-release
CentOS Linux release 7.7.1908 (Core)

$ ansible --version
ansible 2.9.7
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/home/ansi/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/site-packages/ansible
  executable location = /bin/ansible
  python version = 2.7.5 (default, Aug  7 2019, 00:51:29) [GCC 4.8.5 20150623 (Red Hat 4.8.5-39)]

事前準備

Ansibleで操作する対象サーバ(ノード)にpython-psycopg2が必要なのでpostgresqlxx-serverとかインストールするときに一緒にインストールしておくこと!
postgresql_queryを実行するために必要

忙しい人のための答え

main.yml
- name: is exists database postgres
  postgresql_query:
    db: "postgres"
    login_user: "postgres"
    login_password: "{{ user_passwd_postgres_db }}"
    port: "5432"
    query: |
      select 1 as exists from pg_database where datname = 'postgres'
  register: query_res

- debug:
    msg: "{{ query_res.rowcount }}"

解説

postgresql_queryの結果をregisterすると以下のようになる

main.yml
- name: get database list
  postgresql_query:
    db: "postgres"
    login_user: "postgres"
    login_password: "{{ user_passwd_postgres_db }}"
    port: "5432"
    query: |
      select 1 as exists from pg_database where datname = 'postgres'
  register: query_res

- name: debug
  debug:
    msg: "{{ query_res }}"
TASK [test : get database list] ***********************
ok: [192.168.56.104]

TASK [test : debug] ***********************************
ok: [192.168.56.104] => {
    "msg": {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/bin/python"
        },
        "changed": false,
        "failed": false,
        "query": "select 1 as exists from pg_database where datname = 'postgres'\n",
        "query_result": [
            {
                "exists": 1
            }
        ],
        "rowcount": 1,
        "statusmessage": "SELECT 1"
    }
}

私の場合はquery_resに格納したので、query_res.query_resultで結果にアクセス可能。
これは配列であり、今回の結果は1行しかないことが自明なので[0]で1要素目にアクセス。ありがたい事に結果はjsonだ。

ただこれだとSELECTの結果が0件の場合はエラーになるのでquery_res.rowcountが正解。

存在しないDBを検索した場合は以下のとおり。

TASK [test : get database list] ***********************
ok: [192.168.56.104]

TASK [test : debug] ***********************************
ok: [192.168.56.104] => {
    "msg": {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/bin/python"
        },
        "changed": false,
        "failed": false,
        "query": "select 1 as exists from pg_database where datname = 'hoge'\n",
        "query_result": [],
        "rowcount": 0,
        "statusmessage": "SELECT 0"
    }
}

この場合は"query_result" : []になってしまうので、rowcount見るほうがいい。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?