※当サイトの記事には、広告・プロモーションが含まれます。

Ansibleでバックアップとかってどうすれば良いのか

gigazine.net

AIが高度なコードを生成するようになったことで、顧客管理ソフトウェアを手がけるSalesforceのCEOが「AI導入が成功したので今年はエンジニアを雇わない」と発言したり、半導体大手・NVIDIAのCEOが「AIがコードを書くのでもうプログラミングを学ぶ必要はない」と発言したりして物議を醸している一方、AIツール自身はユーザーにプログラミングを学ぶよう提言しています。

AIがすべてのプログラミングコードを生成するようになるので「コーディングを学ぶのは時間の無駄」とReplitのCEOが答える - GIGAZINE

AIによって置き換えられる人間の技能を巡るビジネスリーダーたちの議論に、知識のない人でもプロンプトを入れるだけでアプリを作れるAIを開発したスタートアップ・ReplitのCEOの発言が加わりました。

AIがすべてのプログラミングコードを生成するようになるので「コーディングを学ぶのは時間の無駄」とReplitのCEOが答える - GIGAZINE

⇧ う~む、小規模なシステムなら「AI」で偶然解決できるのかもしれないですが、「幻覚(ハルシネーション)」の問題が無くならない以上、「プログラミング」をある程度、理解できていない状態だと「AI」の提案してくれるものが適切な内容なのか判断できない気がするんだが...

「AI」が提案してくれた「プログラミング」で継ぎ接ぎしていった結果、「変更容易性」の低い状態になって誰も改修できなくなり「大規模障害」が「頻発」する結末が待っていそうなんだが、責任は「AI」に取ってもらうで良いのかね?

「AI」の回答を100%正しいとして良いのならば、「プログラミング」の学習は不要で良いのかもしれませんが、「システム障害」などの問題が発生した時に、且つ、「AI」で解決できない状況になった時にどうするんですかね?

まさか、都合の悪い時だけ「プログラミング」に習熟した有識者に助けを求めるわけじゃないということなのだと信じたいところですね...

「プログラミング」の学習が不要と言えるような「銀の弾丸」的な存在に今のところ「AI」はなっていないと思うんだけどなぁ...

そして、

www.publickey1.jp

github.com

www.publickey1.jp

github.com

⇧ キャッチアップすることが多過ぎる問題...

Ansibleでバックアップとかってどうすれば良いのか

公式のドキュメントを見た限り、「バックアップ」に焦点を当てて言及している内容は見当たらないのですが、

github.com

⇧ issueによると、「バックアップ」する際のファイル名については、制約があるみたい。

肝心の「バックアップ」については、

qiita.com

⇧ 上記サイト様にありますように、「モジュール」の「ansible.builtin.copy」で「backup」という設定を有効にする必要があるっぽい。

issueにあったように、ファイル名が勝手に決められてしまうという...

とは言え、「バックアップ」は特定のディレクトリ配下毎するケースが多い気がするので、日付毎のディレクトリを作成する形ができれば良いのだが、

stackoverflow.com

⇧ stackoverflowによると、「Ansible」側で「ansible_date_time」という「日時」の変数が用意されているらしい。

docs.ansible.com

で、「ansible_date_time」の内容はというと、

github.com

https://github.com/ansible/ansible/blob/devel/lib/ansible/module_utils/facts/system/date_time.py

# Data and time related facts collection for ansible.
#
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible.  If not, see <http://www.gnu.org/licenses/>.

from __future__ import annotations

import datetime
import time

import ansible.module_utils.compat.typing as t
from ansible.module_utils.facts.collector import BaseFactCollector


