Commit 5ef03cd4 authored by Vũ Hoàng Nam's avatar Vũ Hoàng Nam

Add new some sample playbooks and update ansible-lint compatible.

parent abe791f2
---
- name: All about ansible_facts
hosts: all
tasks:
- name: Get all ansible_facts
ansible.builtin.debug:
var: ansible_facts
- name: Get distribution
ansible.builtin.debug:
var: ansible_facts["distribution"]
- name: Check distribution_major_version for testing with When condition.
ansible.builtin.debug:
var: ansible_facts["distribution"] == "22"
- name: When conditions using ansible_facts
ansible.builtin.debug:
msg: "Hello from {{ ansible_facts.hostname }} is running {{ ansible_facts.os_family }}"
when:
- ansible_facts.distribution == "Ubuntu" # AND logic
- ansible_facts["distribution_major_version"] == "11" #22 for true
- name: More When conditions using ansible_facts
ansible.builtin.debug:
msg: "Hello from {{ ansible_facts.hostname }} is running Ubuntu or CentOS"
when: (ansible_facts['distribution'] == "CentOS" or ansible_facts['distribution'] == "Ubuntu")
......@@ -5,12 +5,12 @@
tasks:
- name: Copy source code file to remote host
copy:
ansible.builtin.copy:
src: files/small-flask-app
dest: /home/jay/app
# - name: Build image
# docker_image:
# ansible.builtin.docker_image::
# build:
# path: /home/jay/app/small-flask-app
# name: small-flask-app
......@@ -18,12 +18,12 @@
# source: build
- name: Pull Docker image
docker_image:
ansible.builtin.docker_image:
name: garovu/small-flask-app
source: pull
- name: Run Docker container
docker_container:
ansible.builtin.docker_container:
name: small-flask-app
image: garovu/small-flask-app
ports:
......
---
- name: Install Docker and manage containers
hosts: all
become: yes
become: true
tasks:
- name: Install required packages
apt:
ansible.builtin.apt:
name:
- apt-transport-https
- ca-certificates
......@@ -17,22 +16,21 @@
when: ansible_os_family == 'Debian'
- name: Add Docker GPG key
apt_key:
ansible.builtin.apt_key:
url: https://download.docker.com/linux/ubuntu/gpg
when: ansible_os_family == 'Debian'
- name: Add Docker repository
apt_repository: #use for arm64 chipset
ansible.builtin.apt_repository: # use for arm64 chipset
repo: deb [arch=arm64] https://download.docker.com/linux/ubuntu jammy stable
state: present
when: ansible_os_family == 'Debian'
- name: Install Docker
apt:
ansible.builtin.apt:
name:
- docker-ce
- docker-ce-cli
- containerd.io
state: present
when: ansible_os_family == 'Debian'
---
- hosts: all
become: yes
- name: My First Playbook
hosts: all
tasks:
- name: Ping my hosts
ansible.builtin.ping:
- name: Print message
- name: Print message through dedug modune
ansible.builtin.debug:
msg: Hello world
- name: Install neofetch
apt:
name: neofetch
state: present
- name: Install and run service in block
when: ansible_facts["distribution"] == "Ubuntu"
become: true # just use sudo for task in this block
block:
- name: Install some packages
ansible.builtin.apt:
name:
- neofetch
- ufw
state: latest
- name: Active service
ansible.builtin.service:
name: ufw
state: started
enabled: true
rescue:
- name: Rescue block
ansible.builtin.debug:
msg: "error"
when: ansible_failed_task.name != "Install some packages"
always:
- name: Always block
ansible.builtin.debug:
msg: "finish"
---
- name: Write hostname
hosts: all
become: true
tasks:
- name: write hostname using jinja2
- name: Check user home directory
ansible.builtin.debug:
var: ansible_facts.env.PWD
- name: Write hostname using jinja2
ansible.builtin.template:
src: files/templates/test.j2
dest: /home/jay/msg.txt
dest: '{{ ansible_facts.env.PWD }}/msg.txt'
---
- name: Verify apache installation
hosts: all
become: true
remote_user: root
tasks:
- name: Ensure apache is at the latest version
ansible.builtin.apt:
name: apache2
state: present
- name: Write the apache config file
ansible.builtin.template:
src: files/templates/test.j2
dest: /etc/apache2/sites-available/test.conf
mode: preserve
notify: # call a handlers
- Restart apache
- "restart firewall"
- name: Ensure apache is running
ansible.builtin.service:
name: apache2
state: started
handlers:
- name: Restart apache
ansible.builtin.service:
name: apache2
state: restarted
- name: Restart ufw
ansible.builtin.debug:
msg: "Restart firewall sucessfull."
listen: "restart firewall"
---
- hosts: 127.0.0.1 # localhost ip
connection: local
tasks:
- name: Hello Local
ansible.builtin.debug:
msg: "Hello local"
- name: Copy files to local machine
ansible.builtin.copy:
src: files/note01.txt
dest: ~/
---
- name: First
hosts: cisco
connection: local
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment