Install Ansible on Ubuntu 20.04 and run ansible-playbook

  1. create user ID and sudo rules
    create ID ansible on all Linux:
    sudo adduser ansible
    echo "ansible ALL=(ALL) NOPASSWD:ALL" | sudo tee /etc/sudoers.d/ansible
  2. generate SSH key and copy to client servers:
    ssh-keygen
    ssh-copy-id ansible@192.168.0.143
    ssh-copy-id ansible@192.168.0.61
  3. install ansible:
    sudo apt update
    sudo apt install ansible
  4. add client servers:
    sudo vi /etc/ansible/hosts
    [servers]
    server1 ansible_host=192.168.0.143
    server2 ansible_host=192.168.0.61
  5. verify ansible hosts and run playbook
    sudo ansible-inventory –list -y
    mkdir ~/ansible-demo
    create few ansible-playbook in ~/ansible-demo:

    
    ansible@ubunu2004:~/ansible-demo$ cat install-apt.yml
    ---
    - hosts: all
    become: yes
    tasks:
    - name: Install packages
    apt:
      name:
      - ntpdate
      - nmap
      state: latest
      cache_valid_time: 3600
    ansible@ubunu2004:~/ansible-demo$ cat linux-echo.yml
    ---
    - hosts: all
    become: yes
    tasks:
    - name: Echo the Date to a tmp file
    shell: echo  "`date`"> /tmp/date
    - name: Echo String to a tmp file
    shell: echo  "Techexpert.tips is a greate website" > /tmp/techexpert
run the playbook:
```bash
ansible@ubunu2004:~/ansible-demo$ ansible-playbook install-apt.yml

PLAY [all] ***************************************************************************************************************************************************************************************************

TASK [Gathering Facts] ***************************************************************************************************************************************************************************************
ok: [server1]
ok: [server2]

TASK [Install packages] **************************************************************************************************************************************************************************************
changed: [server1]
changed: [server2]

PLAY RECAP ***************************************************************************************************************************************************************************************************
server1                    : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
server2                    : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

Ansible Playbook Example – Reading a Variable from the Command-line
This Ansible playbook example named linux-scan.yml will install the Nmap package.
It will read the IP addres from the IP_VAR variable and use NMAP to scan this host.

---
- hosts: all
  become: yes
  tasks:
  - name: Install packages
    apt:
      name:
      - nmap
      state: latest
      cache_valid_time: 3600
  - name: Scan host using nmap
    shell: nmap "{{ ip_var }}"
    register: out

  - debug: var=out.stdout_lines

To run this Ansible playbook, use the following command:
ansible-playbook --extra-vars ip_var=192.168.0.143 linux-scan.yml

Leave a Reply

Your email address will not be published. Required fields are marked *