techtsubame’s blog

備忘録であり、何が起きても責任は取りません

Ansible Automation Platform 変数優先順位及び上書き確認

引用元

-

調査結果

custom_varsという変数に以下の*_varを設定しどれが優先されるかを調査。

-は変数定義をコメントアウトした状態。

# テンプレート Playbook ホスト グループ(Control) グループ(ALL) インベントリ 結果
1 template_var playbook_var host_var group_control_var group_all_var inventory_var template_var
2 - playbook_var host_var group_control_var group_all_var inventory_var playbook_var
3 - - host_var group_control_var group_all_var inventory_var host_var
4 - - - group_control_var group_all_var inventory_var group_control_var
5 - - - - group_all_var inventory_var group_all_var
6 - - - - - inventory_var inventory_var

実施

Playbook作成

ansibleサーバ

Playbook

- name: Vars test
  hosts: ALL
  va
    custom_vars: "playbook_var"

  tasks:
    - name: Vars
      debug:
        msg: "{{ custom_vars }}"
      tags: vars_test

Ansible Automation Platform

テンプレート

インベントリ

グループ ALL

グループ Control

ホスト

Ansible Automation Platform ansible用ユーザを作成し認証に鍵を設定

引用元

-

事前

Ansibleサーバ

adminユーザで実施

シェル配置

#!/bin/sh

# Variables
GROUP="ansible_group"
GROUPID=50000
USER="ansible_user"
USERID=50001
PASSWORD="hoge"
SSHPATH="/home/${USER}/.ssh"
AUTHFILE="authorized_keys"
SSHKEY=""

create_group() {
  if getent group ${GROUP} > /dev/null 2>&1; then
    echo "${GROUP} already exists."
  else
    if sudo groupadd -g ${GROUPID} ${GROUP} > /dev/null 2>&1; then
      echo "${GROUP} created."
    else
      echo "Failed to create ${GROUP}: error code $?" && exit 8
    fi
  fi
}

create_user() {
  if getent passwd ${USER} > /dev/null 2>&1; then
    echo "${USER} already exists."
  else
    if sudo useradd -u ${USERID} -g ${GROUP} -m -d /home/${USER} ${USER} > /dev/null 2>&1; then
        echo "${USER}:${PASSWORD}" | sudo chpasswd > /dev/null 2>&1
        echo "${USER} created."
    else
      echo "Failed to create ${USER}: error code $?" && exit 8
    fi
  fi
}

set_ssh_key() {
  if sudo mkdir -p ${SSHPATH} && sudo chown ${USER}:${GROUP} ${SSHPATH} && sudo chmod 700 ${SSHPATH}; then
    if echo ${SSHKEY} | sudo tee "${SSHPATH}/${AUTHFILE}" > /dev/null && sudo chmod 600 "${SSHPATH}/${AUTHFILE}" && sudo chown ${USER}:${GROUP} "${SSHPATH}/${AUTHFILE}"; then
      echo "SSH key file created and permissions set successfully"
    else
      echo "Failed to create SSH key file or set permissions" && exit 8
    fi
  fi
}

setup_sudoers() {
  if grep ${GROUP} /etc/sudoers.d/${GROUP} > /dev/null 2>&1; then
    echo "${GROUP} already exists in sudoers."
  else
    if echo "%${GROUP} ALL=(ALL:ALL) NOPASSWD:ALL" | sudo tee /etc/sudoers.d/${GROUP} > /dev/null && sudo chmod 0440 /etc/sudoers.d/${GROUP}; then
      echo "Sudoers file setup for ${GROUP} completed."
    else
      echo "Failed to setup sudoers file for ${GROUP}." && exit 8
    fi
  fi
}

create_group
create_user
set_ssh_key
setup_sudoers

exit 0

シェル実行

$ bash ./setup.sh
ansible_group created.
ansible_user created.
SSH key file created and permissions set successfully
Sudoers file setup for ansible_group completed.
$ 

ansible_userへスイッチ

$ su - ansible_user
Password:
$

鍵作成

$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/ansible_user/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/ansible_user/.ssh/id_rsa.
Your public key has been saved in /home/ansible_user/.ssh/id_rsa.pub.
The key fingerprint is:
- snip -
+----[SHA256]-----+
$ 

ansibleサーバも対象のため、以下を手動で実施

$ cp ~/.ssh/id_rsa ~/.ssh/authorized_keys

作業ユーザへスイッチ

$ su - admin
$ 

シェルに公開鍵を設定

$ sudo sh -c 'sed -i "s#SSHKEY=\"\"#SSHKEY=\"$(cat /home/ansible_user/.ssh/id_rsa.pub)\"#g" ./setup.sh'

シェルを対象サーバに転送

$ scp setup.sh admin@control01:.
$ scp setup.sh admin@control02:.

SSH越しにシェルを実行

01,02で標準出力が違うのはadminユーザの設定テストをして状態が異なるため
$ ssh -t admin@control01 'sh ./setup.sh'
[sudo] password for admin:
ansible_group created.
ansible_user created.
SSH key file created and permissions set successfully
Sudoers file setup for ansible_group completed.
Connection to control01 closed.
$ ssh -t admin@control02 'sh ./setup.sh'
admin@control02's password:

We trust you have received the usual lecture from the local System
Administrator. It usually boils down to these three things:

    #1) Respect the privacy of others.
    #2) Think before you type.
    #3) With great power comes great responsibility.

[sudo] password for admin:
ansible_group created.
ansible_user created.
SSH key file created and permissions set successfully
Sudoers file setup for ansible_group completed.
Connection to control02 closed.

ansible_userにスイッチ

$ su - ansible_user

接続テスト

[ansible_user@master01 ~]$ ssh ansible_user@control01
The authenticity of host 'control01 (192.168.50.41)' can't be established.
ECDSA key fingerprint is SHA256:T------.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'control01,192.168.50.41' (ECDSA) to the list of known hosts.
Activate the web console with: systemctl enable --now cockpit.socket

[ansible_user@control01 ~]$ exit
[ansible_user@master01 ~]$ ssh ansible_user@control02
The authenticity of host 'control02 (192.168.50.33)' can't be established.
ECDSA key fingerprint is SHA256:jVWzlk+0fsrUkL5xxBLorwYb6NxFRSMXBOkpsvIrT6U.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'control02,192.168.50.33' (ECDSA) to the list of known hosts.
Activate the web console with: systemctl enable --now cockpit.socket

Register this system with Red Hat Insights: insights-client --register
Create an account or view all your systems at https://red.ht/insights-dashboard
[ansible_user@control02 ~]$ exit

秘密鍵確認(後続手順で使用)

$ sudo cat ~ansible_user/.ssh/id_rsa
$ 

Ansible Automation Platform

認証情報

編集

変更

項目 変更前 変更後
ユーザ名 admin ansible_user
SSH秘密鍵 - 秘密鍵確認(後続手順で使用)の内容をペースト

確認

Testジョブの実行

0行目の最後に実行ユーザが変わっていることを確認

Ansible Automation Platform インベントリへホストの追加からHello worldの実行まで

実施すること

  • ホストの追加
  • ホストグループの追加
  • 認証情報の設定

引用元

-

実施

リソース

認証情報

追加

プロジェクト

作成

ディレクトリ作成

ソースコントロールのタイプを手動にするため、事前にディレクトリを作成

プロジェクト作成

インベントリ

ホスト

追加

以下を繰り返し追加 - control01 - control02 - master01

グループ

controlをまとめる作成

ホストの追加

masterのグループを作成

ALLのグループを作成

小グループとしてcontrolsとmastersを設定する

ホストを確認すると3台のホストが存在する

グループのイメージ

リソース

プレイブックの配置