class DateTimeFactCollector(BaseFactCollector):
    name = 'date_time'
    _fact_ids = set()  # type: t.Set[str]

    def collect(self, module=None, collected_facts=None):
        facts_dict = {}
        date_time_facts = {}

        # Store the timestamp once, then get local and UTC versions from that
        epoch_ts = time.time()
        now = datetime.datetime.fromtimestamp(epoch_ts)
        utcnow = datetime.datetime.fromtimestamp(
            epoch_ts,
            tz=datetime.timezone.utc,
        )

        date_time_facts['year'] = now.strftime('%Y')
        date_time_facts['month'] = now.strftime('%m')
        date_time_facts['weekday'] = now.strftime('%A')
        date_time_facts['weekday_number'] = now.strftime('%w')
        date_time_facts['weeknumber'] = now.strftime('%W')
        date_time_facts['day'] = now.strftime('%d')
        date_time_facts['hour'] = now.strftime('%H')
        date_time_facts['minute'] = now.strftime('%M')
        date_time_facts['second'] = now.strftime('%S')
        date_time_facts['epoch'] = now.strftime('%s')
        # epoch returns float or string in some non-linux environments
        if date_time_facts['epoch'] == '' or date_time_facts['epoch'][0] == '%':
            date_time_facts['epoch'] = str(int(epoch_ts))
        # epoch_int always returns integer format of epoch
        date_time_facts['epoch_int'] = str(int(now.strftime('%s')))
        if date_time_facts['epoch_int'] == '' or date_time_facts['epoch_int'][0] == '%':
            date_time_facts['epoch_int'] = str(int(epoch_ts))
        date_time_facts['date'] = now.strftime('%Y-%m-%d')
        date_time_facts['time'] = now.strftime('%H:%M:%S')
        date_time_facts['iso8601_micro'] = utcnow.strftime("%Y-%m-%dT%H:%M:%S.%fZ")
        date_time_facts['iso8601'] = utcnow.strftime("%Y-%m-%dT%H:%M:%SZ")
        date_time_facts['iso8601_basic'] = now.strftime("%Y%m%dT%H%M%S%f")
        date_time_facts['iso8601_basic_short'] = now.strftime("%Y%m%dT%H%M%S")
        date_time_facts['tz'] = time.strftime("%Z")
        date_time_facts['tz_dst'] = time.tzname[1]
        date_time_facts['tz_offset'] = time.strftime("%z")

        facts_dict['date_time'] = date_time_facts
        return facts_dict    

github.com

https://github.com/ansible/ansible/blob/devel/lib/ansible/module_utils/facts/default_collectors.py#L118

# This code is part of Ansible, but is an independent component.
# This particular file snippet, and this file snippet only, is BSD licensed.
# Modules you write using this snippet, which is embedded dynamically by Ansible
# still belong to the author of the module, and may assign their own license
# to the complete work.
#
# (c) 2017 Red Hat Inc.
#
# Redistribution and use in source and binary forms, with or without modification,
# are permitted provided that the following conditions are met:
#
#    * Redistributions of source code must retain the above copyright
#      notice, this list of conditions and the following disclaimer.
#    * Redistributions in binary form must reproduce the above copyright notice,
#      this list of conditions and the following disclaimer in the documentation
#      and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
# IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
# USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
from __future__ import annotations

import ansible.module_utils.compat.typing as t

from ansible.module_utils.facts.collector import BaseFactCollector

from ansible.module_utils.facts.other.facter import FacterFactCollector
from ansible.module_utils.facts.other.ohai import OhaiFactCollector

from ansible.module_utils.facts.system.apparmor import ApparmorFactCollector
from ansible.module_utils.facts.system.caps import SystemCapabilitiesFactCollector
from ansible.module_utils.facts.system.chroot import ChrootFactCollector
from ansible.module_utils.facts.system.cmdline import CmdLineFactCollector
from ansible.module_utils.facts.system.distribution import DistributionFactCollector
from ansible.module_utils.facts.system.date_time import DateTimeFactCollector
from ansible.module_utils.facts.system.env import EnvFactCollector
from ansible.module_utils.facts.system.dns import DnsFactCollector
from ansible.module_utils.facts.system.fips import FipsFactCollector
from ansible.module_utils.facts.system.loadavg import LoadAvgFactCollector
from ansible.module_utils.facts.system.local import LocalFactCollector
from ansible.module_utils.facts.system.lsb import LSBFactCollector
from ansible.module_utils.facts.system.pkg_mgr import PkgMgrFactCollector
from ansible.module_utils.facts.system.pkg_mgr import OpenBSDPkgMgrFactCollector
from ansible.module_utils.facts.system.platform import PlatformFactCollector
from ansible.module_utils.facts.system.python import PythonFactCollector
from ansible.module_utils.facts.system.selinux import SelinuxFactCollector
from ansible.module_utils.facts.system.service_mgr import ServiceMgrFactCollector
from ansible.module_utils.facts.system.ssh_pub_keys import SshPubKeyFactCollector
from ansible.module_utils.facts.system.systemd import SystemdFactCollector
from ansible.module_utils.facts.system.user import UserFactCollector

