返回
Featured image of post ansible 入門指南 - Module

ansible 入門指南 - Module

學 Ansible 瞭解了 Playbook 使用 Module 開始百花齊放

前言

注意! 請使用 GCE 當作搭建 Managed Node 作為實戰範例,選 gce e2-micro 做為練習 一個月預估費用 7.11美,gcp 首次帳號提供300美一年使用

Module前置需求
ansible.builtin.apt - 網址-
ansible.builtin.command - 網址-
ansible.builtin.systemd_service - 網址python3apt
ansible.builtin.git - 網址git

ansible.builtin

apt

apt-get 於 Managed Node 安裝工具

---
# ans: https://superuser.com/questions/1720844/how-can-i-install-redis-server-7-on-ubuntu-22-04
# Hub: https://launchpad.net/
- name: 將 Redis 添至 apt 倉庫
  become: true
  ansible.builtin.apt_repository:
    repo: ppa:redislabs/redis
- name: 更新 apt 倉庫
  become: true
  ansible.builtin.apt:
    name: "*"
    state: latest
- name: 安裝 "Redis"
  become: true
  ansible.builtin.apt:
    name: redis
- name: 等同執行 "apt-get clean"
  become: true
  apt:
    clean: yes

command

command 於 Managed Node 執行

---
- name: 安裝 "curl"
  apt:
    name: curl
    state: present
- name: 執行 "curl"
  command: 
    cmd: curl ifconfig.me
  register: data
- debug: msg="{{ data.stdout }}"

systemd_service

使用 Docker 練習會卡到中風,systemd PID必須為1,找時間改用 Visualbox 測試

systemd 控管服務運行

---
- name: 重啟 "Redis"
  become: true             #使用 root權限
  ansible.builtin.systemd:
    name: redis-server.service
    state: restarted

git

---
- name: git clone
  ansible.builtin.git:
    repo: https://github.com/ansible/ansible-examples.git
    dest: /home/ansible/ansible-examples
Licensed under CC BY-NC-SA 4.0
comments powered by Disqus