Ansible: Setup and Install ansible on Centos 7

From Define Wiki
Jump to navigation Jump to search

Basic Setup

    • 4 Nodes on vScaler each with Centos 7 image (1 system with floating IP access / all nodes on internal private network)
    • SSH keys setup between hosts for both users: centos/root
    • SSH in to all nodes to prevent prompt about host authticity
    • /etc/hosts setup on all nodes

Install and Configure Ansible

  • Install ansible on the server (not needed on clients)
# Install the RPMs
yum -y install epel-release 
yum -y install ansible
  • Setup hosts for ansible
# in the file: /etc/ansible/hosts
# this assumes all servers are part of the same group: servers
[servers]
#at1 ansible_ssh_host=192.168.1.27 (this is our server and does not need to be in there) 
at2 ansible_ssh_host=192.168.1.28
at3 ansible_ssh_host=192.168.1.30
at4 ansible_ssh_host=192.168.1.29
  • Check and make sure ansible can talk to all the hosts
[root@ansible-test-1 ~]# ansible -m ping all 
at2 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
at4 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
at3 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
  • Some examples of ansible commands
# Ping a group of hosts
[root@ansible-test-1 ~]# ansible -m ping servers
at3 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
at2 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
at4 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}

# ping a single host
[root@ansible-test-1 ~]# ansible -m ping at2
at2 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}

# Ping a range of hosts
[root@ansible-test-1 ~]# ansible -m ping at2:at4
at2 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
at4 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}

# Run a shell command on a node
[root@ansible-test-1 ~]# ansible -m shell -a 'free -m' at2
at2 | SUCCESS | rc=0 >>
              total        used        free      shared  buff/cache   available
Mem:           3791          95        3428          16         266        3478
Swap:             0           0           0