#author("2020-06-10T16:02:57+09:00","default:tanak0to","tanak0to") #author("2020-06-23T11:26:31+09:00","default:tanak0to","tanak0to") RIGHT:[[Mac 関係覚え書き]] *インストール [#q8cf0f84] 環境: macOS 10.15.4 Ansible は homebrew でインストール. $ brew install ansible Python は /usr/local/opt/ansible 以下に ansible のパッケージに付属する python 3.8 へのシンボリックリンクが作成され, それが使われるようだ. /usr/local/bin には Python3.8 はシンボリックリンクされないので, OS 標準の Python 3.7 との関係がややこしい. *実行 [#fa4fb45c] 他のサーバを管理するには公開鍵暗号で ssh 接続できる必要あり. homebrew では sshpass をインストールしないポリシーのようだ. ~/Ansible/ に inventory ファイル,Playbook ファイル,var ファイルのサンプルを作った. ~/Ansible/ ├── ansible.cfg ├── group_vars │ └── aws.yml ├── inventory ├── pb_aws_cmd.yml └── pb_aws_updata.yml % ansible-playbook -i inventory pb_aws_cmd.yml で AWS の EC2 にコマンドを打てることを確認. **ansible.cfg [#ee87f898] [defaults] host_key_checking = False interpreter_python=auto_silent **aws.yml [#w8a77fad] ansible_ssh_private_key_file: ~/.ssh/aws.pem ansible_user: hoge **inventory [#j996c6d5] [localhost] 127.0.0.1 ansible_connection=local [localhost:vars] ansible_python_interpreter=/usr/local/opt/ansible/libexec/bin/python3.8 [aws] aws.example.com **pb_aws_cmd.yml [#h4d3e877] Software Design 2018 12月号のサンプルより - hosts: aws vars: sample_vars: software_design_2018 tasks: - shell: uptime register: result - debug: msg: "{{ result.stdout }}" - debug: msg: "{{ sample_vars }}" **pb_aws_update.yml [#w5aa1bda] apt upgrade を実行する Playbook(インデントに注意) - hosts: aws become: true become_user: root tasks: - name: Update apt repo and cache on all Debian/Ubuntu boxes apt: update_cache=yes force_apt_get=yes cache_valid_time=3600 - name: Upgrade all packages on servers apt: upgrade=dist force_apt_get=yes - name: Check if a reboot is needed on all servers register: reboot_required_file stat: path=/var/run/reboot-required get_md5=no - name: Reboot the box if kernel updated reboot: msg: "Reboot initiated by Ansible for kernel updates" connect_timeout: 5 reboot_timeout: 300 pre_reboot_delay: 0 post_reboot_delay: 30 test_command: uptime when: reboot_required_file.stat.exists *AWS EC2 を操作 [#k10058f9] **boto, boto3 [#c8191bfa] Ansible で AWS をコントロールしようとするには, Python の boto, boto3, botocore モジュールが必要となる. この boto と boto3 の違いはよくわからない. -https://www.it-swarm.dev/ja/python/aws-botoとboto3の違いは何ですか/1056044775/amp/ -https://dev.classmethod.jp/articles/2018-solo-boto3-advent-calendar-day19/ https://github.com/boto/boto によると,boto3 は boto の最新版で,一般的な利用には boto3 を推奨すると書いてある. boto3 と boto は並行して利用することも可能だと. ***boto3 インストール [#fd9a984b] macOS 10.15.4 Catalina & Homebrew の Ansible を使うなら, 元々 boto,boto3,botocore が入っているのでインストールの必要なし. # /usr/local/bin/pip3.8 install boto3 ***aws cli インストール [#nba33772] % brew install awscli % aws configure AWS Access Key ID [None]: XXXXXXXXXXXXXXXXX <- AWS Console の IAM で作成した Access ID と Key のペア AWS Secret Access Key [None]: YYYYYYYYYYYYYYYYYYYYYYYYYYYY Default region name [None]: ap-northeast-1 Default output format [None]: text % ls ~/.aws config credentials ***boto3 ライブラリの動作確認 [#f454ff00] % /usr/local/opt/python@3.8/bin/python3.8 >>> import boto3 >>> s3 = boto3.resource('s3') >>> for bucket in s3.buckets.all(): print(bucket.name) **pb_aws_alfa_stating.yml [#oa72254a] タグ Name に "alfa1" とつけたEC2 を起動させる(running 状態にする) Playbook - name: Start Alfa on AWS hosts: localhost connection: local gather_facts: false vars: common: region: ap-northeast-1 name: alfa1 tasks: - name: Start Alfa 1 ec2_instance: region: "{{ common.region }}" state: running filters: "tag:Name": "{{ common.name }}" 実行 % ansible-playbook -i inventory pb_aws_alfa_stating.yml *VisualStudio Code から Ansible 操作 [#de12ac37] Mac 版の VisualStudio Code から,自身の Ansible を利用する **機能拡張をインストール [#w46b8fb1] メニュー[Code]>[基本設定]>[拡張機能] 検索フォームから "Ansible" を検索 ”VSCode extension for Ansible(Microsoft)" (Ver.0.5.2) をインストール. **設定 [#b20b19c8] Visual Studio Code から Playbook を置いているフォルダ ~/Ansible を開く. メニュー[ファイル]>[開く] 左側のリストから [ANSIBLE] フォルダを選択し, メニュー[Code]>[基本設定]>[設定] [ワークスペース]タブを選択し,リストから[拡張機能]>[Ansilbe Extension...]を選択. [Ansible:Custom Options]に -i inventory と記入する. **AWS の認証情報 [#xd0b3309] AWS の認証情報は ~/.vscode/ もしくは ~/Ansible/.vscode/ に ansible-credentials.yml という名前で置くことができる. 中身は aws: AWS_ACCESS_KEY_ID: 'XXXXXXXXXXXXXXX' AWS_SECRET_ACCESS_KEY: 'YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY' **Playbook 実行 [#z37fabb9] 左のリストから実行する Playbook を右クリックして,コンテキストメニューから [Run Ansible Playbook in Local Ansilbe] を 実行する. ssh を使ったリモートでの実行もできそう. (Windows の VS Code で Playbook を書いて,そのまま実行とかできるかな) ---- *Ansible で Windows 管理 [#cd884989] **Ansible コントロールノード側の準備 [#te3aa331] Python の pywinrm ライブラリが必要だが,homebrew で入れた ansbile には既に含まれているようだ. **Windows 側の準備 [#z2ec544b] +サービス設定から [Windows Remote Management] を有効化 +WinRM 経由の接続を受け付けるためのリスナーの設定 +Windows ファイアウォールの設定で tcp/5985, tcp/5986 を開放する Windows 側で https://github.com/ansible/ansible/blob/devel/examples/scripts/ConfigureRemotingForAnsible.ps1 をダウンロードして実行するのでもよい. Power Shell を管理者権限で起動し PS > powershell -ExecutionPolicy ByPass .\ConfigureRemotingForAnsible.ps1 **Mac で Playbook を実行するときの注意 [#x63bc2b6] Mac で Playbook を実行した場合,以下のようなエラーで失敗する時がある. % ansible-playbook -i inventory pb_windows_update.yml PLAY [Perform Windows Update from Ansible] ************************************* TASK [Check HotFix List Before Updating] *************************************** objc[11875]: +[NSNumber initialize] may have been in progress in another thread when fork() was called. We cannot safely call it or ignore it in the fork() child process. Crashing instead. Set a breakpoint on objc_initializeAfterForkError to debug. ERROR! A worker was found in a dead state このような場合, 以下の環境変数を設定してから実行する % export OBJC_DISABLE_INITIALIZE_FORK_SAFETY=YES *Ansilbe Valult のメモ [#abada265] ./host_vars/ localhost/ vars <--暗号化が必要ない変数を入れる vault <--ansible-vault で暗号化したファイルを入れる. ./.vault.pass <--vault ファイルを暗号化したときのパスワードを生書き ↑ ansible_cfg で vault_password_file= でファイル名を指定する. vault ファイルの作り方 $ ansible-vault create vault New Vault password: ← vault ファイルのパスワードを設定 Confirm New Vault password: ← vault ファイルのパスワード再確認 -----(変数名とその値を vi で書いていく) ansible_password: hogehoge ansible_sudo_pass: hogerahogera -----(vi で書き込み終了) vault ファイルの確認 $ ansible-vault edit vault