from ansible.module_utils.facts.hardware.base import HardwareCollector
from ansible.module_utils.facts.hardware.aix import AIXHardwareCollector
from ansible.module_utils.facts.hardware.darwin import DarwinHardwareCollector
from ansible.module_utils.facts.hardware.dragonfly import DragonFlyHardwareCollector
from ansible.module_utils.facts.hardware.freebsd import FreeBSDHardwareCollector
from ansible.module_utils.facts.hardware.hpux import HPUXHardwareCollector
from ansible.module_utils.facts.hardware.hurd import HurdHardwareCollector
from ansible.module_utils.facts.hardware.linux import LinuxHardwareCollector
from ansible.module_utils.facts.hardware.netbsd import NetBSDHardwareCollector
from ansible.module_utils.facts.hardware.openbsd import OpenBSDHardwareCollector
from ansible.module_utils.facts.hardware.sunos import SunOSHardwareCollector

from ansible.module_utils.facts.network.base import NetworkCollector
from ansible.module_utils.facts.network.aix import AIXNetworkCollector
from ansible.module_utils.facts.network.darwin import DarwinNetworkCollector
from ansible.module_utils.facts.network.dragonfly import DragonFlyNetworkCollector
from ansible.module_utils.facts.network.fc_wwn import FcWwnInitiatorFactCollector
from ansible.module_utils.facts.network.freebsd import FreeBSDNetworkCollector
from ansible.module_utils.facts.network.hpux import HPUXNetworkCollector
from ansible.module_utils.facts.network.hurd import HurdNetworkCollector
from ansible.module_utils.facts.network.linux import LinuxNetworkCollector
from ansible.module_utils.facts.network.iscsi import IscsiInitiatorNetworkCollector
from ansible.module_utils.facts.network.nvme import NvmeInitiatorNetworkCollector
from ansible.module_utils.facts.network.netbsd import NetBSDNetworkCollector
from ansible.module_utils.facts.network.openbsd import OpenBSDNetworkCollector
from ansible.module_utils.facts.network.sunos import SunOSNetworkCollector

from ansible.module_utils.facts.virtual.base import VirtualCollector
from ansible.module_utils.facts.virtual.dragonfly import DragonFlyVirtualCollector
from ansible.module_utils.facts.virtual.freebsd import FreeBSDVirtualCollector
from ansible.module_utils.facts.virtual.hpux import HPUXVirtualCollector
from ansible.module_utils.facts.virtual.linux import LinuxVirtualCollector
from ansible.module_utils.facts.virtual.netbsd import NetBSDVirtualCollector
from ansible.module_utils.facts.virtual.openbsd import OpenBSDVirtualCollector
from ansible.module_utils.facts.virtual.sunos import SunOSVirtualCollector

# these should always be first due to most other facts depending on them
_base = [
    PlatformFactCollector,
    DistributionFactCollector,
    LSBFactCollector
]  # type: t.List[t.Type[BaseFactCollector]]

# These restrict what is possible in others
_restrictive = [
    SelinuxFactCollector,
    ApparmorFactCollector,
    ChrootFactCollector,
    FipsFactCollector
]  # type: t.List[t.Type[BaseFactCollector]]

# general info, not required but probably useful for other facts
_general = [
    PythonFactCollector,
    SystemCapabilitiesFactCollector,
    PkgMgrFactCollector,
    OpenBSDPkgMgrFactCollector,
    ServiceMgrFactCollector,
    CmdLineFactCollector,
    DateTimeFactCollector,
    EnvFactCollector,
    LoadAvgFactCollector,
    SshPubKeyFactCollector,
    UserFactCollector,
    SystemdFactCollector
]  # type: t.List[t.Type[BaseFactCollector]]

# virtual, this might also limit hardware/networking
_virtual = [
    VirtualCollector,
    DragonFlyVirtualCollector,
    FreeBSDVirtualCollector,
    LinuxVirtualCollector,
    OpenBSDVirtualCollector,
    NetBSDVirtualCollector,
    SunOSVirtualCollector,
    HPUXVirtualCollector
]  # type: t.List[t.Type[BaseFactCollector]]

