0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Ansible Trial

Posted at

はじめに

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

今日はここまで。 つづくよてい

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?