環境: 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 との関係がややこしい.
他のサーバを管理するには公開鍵暗号で 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 にコマンドを打てることを確認.
[defaults] host_key_checking = False interpreter_python=auto_silent
ansible_ssh_private_key_file: ~/.ssh/aws.pem ansible_user: hoge
[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
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 }}"
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
Ansible で AWS をコントロールしようとするには, Python の boto, boto3, botocore モジュールが必要となる.
この boto と boto3 の違いはよくわからない.
https://github.com/boto/boto によると,boto3 は boto の最新版で,一般的な利用には boto3 を推奨すると書いてある. boto3 と boto は並行して利用することも可能だと.
macOS 10.15.4 Catalina & Homebrew の Ansible を使うなら, 元々 boto,boto3,botocore が入っているのでインストールの必要なし.
# /usr/local/bin/pip3.8 install boto3
% 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
% /usr/local/opt/python@3.8/bin/python3.8
>>> import boto3
>>> s3 = boto3.resource('s3')
>>> for bucket in s3.buckets.all():
print(bucket.name)
タグ 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
Mac 版の VisualStudio Code から,自身の Ansible を利用する
メニュー[Code]>[基本設定]>[拡張機能]
検索フォームから "Ansible" を検索
”VSCode extension for Ansible(Microsoft)" (Ver.0.5.2)
をインストール.
Visual Studio Code から Playbook を置いているフォルダ ~/Ansible を開く.
メニュー[ファイル]>[開く]
左側のリストから [ANSIBLE] フォルダを選択し, メニュー[Code]>[基本設定]>[設定]
[ワークスペース]タブを選択し,リストから[拡張機能]>[Ansilbe Extension...]を選択.
[Ansible:Custom Options]に
-i inventory
と記入する.
AWS の認証情報は ~/.vscode/ もしくは ~/Ansible/.vscode/ に ansible-credentials.yml という名前で置くことができる. 中身は
aws: AWS_ACCESS_KEY_ID: 'XXXXXXXXXXXXXXX' AWS_SECRET_ACCESS_KEY: 'YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY'
左のリストから実行する Playbook を右クリックして,コンテキストメニューから [Run Ansible Playbook in Local Ansilbe] を 実行する.
ssh を使ったリモートでの実行もできそう. (Windows の VS Code で Playbook を書いて,そのまま実行とかできるかな)
Python の pywinrm ライブラリが必要だが,homebrew で入れた ansbile には既に含まれているようだ.
Windows 側で https://github.com/ansible/ansible/blob/devel/examples/scripts/ConfigureRemotingForAnsible.ps1 をダウンロードして実行するのでもよい.
Power Shell を管理者権限で起動し
PS > powershell -ExecutionPolicy ByPass .\ConfigureRemotingForAnsible.ps1
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