_hardware = [
    HardwareCollector,
    AIXHardwareCollector,
    DarwinHardwareCollector,
    DragonFlyHardwareCollector,
    FreeBSDHardwareCollector,
    HPUXHardwareCollector,
    HurdHardwareCollector,
    LinuxHardwareCollector,
    NetBSDHardwareCollector,
    OpenBSDHardwareCollector,
    SunOSHardwareCollector
]  # type: t.List[t.Type[BaseFactCollector]]

_network = [
    DnsFactCollector,
    FcWwnInitiatorFactCollector,
    NetworkCollector,
    AIXNetworkCollector,
    DarwinNetworkCollector,
    DragonFlyNetworkCollector,
    FreeBSDNetworkCollector,
    HPUXNetworkCollector,
    HurdNetworkCollector,
    IscsiInitiatorNetworkCollector,
    NvmeInitiatorNetworkCollector,
    LinuxNetworkCollector,
    NetBSDNetworkCollector,
    OpenBSDNetworkCollector,
    SunOSNetworkCollector
]  # type: t.List[t.Type[BaseFactCollector]]

# other fact sources
_extra_facts = [
    LocalFactCollector,
    FacterFactCollector,
    OhaiFactCollector
]  # type: t.List[t.Type[BaseFactCollector]]

# TODO: make config driven
collectors = _base + _restrictive + _general + _virtual + _hardware + _network + _extra_facts    

github.com

https://github.com/ansible/ansible/blob/devel/lib/ansible/module_utils/facts/ansible_collector.py#L72

# This code is part of Ansible, but is an independent component.
# This particular file snippet, and this file snippet only, is BSD licensed.
# Modules you write using this snippet, which is embedded dynamically by Ansible
# still belong to the author of the module, and may assign their own license
# to the complete work.
#
# (c) 2017 Red Hat Inc.
#
# Redistribution and use in source and binary forms, with or without modification,
# are permitted provided that the following conditions are met:
#
#    * Redistributions of source code must retain the above copyright
#      notice, this list of conditions and the following disclaimer.
#    * Redistributions in binary form must reproduce the above copyright notice,
#      this list of conditions and the following disclaimer in the documentation
#      and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
# IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
# USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#

from __future__ import annotations

import fnmatch
import sys

import ansible.module_utils.compat.typing as t

from ansible.module_utils.facts import timeout
from ansible.module_utils.facts import collector
from ansible.module_utils.common.collections import is_string


class AnsibleFactCollector(collector.BaseFactCollector):
    """A FactCollector that returns results under 'ansible_facts' top level key.

       If a namespace if provided, facts will be collected under that namespace.
       For ex, a ansible.module_utils.facts.namespace.PrefixFactNamespace(prefix='ansible_')

       Has a 'from_gather_subset() constructor that populates collectors based on a
       gather_subset specifier."""

    def __init__(self, collectors=None, namespace=None, filter_spec=None):

        super(AnsibleFactCollector, self).__init__(collectors=collectors,
                                                   namespace=namespace)

        self.filter_spec = filter_spec

    def _filter(self, facts_dict, filter_spec):
        # assume filter_spec='' or filter_spec=[] is equivalent to filter_spec='*'
        if not filter_spec or filter_spec == '*':
            return facts_dict

        if is_string(filter_spec):
            filter_spec = [filter_spec]

        found = []
        for f in filter_spec:
            for x, y in facts_dict.items():
                if not f or fnmatch.fnmatch(x, f):
                    found.append((x, y))
                elif not f.startswith(('ansible_', 'facter', 'ohai')):
                    # try to match with ansible_ prefix added when non empty
                    g = 'ansible_%s' % f
                    if fnmatch.fnmatch(x, g):
                        found.append((x, y))
        return found

    def collect(self, module=None, collected_facts=None):
        collected_facts = collected_facts or {}

        facts_dict = {}

        for collector_obj in self.collectors:
            info_dict = {}

            try:

                # Note: this collects with namespaces, so collected_facts also includes namespaces
                info_dict = collector_obj.collect_with_namespace(module=module,
                                                                 collected_facts=collected_facts)
            except Exception as e:
                sys.stderr.write(repr(e))
                sys.stderr.write('\n')

            # shallow copy of the new facts to pass to each collector in collected_facts so facts
            # can reference other facts they depend on.
            collected_facts.update(info_dict.copy())

            # NOTE: If we want complicated fact dict merging, this is where it would hook in
            facts_dict.update(self._filter(info_dict, self.filter_spec))

        return facts_dict


