====== チュートリアルのようなもの ====== 多分、一般的にはこれをチュートリアルとは言わないと思います。m(\\ ほぼ素の仮想マシンを用意して、その環境設定を一から行っていきます。\\ ansible以前の条件は以下の通り((お仕事の成果を流用してるので、TargetがCentOSです)) 管理側 ホスト名 master OS CentOS ansible インストール済み User root pass:ansible 自身(127.0.0.1)をKnown_hostsに追加しておく ssh-keygen -R 127.0.0.1 ssh-keyscan -H 127.0.0.1>>~/.ssh/known_hosts 被管理側 ホスト名 client1,2 OS CentOS User root pass:ansible ===== 管理側ホストの設定 ===== rootでログイン\\ 事前準備として以下のファイルを作成 [mine] 127.0.0.1 [mine:vars] ansible_ssh_port=22 ansible_ssh_user=root #ansible_ssh_pass=ansible #ansible_sudo_pass=ansible 以下のコマンドで、設定するパスワードハッシュを作成\\ 作成したハッシュは、Playbookで使用します # python -c 'import crypt; print crypt.crypt("NewUserPa$$w0rd", "$6$SaltSALT$")' 以下の処理をこなうPlaybookを作成 - rootパスワード変更 - ansibleユーザを追加 - ansibleユーザが被管理ホストにアクセスする為のssh keyを作成 - ansibleユーザがsudo可能に設定 - ansibleユーザにsshアクセス用PublicKeyを設定 - Best Practicesに沿ったディレクトリを作成するPlaybookをダウンロード - sshの設定 - rootのsshアクセスを禁止 - 公開鍵によるsshアクセスを許可 - パスワード認証禁止 --- - hosts: mine # 対象を指定。all は全ホスト #sudo: yes # sudo を行う vars: # 変数指定 username: ansible userpassword: $6$aBcD9876$q5X0jkld4QdBZSAqpHAejIRDf6fHbr73CWq9Ka0iwMiqh2UoXxsMIWFJcvYWq2D.PUFYTF84MnZpUip/DfWYw0 userhome: /home/ansible rootpassword: $6$aBcD9876$znTv96hIRWiETfoOUgZ9lpqhpfY5NuyKB01Hf9ayZxvw4AYTXgtaRXDBKhknS3HHDVi4B053MITTuPtfP2xrH. ansible_dir_yml_url: https://raw.githubusercontent.com/hnakamur/ansible-playbooks/master/ansible-directories.yml sshuser: - name: ansible key: path/to/ansibleuser.id_rsa.pub tasks: # 実行するtask の指定を開始 - name: rootパスワード変更 # task の名前 user: name=root password={{ rootpassword}} - name: ansibleユーザーを追加しsshkeyを作成 user: name={{ username }} password={{ userpassword }} generate_ssh_key=yes ssh_key_bits=4096 - name: ansibleユーザーをsudoersに追加 ## NOPASSWD: ALLの「:」でエラーが出るので以下のようにネスト ## または NOPASSWD:ALL とスペースをなくす ## または - lineinfile: "regexp='^%wheel' line='%wheel ALL=(ALL) NOPASSWD: ALL' dest=/etc/sudoers" ## のように全体を"でくくる lineinfile: dest: '/etc/sudoers' backup: yes state: present regexp: '^ansible' line: 'ansible ALL=(ALL) NOPASSWD: ALL' - name: ansibleユーザにsshアクセス用PublicKeyを設定 authorized_key: user={{ item['name'] }} key="{{ lookup('file', item['key']) }}" with_items: sshuser - name: ansibleディレクトリを作成 file: path={{userhome}}/ansible state=directory owner=ansible group=ansible - name: Best Practicesに沿ったディレクトリを作成するPlaybookをダウンロード get_url: url={{ansible_dir_yml_url}} dest={{userhome}}/ansible/ansible-directories.yml owner=ansible group=ansible - name: configure sshd_config lineinfile: dest='/etc/ssh/sshd_config' backup=yes state=present regexp="{{item.regexp}}" insertafter="{{item.insertafter}}" line="{{item.line}}" with_items: # Root ログイン禁止 - regexp: '^PermitRootLogin' line: 'PermitRootLogin no' insertafter: '#PermitRootLogin' # 公開鍵認証を許可 - regexp: '^PubkeyAuthentication' line: 'PubkeyAuthentication yes' insertafter: '#PubkeyAuthentication' # 公開鍵認証を許可 - regexp: '^AuthorizedKeysFile' line: 'AuthorizedKeysFile .ssh/authorized_keys' insertafter: '#AuthorizedKeysFile' # パスワード認証禁止 - regexp: '^PasswordAuthentication' line: 'PasswordAuthentication no' insertafter: '#PasswordAuthentication' # パスワード認証禁止 - regexp: '^UsePAM' line: 'UsePAM no' insertafter: '#UsePAM' # パスワード認証禁止 - regexp: '^ChallengeResponseAuthentication' line: 'ChallengeResponseAuthentication no' insertafter: '#ChallengeResponseAuthentication' ansibleでログイン - Best Practicesに沿ったディレクトリをansibleユーザhomeに作成 $ cd ~/ansible $ ansible-playbook --extra-vars '{"roles":["common", "mysql", "webserver"]}' ansible-directories.yml OR $ ansible-playbook --extra-vars 'roles=apache' ansible-directories.yml 構成は以下のとおりです。 root │ hosts │ site.yml ├─ group_vars │ {group name} └─ roles └─ {role name} ├─ files │ {file name} ├─ handlers │ main.yml ├─ tasks │ main.yml └─ templates {template name} ^ディレクトリ/ファイル^説明^ |hosts|実行対象サーバーの定義ファイル。複数のマシンをグループにわけて定義。| |site.yml|サーバーに実行させるroleを定義| |group_vars|グループ変数の定義ファイルの格納ディレクトリ。hosts内で定義されたグループごとに別々の変数を定義。| |roles/handlers|起動コマンドなどを定義するファイルの格納ディレクトリ。例えばMySQLのrestartコマンドなどはここで定義| |roles/tasks|メインタスクを定義するファイルの格納ディレクトリ。| |roles/files|システムで使用される静的なファイルの格納ディレクトリ。packageのrepファイルやconfigのファイルを格納。| |roles/templates|システムで使用される動的に更新するファイルの格納ディレクトリ。taskの実行結果やgroup_varsの値によってconfigを書き換える必要がある場合に利用。| ===== 被管理側ホストの設定 ===== ===== 参考 ===== [[http://qiita.com/raki/items/e8b1cdac4a86eea0231f|オレオレ Ansible Best Practices 2015年1月版]]