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 @@ ...@@ -5,12 +5,12 @@
tasks: tasks:
- name: Copy source code file to remote host - name: Copy source code file to remote host
copy: ansible.builtin.copy:
src: files/small-flask-app src: files/small-flask-app
dest: /home/jay/app dest: /home/jay/app
# - name: Build image # - name: Build image
# docker_image: # ansible.builtin.docker_image::
# build: # build:
# path: /home/jay/app/small-flask-app # path: /home/jay/app/small-flask-app
# name: small-flask-app # name: small-flask-app
...@@ -18,12 +18,12 @@ ...@@ -18,12 +18,12 @@
# source: build # source: build
- name: Pull Docker image - name: Pull Docker image
docker_image: ansible.builtin.docker_image:
name: garovu/small-flask-app name: garovu/small-flask-app
source: pull source: pull
- name: Run Docker container - name: Run Docker container
docker_container: ansible.builtin.docker_container:
name: small-flask-app name: small-flask-app
image: garovu/small-flask-app image: garovu/small-flask-app
ports: ports:
......
--- ---
- name: Install Docker and manage containers - name: Install Docker and manage containers
hosts: all hosts: all
become: yes become: true
tasks: tasks:
- name: Install required packages - name: Install required packages
apt: ansible.builtin.apt:
name: name:
- apt-transport-https - apt-transport-https
- ca-certificates - ca-certificates
...@@ -17,22 +16,21 @@ ...@@ -17,22 +16,21 @@
when: ansible_os_family == 'Debian' when: ansible_os_family == 'Debian'
- name: Add Docker GPG key - name: Add Docker GPG key
apt_key: ansible.builtin.apt_key:
url: https://download.docker.com/linux/ubuntu/gpg url: https://download.docker.com/linux/ubuntu/gpg
when: ansible_os_family == 'Debian' when: ansible_os_family == 'Debian'
- name: Add Docker repository - 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 repo: deb [arch=arm64] https://download.docker.com/linux/ubuntu jammy stable
state: present state: present
when: ansible_os_family == 'Debian' when: ansible_os_family == 'Debian'
- name: Install Docker - name: Install Docker
apt: ansible.builtin.apt:
name: name:
- docker-ce - docker-ce
- docker-ce-cli - docker-ce-cli
- containerd.io - containerd.io
state: present state: present
when: ansible_os_family == 'Debian' when: ansible_os_family == 'Debian'
--- ---
- hosts: all - name: My First Playbook
become: yes hosts: all
tasks: tasks:
- name: Ping my hosts - name: Ping my hosts
ansible.builtin.ping: ansible.builtin.ping:
- name: Print message - name: Print message through dedug modune
ansible.builtin.debug: ansible.builtin.debug:
msg: Hello world msg: Hello world
- name: Install neofetch - name: Install and run service in block
apt: when: ansible_facts["distribution"] == "Ubuntu"
name: neofetch become: true # just use sudo for task in this block
state: present 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 - name: Write hostname
hosts: all hosts: all
become: true
tasks: 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: ansible.builtin.template:
src: files/templates/test.j2 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