コンテンツへスキップ

ansible

ファイル

root@localhost myProject]# tree
.
|-- ansible.cfg
|-- group_vars
|   `-- db.yml
|-- hosts
|-- roles
|   |-- postgresql
|   |   |-- defaults
|   |   |   `-- main.yml
|   |   |-- handlers
|   |   |   `-- main.yml
|   |   |-- tasks
|   |   |   |-- configurations.yml
|   |   |   |-- createDbCluster.yml
|   |   |   |-- main.yml
|   |   |   |-- packages.yml
|   |   |   `-- services.yml
|   |   `-- templates
|   |       `-- postgresql.conf.j2
|   `-- test1
|       `-- tasks
|           |-- hashTest.yml
|           `-- main.yml
`-- site.yml

9 directories, 14 files

db.yml

[root@localhost myProject]# more group_vars/db.yml
---
postgresql_version: "12"

dbs:
  db1:
    dataDirectory: /var/lib/pgsql/data1
    port: 5432
  db2:
    dataDirectory: /var/lib/pgsql/data2
    port: 5433

task

[root@localhost myProject]# more roles/test1/tasks/main.yml
- name: "hash test"
  import_tasks: hashTest.yml
[root@localhost myProject]# more roles/test1/tasks/hashTest.yml
---
- name: "print dict"
  #debug:
  #  var: item
  file: path={{ item.value.dataDirectory }} state=directory owner=root group=root mode=0755
  with_dict: "{{ dbs }}"

実行結果

[root@localhost myProject]# ansible-playbook -i ./hosts site.yml
/usr/lib/python2.7/site-packages/ansible/parsing/vault/__init__.py:44: CryptographyDeprecationWarning: Python 2 is no longer supported by the Python core team. Support for it is now deprecated in cryptography, and will be removed in the next release.
  from cryptography.exceptions import InvalidSignature

PLAY [db] *********************************************************************************************************************************************************************

TASK [Gathering Facts] ********************************************************************************************************************************************************
ok: [localhost]

TASK [test1 : print dict] *****************************************************************************************************************************************************
changed: [localhost] => (item={u'key': u'db1', u'value': {u'dataDirectory': u'/var/lib/pgsql/data1', u'port': 5432}})
changed: [localhost] => (item={u'key': u'db2', u'value': {u'dataDirectory': u'/var/lib/pgsql/data2', u'port': 5433}})

PLAY RECAP ********************************************************************************************************************************************************************
localhost                  : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0