--- - 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'