Learning – Ansible 101 – Episode 2 – Ad-hoc tasks and Inventory

Learning - Ansible 101 - Episode 2 - Ad-hoc tasks and Inventory

Run ad-hoc tasks using Ansible.

Vagrant Cloud

Vagrant init

vagrant init geerlingguy/centos7

Vagrantfile

Vagrant.configure("2") do |config|
  config.vm.box = "geerlingguy/centos7"

  config.ssh.insert_key = false

  config.vm.synced_folder ".", "/vagrent", disabled: true

  config.vm.provider :virtualbox do |v|
    v.memory= 256
    v.linked_clone = true
  end

  # App server 1
  config.vm.define "app1" do |app|
    app.vm.hostname = "orc-app1.test"
    app.vm.network :private_network, ip: "192.168.60.4"
  end

  # App server 2
  config.vm.define "app2" do |app|
    app.vm.hostname = "orc-app2.test"
    app.vm.network :private_network, ip: "192.168.60.5"
  end

  # DB server
  config.vm.define "db" do |db|
    db.vm.hostname = "orc-db.test"
    db.vm.network :private_network, ip: "192.168.60.6"
  end
end
vagrant up

Create inventory file

# Application servers
[app]
192.168.60.4
192.168.60.5

# Database server
[db]
192.168.60.6

# Group has all the servers
[multi:children]
app
db

# Variable for all the servers
[multi:vars]
ansible_ssh_user=vagrant
ansible_ssh_private_key_file=~/.vagrant.d/insecure_private_key

Run Ansible

ansible multi -i inventory -a "hostname"

Note: accept host key if prompt, or change the .ssh/config file to ignore host key

To run one by one,

ansible multi -i inventory -a "hostname" -f 1

Run ansible to query configuration

ansible -i inventory db -m setup

Install NTP

ansible -i inventory multi -b -m yum -a "name=ntp state=present"

ansible -i inventory multi --become -m yum -a "name=ntp state=present"

Note: Run the second time, the status will be SUCCESS, not CHANGED

Ask for sudo password

ansible -i inventory multi -K -m yum -a "name=ntp state=present"

Note: for this image, the vagrant user has password vargent

Check service status

ansible -i inventory multi -b -m service -a "name=ntpd state=started enabled=yes"

Check service options

  • Use google search "ansible service module".

  • Use ansible-doc command

ansible-doc service

NTP commands

ansible -i inventory multi -b -a "service ntpd stop"
ansible -i inventory multi -b -a "ntpdate -q 0.rhel.pool.ntp.org"

my_sql

Setup user on mysql server.

ansible -i inventory db -b -m mysql_user -a "name=django host=% password=12345 priv=*.*:ALL state=present"

Limit to one server

ansible -i inventory multi -a "free -m" --limit '192.168.60.4'
ansible -i inventory multi -a "free -m" --limit '*.4'
ansible -i inventory multi -a "free -m" --limit '!192.168.60.4'
ansible -i inventory multi -a "free -m" --limit '!192.168.60.4:!192.168.60.5'

References

Ansible 101 - Episode 2 - Ad-hoc tasks and Inventory

Leave a Reply

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

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>


The reCAPTCHA verification period has expired. Please reload the page.