[root@master01 playbooks]# pwd
/var/lib/awx/projects/playbooks
[root@master01 playbooks]# cat main.yml
- name: Hello World Sample
  hosts: controls
  tasks:
    - name: Hello Message
      debug:
        msg: "Hello World!"
[root@master01 playbooks]#

テンプレート

作成

インベントリ

先に作成した実行ホストリストを指定

プロジェクト

先に作成したPlaybookの場所を指定

Playbook

先に作成したプロジェクト配下に置いているPlaybookを指定 サブディレクトリも読み込まれる ただしYaml内に hosts: xxxの記載があるかのチェックを行なっている 無い場合はリストに表示されない

認証情報

先に作成したサーバへのログイン方法を設定した認証情報を指定

定義

実行

認証エラーになる場合は、一度認証情報の設定から [ ] 起動プロンプトのチェックボックスにチェックを入れて実行する

Ansible Automation Platform インベントリ作成まで

実施すること

引用元

qiita.com

実施

インスタンスグループ

クラスター内の複数のインスタンス(ノード)を一つのグループとして扱うことができます。
主にAAP環境を複数のインスタンス(サーバ)から構成するときに使用します。
また、最大同時実行ジョブ数等を設定できます。

作成

設定項目

名前
インスタンスグループの名前を指定します。
名前は一意でなければならず、"controller"という名前に指定しないようにしてください。
ポリシーインスタンスの最小値
新規インスタンスがオンラインになると、
このグループに自動的に最小限割り当てられるインスタンス数を入力します。
ポリシーインスタンスの割合
スライダーを使用して、新規インスタンスがオンラインになると、
このグループに自動的に最小限割り当てられるインスタンスの割合を選択します。
最大同時実行ジョブ
Ansible Automation Platformが一度に実行できるジョブの最大数を制御します。
これは、システムのリソース使用量を管理し、パフォーマンスを最適化するためのものです。
最大フォーク
Ansibleが一度に実行できるタスクの最大数を制御します。 
これは、Ansibleのタスクが並列に実行され、全体の実行時間が短縮される可能性があります。

作成(GUI)

組織

Ansible Automation Platformにおける「組織」は、ユーザー、チーム、プロジェクト、およびインベントリーの論理コレクションで、
Automation Controllerオブジェクトの階層の最上位のオブジェクトです。

作成

設定項目

名前
ユニークな名前を設定します。
説明
説明を記載します。
最大ホスト数
この組織で管理可能な最大ホスト数。デフォルト値は 0 で、管理可能な数に制限がありません。 
インスタンスグループ
この組織を実行するインスタンスグループを選択します。
実行環境
この組織内のジョブに使用される実行環境。
これは、実行環境がプロジェクト、ジョブテンプレート、またはワークフローレベルで
明示的に割り当てられていない場合にフォールバックとして使用されます。
Galaxy 認証情報
Ansible GalaxyまたはAutomation Hub APIにアクセスするために必要な認証情報を指します。

作成(GUI)

インベントリー

Ansibleがタスクを実行する対象となるホスト(サーバー)のリストのことです。
これは、Ansibleがどのサーバーに対して作業を行うべきかを知るためのものです。

作成

イベントリーの種類

インベントリー
これはAnsibleが管理する対象となるホスト(サーバー)のリストです。
通常、テキストファイルに記述され、Ansibleがどのサーバーに対して作業を行うべきかを知るためのものです。
スマートインベントリー
スマートインベントリーは、特定の条件に一致するホストを動的にグループ化する機能です。
これは、管理対象のホストが多いような大規模な環境で有効な手法で、一部のインベントリのみを表示する便利機能です。
構築済みインベントリー
この用語はAnsibleの公式ドキュメンテーションでは明確に定義されていません。しかし、一般的には、事前に作成(構築)され、特定のタスクやプロジェクトで使用するためのホストのリストを指すことが多いです。

作成(GUI)