Ansible

注意:

JekyllのLiquidとAnsibleのjinjaの文法がぶつかって、一部コンフィグがうまく表示されない({{}}が衝突)。以下参照。

GitHub - YumaYX docs ansible


ホストの書き方

cat <<HOST > hosts
sv1 ansible_host=192.168.121.121 ansible_user=vagrant ansible_ssh_private_key_file=.vagrant/machines/server1/libvirt/private_key
sv2 ansible_host=192.168.121.41  ansible_user=vagrant ansible_ssh_private_key_file=.vagrant/machines/server2/libvirt/private_key

[two]
sv1
sv2

[mygroup:children]
two

HOST

Ansibleコンフィグの書き方

cat <<CFG > ansible.cfg
[defaults]
host_key_checking = False
[ssh_connection]
pipelining = True
CFG

ロールの作り方

ansible-galaxy init roles/init
cat << EOF > r.yml
- hosts: all
  become: true
  roles:
    - init
EOF

実行の仕方

dry run

ansible-playbook -i hosts site.yml --syntax-check
ansible-playbook -i hosts site.yml --check --diff

簡単なモジュール

ansible -i hosts all -m ping
ansible localhost -m ping

ローカル実行する方法

ansible-playbook -i localhost, -c local r.yml

指定ホストのみ実行する方法

--limit [hostname | group]

ansible-playbook -i hosts r.yml --limit sv1

レシピ

ホスト指定でタスクの実行の書き方

- name: Git Clone Repos with https
  ansible.builtin.git:
    repo: "https://github.com/YumaYX/"
    dest: "/tmp/"
  with_items:
    - dotfiles
  when: inventory_hostname in groups['two']

実行表示させる

- name: Check Ruby Install
  shell: bash -lc "ruby -v"
  register: result
  ignore_errors: true
- name: debug
  debug:
    msg: ""

実行をログ取る

- name: comm
  shell: "date"
  register: reg_val
  ignore_errors: yes

- name: create dir
  local_action: file path=log state=directory

- name: output file
  local_action: copy content=

アイテムループ

- name: Install necessary gems
  gem:
    name: ""
    executable: "/home//.rbenv/shims/gem"
  with_items:
    - rake
    - minitest
    - serverspec

dnfパッケージインストール

- name: install tools
  ansible.builtin.dnf:
    name: ""
    state: present
  vars:
    packages:
    - vim
    - make
    - "@development"
    - "@Server with GUI"

コマンド実行

- name: Update Gems system
  shell: bash -lc "gem update --system"

配列辞書ループ

- name: user
  ansible.builtin.user:
    name: ""
    uid: ""
    password: ""
  with_items: ""
  vars:
    users:
      - { name: 'user1', uid: 1234, pass: '$6$8k...' }

テンプレートの使い方、Jinja

テンプレートのファイルを作っておく。

mkdir roles/init/templates
touch roles/init/templates/file.txt.j2
- name: file upload
  ansible.builtin.template:
    src: templates/file.txt.j2
    dest: /etc/file.txt

インストールの仕方、環境構築

pip

echo 'ansible' > requirements.txt #ansibleをインストールする場合

echo 'ansible-core' >> requirements.txt
python3 -m venv venv
source venv/bin/activate
pip3 install -r requirements.txt

dnf

# ansible-coreのみ
dnf -y install ansible-core # as root

dnfでのAnsibleのインストールは、省略する。


# as root
## kvm
dnf -y install qemu-kvm libvirt virt-install
systemctl enable --now libvirtd

## vagrant
yum install -y yum-utils
yum-config-manager --add-repo https://rpm.releases.hashicorp.com/RHEL/hashicorp.repo
yum -y install vagrant

dnf -y install libvirt-devel
usermod -aG libvirt yuma

su - yuma
# as user
vagrant plugin install vagrant-libvirt

cat <<'VEOF' > Vagrantfile
# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure('2') do |config|
  config.vm.box = 'almalinux/9'
  config.vm.provider 'libvirt' do |vb|
    vb.memory = 768
    vb.cpus = 1
  end
  config.vm.define "server1" do |server|
    server.vm.network "public_network", :dev => 'wlp2s0'
  end

  config.vm.define "server2" do |server|
    server.vm.network "public_network", :dev => 'wlp2s0'
  end
end
VEOF

vagrant up