AnsiblePart1


Ansible

is a config management tool,sample inventory file is /etc/ansible/hosts

sudo apt install ansible or pip install ansible and /etc/ansible/ansible.cfg is the config file

ansible -m ping localhost --ask-pass will give a message that sshpass needs to be installed, install using sudo apt install sshpass

ssh user@localhost, will ask for ssh password of the user you are logging in once

successful run ansible -m ping localhost --ask-pass again you should see succesful ping pong message

# This is the default ansible 'hosts' file. It should live in /etc/ansible/hosts

## green.example.com

## 192.168.100.1

[webservers]
## alpha.example.org

## beta.example.org

192.168.33.10  ansible_user=vagrant

## www[001:006].example.com

## [dbservers]

## db01.intranet.mydomain.net

## db02.intranet.mydomain.net

## db-[99:101]-node.example.com

[appservers]
192.168.33.11  ansible_user=vagrant

[webservers:vars]
ansible_password=vagrant

Ansible ad hoc commands

ansible -m ping localhost --ask-pass

sudo ansible localhost -m "date" --ask-pass

ansible -m setup localhost --ask-pass

ansible -b -m apt -a "name=ntp state=present"  localhost --ask-pass -vvvv # -vvvv is verbose 

ansible -b -a "service ntp start" localhost -k # k is the same as ask-pass

ansible -b -a "service ntp stop" localhost -k

ansible -m ping localhost -k

ansible localhost -b -a "tail /var/log/nginx/error.log" -k

ansible localhost -b -m shell -a "tail /var/log/nginx/error.log" -k

** ansible-doc command **

ansible-doc service for example

Playbook file

Playbook files are YAML files, Be careful with indentation and do not use tab and always spaces

ansible-playbook --syntax-check playbooktest.yml 

- hosts: centos
  remote_user: root
  
  gather_facts: no

  tasks:
  - name: test ping
     ping:
     remote_user: root
  - name: check ssh
      service:
        name: sshd
        state: started
  - name: check nginx
      service:
        name: nginx
        state:started
     

where centos is a group of servers,remote_user will run as root,facts are disabled


These writings represent my own personal views alone.
Licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.