久しぶりに Ansible を触ってみたら ansible.cfg を作成する ansible-config init コマンドができていたので素振りしてみる。

ドキュメント

Ansible Configuration Settings — Ansible Documentation

バージョン確認

pip で最新の ansible をインストールしている。 ansible-corev2.13.2

GitHub を見たところ、ansible-config の init オプションは v2.12.0 から追加されているので、使うなら v2.12.0 以上で。 職場では v2.9 を使っているため、このオプションは使えない模様……。

$ ansible-config --version
ansible-config [core 2.13.2]
  config file = None
  configured module search path = ['/home/nnstt1/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /home/nnstt1/.virtualenv/ansible/lib/python3.9/site-packages/ansible
  ansible collection location = /home/nnstt1/.ansible/collections:/usr/share/ansible/collections
  executable location = /home/nnstt1/.virtualenv/ansible/bin/ansible-config
  python version = 3.9.10 (main, Feb  9 2022, 00:00:00) [GCC 11.2.1 20220127 (Red Hat 11.2.1-9)]
  jinja version = 3.1.2
  libyaml = True

使い方

--help 引数をつけて使い方を見てみる。

$ ansible-config init --help
usage: ansible-config init [-h] [-v] [-c CONFIG_FILE] [-t {all,base,become,cache,callback,cliconf,connection,httpapi,inventory,lookup,netconf,shell,vars}] [--format {ini,env,vars}] [--disabled] [args ...]

positional arguments:
  args                  Specific plugin to target, requires type of plugin to be set

optional arguments:
  -h, --help            show this help message and exit
  -v, --verbose         Causes Ansible to print more debug messages. Adding multiple -v will increase the verbosity, the builtin plugins currently evaluate up to -vvvvvv. A reasonable level to start is -vvv, connection debugging might require -vvvv.
  -c CONFIG_FILE, --config CONFIG_FILE
                        path to configuration file, defaults to first file found in precedence.
  -t {all,base,become,cache,callback,cliconf,connection,httpapi,inventory,lookup,netconf,shell,vars}, --type {all,base,become,cache,callback,cliconf,connection,httpapi,inventory,lookup,netconf,shell,vars}
                        Filter down to a specific plugin type.
  --format {ini,env,vars}, -f {ini,env,vars}
                        Output format for init
  --disabled            Prefixes all entries with a comment character to disable them

--disabled

ドキュメントには --disabled 引数を使ったサンプルが載っていたが、どうやら各設定をコメントアウトした状態で ansible.cfg を出力するようだ。 設定可能な項目を把握できるためとても嬉しい。

$ ansible-config init --disabled > ansible.cfg

基本的にはサンプル通りでよいと思う。

-t/--type

-t/--type では特定のプラグインの設定を選択して出力できる引数のようだけど、意識してプラグインを使ったことがないのでどのような設定項目があるかは不明。

この引数をつけない場合は -t base と同じ ansible.cfg が出力された

-f/--format

-f/--format は名前の通り ansible.cfg の出力フォーマットを指定できる。 よく見るのは ini 形式だが、環境変数に設定できる env もある。

vars という形式も指定できるが、試しに実行したところ YAML 形式で以下が出力された。 おそらく Playbook の vars_files で直接読み込むためのフォーマットだと思うが、他のフォーマットのものと比べて設定項目が極端に少ない、なぜだろう?

# Sets the output directory and filename prefix to generate coverage run info.(str): Sets the output directory on the remote host to generate coverage reports to.
#Currently only used for remote coverage on PowerShell modules.
#This is for internal use only.
_ansible_coverage_remote_output: ''


# No syslog on target(boolean): Toggle Ansible logging to syslog on the target when it executes tasks. On Windows hosts this will disable a newer style PowerShell modules from writting to the event log.
ansible_no_target_syslog: false


# Gather Facts Modules(list): Which modules to run during a play's fact gathering stage, using the default of 'smart' will try to figure it out based on connection type.
#If adding your own modules but you still want to use the default Ansible facts, you will want to include 'setup' or corresponding network module to the list (if you add 'smart', Ansible will also figure it out).
#This does not affect explicit calls to the 'setup' module, but does always affect the 'gather_facts' action (implicit or explicit).
ansible_facts_modules:
- smart


# Python interpreter path (or automatic discovery behavior) used for module execution(string): Path to the Python interpreter to be used for module execution on remote targets, or an automatic discovery mode. Supported discovery modes are ``auto`` (the default), ``auto_silent``, ``auto_legacy``, and ``auto_legacy_silent``. All discovery modes employ a lookup table to use the included system Python (on distributions known to include one), falling back to a fixed ordered list of well-known Python interpreter locations if a platform-specific default is not available. The fallback behavior will issue a warning that the interpreter should be set explicitly (since interpreters installed later may change which one is used). This warning behavior can be disabled by setting ``auto_silent`` or ``auto_legacy_silent``. The value of ``auto_legacy`` provides all the same behavior, but for backwards-compatibility with older Ansible releases that always defaulted to ``/usr/bin/python``, will use that interpreter if present.
ansible_python_interpreter: auto


# Adjust maximum file descriptor soft limit during Python module execution(string): Attempts to set RLIMIT_NOFILE soft limit to the specified value when executing Python modules (can speed up subprocess usage on Python 2.x. See https://bugs.python.org/issue11284). The value will be limited by the existing hard limit. Default value of 0 does not attempt to adjust existing system-defined limits.
ansible_python_module_rlimit_nofile: 0


# Windows Async Startup Timeout(integer): For asynchronous tasks in Ansible (covered in Asynchronous Actions and Polling), this is how long, in seconds, to wait for the task spawned by Ansible to connect back to the named pipe used on Windows systems. The default is 5 seconds. This can be too low on slower systems, or systems under heavy load.
#This is not the total time an async command can run for, but is a separate timeout to wait for an async command to start. The task will only start to be timed against its async_timeout once it has connected to the pipe, so the overall maximum duration the task can take will be extended by the amount specified here.
ansible_win_async_startup_timeout: 5

コメントアウトされない

-f/--format--disabled を併用した場合、YAML の配列部分がコメントアウトされない事象が見つかった。 Issue を投げてみようかな。

# Sets the output directory and filename prefix to generate coverage run info.(str): Sets the output directory on the remote host to generate coverage reports to.
#Currently only used for remote coverage on PowerShell modules.
#This is for internal use only.
#_ansible_coverage_remote_output: ''


# No syslog on target(boolean): Toggle Ansible logging to syslog on the target when it executes tasks. On Windows hosts this will disable a newer style PowerShell modules from writting to the event log.
#ansible_no_target_syslog: false


# Gather Facts Modules(list): Which modules to run during a play's fact gathering stage, using the default of 'smart' will try to figure it out based on connection type.
#If adding your own modules but you still want to use the default Ansible facts, you will want to include 'setup' or corresponding network module to the list (if you add 'smart', Ansible will also figure it out).
#This does not affect explicit calls to the 'setup' module, but does always affect the 'gather_facts' action (implicit or explicit).
#ansible_facts_modules:
- smart


# Python interpreter path (or automatic discovery behavior) used for module execution(string): Path to the Python interpreter to be used for module execution on remote targets, or an automatic discovery mode. Supported discovery modes are ``auto`` (the default), ``auto_silent``, ``auto_legacy``, and ``auto_legacy_silent``. All discovery modes employ a lookup table to use the included system Python (on distributions known to include one), falling back to a fixed ordered list of well-known Python interpreter locations if a platform-specific default is not available. The fallback behavior will issue a warning that the interpreter should be set explicitly (since interpreters installed later may change which one is used). This warning behavior can be disabled by setting ``auto_silent`` or ``auto_legacy_silent``. The value of ``auto_legacy`` provides all the same behavior, but for backwards-compatibility with older Ansible releases that always defaulted to ``/usr/bin/python``, will use that interpreter if present.
#ansible_python_interpreter: auto


# Adjust maximum file descriptor soft limit during Python module execution(string): Attempts to set RLIMIT_NOFILE soft limit to the specified value when executing Python modules (can speed up subprocess usage on Python 2.x. See https://bugs.python.org/issue11284). The value will be limited by the existing hard limit. Default value of 0 does not attempt to adjust existing system-defined limits.
#ansible_python_module_rlimit_nofile: 0


# Windows Async Startup Timeout(integer): For asynchronous tasks in Ansible (covered in Asynchronous Actions and Polling), this is how long, in seconds, to wait for the task spawned by Ansible to connect back to the named pipe used on Windows systems. The default is 5 seconds. This can be too low on slower systems, or systems under heavy load.
#This is not the total time an async command can run for, but is a separate timeout to wait for an async command to start. The task will only start to be timed against its async_timeout once it has connected to the pipe, so the overall maximum duration the task can take will be extended by the amount specified here.
#ansible_win_async_startup_timeout: 5

-c/--config

設定ファイルを指定する引数だけど、init オプションをつけた場合はなにも変化はなかった。 正直つかいどころが分からない。

args

ansible-config init [args] で特定のプラグインをターゲットにする場合に設定するようだけど、こちらもどころが分からなかった。