はじめに
Ansibleで、PCをセットアップしています。その備忘録として記録します。
事前準備
ssh-copy-idを設定して、パスワードレスでログイン
コマンドライン
ansible-playbook --user ubuntu -i ubuntu ubuntu-setup.yml
yamlファイル詳細
ヘッダを先頭に、必要なタスクを1つのYamlファイルにまとめればOK。
ヘッダ
先頭の"---"が重要。
---
- name: setup ubuntu
hosts: all
become: 'True'
tasks:
apt update/upgrade
- name: apt update and upgrade
apt:
upgrade: yes
update_cache: yes
cache_valid_time: 86400
グループ作成
- name: create group "myid"
ansible.builtin.group:
name: myid
state: present
gid: 1001
ユーザ登録・ホームディレクトリ作成
- name: Create user "myid"
ansible.builtin.user:
name: myid
password: "{{ 'myid' | password_hash('sha512', 'myid') }}"
uid: 1001
group: myid
groups:
- sudo
- users
- adm
- cdrom
- dip
- plugdev
- lxd
state: "present"
shell: "/bin/bash"
skeleton: "/etc/skel"
system: false
create_home: true
home: "/home/myid"
SSH情報のコピー
ansibleを実行している環境から.ssh情報を、新しい環境へコピーする
- name: Create /home/myid/.ssh
ansible.builtin.file:
path: /home/myid/.ssh
state: directory
owner: myid
group: myid
mode: "0700"
- name: Copy .ssh/id* to hosts
ansible.builtin.copy:
src: "{{ item }}"
dest: "/home/myid/.ssh/"
owner: myid
group: myid
mode: 0600
with_fileglob:
- "/home/myid/.ssh/id*"
Sudoersの設定変更
パスワードレスでsudoできるように設定
- name: Check sudoers is already modified
lineinfile:
state: absent
path: /etc/sudoers
regexp: "ALL=NOPASSWD: ALL"
check_mode: true
changed_when: false
register: check
- name: Update sudoers
lineinfile:
dest: /etc/sudoers
state: present
insertafter: "See sudoers"
line: "%sudo ALL=NOPASSWD: ALL"
validate: '/usr/sbin/visudo -cf %s'
when: check.found == 0
今日はここまで。 つづくよてい