Ansible Essentials: get started to use Ansible playbook

video tutorial

https://www.ansible.com/resources/webinars-training/introduction-to-ansible

https://www.ansible.com/resources/videos/quick-start-video?extIdCarryOver=true&sc_cid=701f2000001OH7YAAW

https://www.ansible.com/resources/get-started

yingshaoxo: Those are useful videos for you to understand ansible.

installation

sudo apt install ansible sshpass

books

  • Vincent Sesto - Practical Ansible_ Configuration Management from Start to Finish-Apress (2021)

1. build a Linux server that has ssh service available

reference from: https://dev.to/s1ntaxe770r/how-to-setup-ssh-within-a-docker-container-i5i

# Dockerfile

FROM ubuntu:latest

RUN apt update && apt install  openssh-server sudo -y

RUN useradd -rm -d /home/ubuntu -s /bin/bash -g root -G sudo -u 1000 test 

RUN  echo 'test:test' | chpasswd

RUN service ssh start

EXPOSE 22

CMD ["/usr/sbin/sshd","-D"]

build

sudo docker build -t sshd .

use

sudo docker run --name sshd -p 2222:22 -d sshd

stop

sudo docker stop sshd
sudo docker rm sshd

connect

ssh-copy-id test@127.0.0.1 -p 2222
ssh test@127.0.0.1 -p 2222
#password is 'test'

2. use ansible to run commands over your server 

configuration

# inventory
[App1]
127.0.0.1:2222

[App1:vars]
ansible_connection=ssh
ansible_user=test
ansible_ssh_pass=test
ansible_sudo_pass=tes
# test.yml
---
- name: My task 1
  hosts: App1
  tasks:
    - name: print out who you are, test
      remote_user: test
      command: "/bin/whoami"
    - name: print out who you are, root
      remote_user: test
      command: "/bin/whoami"
      become: true

usage

ansible all -i inventory -u test -m ping

ansible all -i inventory -u test -a "/bin/echo hello"

ansible-playbook -i inventory test.yml -v

More info

https://docs.ansible.com/ansible/latest/user_guide/playbooks.html

https://yingshaoxo.blogspot.com/2021/05/ansible-essentials-get-started-to-use.html