Ansible Notes - Oliver-Mustoe/Oliver-Mustoe-Tech-Journal GitHub Wiki

In this page I detail how I setup ansible along with a Playbook example.

Notes

First I installed Ansible on the Linux workstation I was working on with the command:

  • sudo apt install ansible

Then I added my ssh key to the hosts I wanted to use (if I didn't have a key I would create one with the command "ssh-keygen -t rsa -b 4096") with the command:

  • ssh-copy-id < USER >@< HOST >
    NOTE: Replace the sections that are <> with relevant information.

Then I edited the file /etc/ansible/hosts with the command:

  • sudo nano /etc/ansible/hosts

After that I added the following information to the file:

 [< GROUP NAME >:vars]  
 ansible_user=< ENTER A USERNAME HERE!!!!! >  
 ansible_become=yes  
 ansible_become_method=sudo  
   
 [< GROUP NAME >]  
 < EXAMPLE_IP >  
 < EXAMPLE_IP >  

NOTE: Replace the sections that are <> with relevant information. If you are using systems without duplicate accounts (same username and pass), you can not use this configuration.

To test my configuration is working I ran the command, and entered my password at the prompt:

  • ansible CentOSSystems -m ping --ask-become-pass
    (This will return green messages.)

Playbook example - upgrade all

First I created a YAML file, file format for Ansible playbooks, with the command:

  • nano upgrade.yml

Where I entered the following text:

 ---  
 - name: Check for upgrades  
   hosts: all  
   become: true  

   tasks:  
    - name: Upgrading  
      yum:  
        name: "*"  
        state: latest 

Which will upgrade all of my hosts in my /etc/ansible/hosts, to run the playbook enter the command:

  • ansible-playbook --ask-become-pass upgrade.yml

Sources

https://techviewleo.com/upgrade-reboot-linux-systems-using-ansible/
https://docs.ansible.com/ansible/latest/user_guide/intro_getting_started.html
https://docs.ansible.com/ansible/2.3/playbooks_checkmode.html
https://www.digitalocean.com/community/cheatsheets/how-to-use-ansible-cheat-sheet-guide
https://docs.ansible.com/ansible/2.3/yum_module.html
https://www.cyberciti.biz/faq/how-to-set-and-use-sudo-password-for-ansible-vault/

⚠️ **GitHub.com Fallback** ⚠️