class CollectorMetaDataCollector(collector.BaseFactCollector):
    """Collector that provides a facts with the gather_subset metadata."""

    name = 'gather_subset'
    _fact_ids = set()  # type: t.Set[str]

    def __init__(self, collectors=None, namespace=None, gather_subset=None, module_setup=None):
        super(CollectorMetaDataCollector, self).__init__(collectors, namespace)
        self.gather_subset = gather_subset
        self.module_setup = module_setup

    def collect(self, module=None, collected_facts=None):
        # NOTE: deprecate/remove once DT lands
        # we can return this data, but should not be top level key
        meta_facts = {'gather_subset': self.gather_subset}

        # NOTE: this is just a boolean indicator that 'facts were gathered'
        # and should be moved to the 'gather_facts' action plugin
        # probably revised to handle modules/subsets combos
        if self.module_setup:
            meta_facts['module_setup'] = self.module_setup
        return meta_facts


def get_ansible_collector(all_collector_classes,
                          namespace=None,
                          filter_spec=None,
                          gather_subset=None,
                          gather_timeout=None,
                          minimal_gather_subset=None):

    filter_spec = filter_spec or []
    gather_subset = gather_subset or ['all']
    gather_timeout = gather_timeout or timeout.DEFAULT_GATHER_TIMEOUT
    minimal_gather_subset = minimal_gather_subset or frozenset()

    collector_classes = \
        collector.collector_classes_from_gather_subset(
            all_collector_classes=all_collector_classes,
            minimal_gather_subset=minimal_gather_subset,
            gather_subset=gather_subset,
            gather_timeout=gather_timeout)

    collectors = []
    for collector_class in collector_classes:
        collector_obj = collector_class(namespace=namespace)
        collectors.append(collector_obj)

    # Add a collector that knows what gather_subset we used so it it can provide a fact
    collector_meta_data_collector = \
        CollectorMetaDataCollector(gather_subset=gather_subset,
                                   module_setup=True)
    collectors.append(collector_meta_data_collector)

    fact_collector = \
        AnsibleFactCollector(collectors=collectors,
                             filter_spec=filter_spec,
                             namespace=namespace)

    return fact_collector    

⇧「Control node」の「システム時間」のようなのだが、

■抜粋 https://github.com/ansible/ansible/blob/devel/lib/ansible/module_utils/facts/system/date_time.py

import datetime
import time

...省略

        # Store the timestamp once, then get local and UTC versions from that
        epoch_ts = time.time()
        now = datetime.datetime.fromtimestamp(epoch_ts)
        utcnow = datetime.datetime.fromtimestamp(
            epoch_ts,
            tz=datetime.timezone.utc,
        )
...省略

        date_time_facts['date'] = now.strftime('%Y-%m-%d')
        date_time_facts['time'] = now.strftime('%H:%M:%S')
        date_time_facts['iso8601_micro'] = utcnow.strftime("%Y-%m-%dT%H:%M:%S.%fZ")
        date_time_facts['iso8601'] = utcnow.strftime("%Y-%m-%dT%H:%M:%SZ")
        date_time_facts['iso8601_basic'] = now.strftime("%Y%m%dT%H%M%S%f")
        date_time_facts['iso8601_basic_short'] = now.strftime("%Y%m%dT%H%M%S")

...省略

⇧ 基本的には、「UTC」の日時を扱っている模様。

docs.python.org

docs.python.org

 

「Control node」の「システム時間」ということは、

docs.ansible.com

⇧「Managed nodes」の「システム時間」と乖離が生まれるかもしれないと。

「バックアップ」対象は、「Managed nodes」にあるからして、普通に考えて「Managed node」の「システム時間」を利用すべきなので、「Ansible」側で用意されている「ansible_date_time」を利用するのはマズい気がする...

となってくると、

serverfault.com

⇧ 上記サイト様にありますように、

  • ansible.builtin.command
  • ansible.builtin.shell

を利用して「Managed node」側の「システム時間」を取得する感じになるんかなぁ...

何となく、「Ansible」は対象のマシンの「バックアップ」を想定していないのではないかという気がしてしまうのだが...

「Ansible」における「バックアップ」全然簡単じゃない気がするんだが...

結局のところ、「ansible.builtin.copy」の「backup」の設定を使うと世代管理的な「バックアップ」の実現は難しいような気がするんだが...

毎度モヤモヤ感が半端ない…

今回はこのへんで。