Configure 5 Linux servers with one block code in playbbok ansible_local

19 views
Skip to first unread message

BenZ

unread,
Aug 28, 2018, 3:58:47 AM8/28/18
to Ansible Project
Hi, 
I am using vagrant to create 5 servers and using ansible to configure each of them to install Nginx and add the the sentence "hello from web server{server number}.
I am trying to understand what is best way to configure the playbook to configure the servers in one block. Now I use 5 blocks that have the same code only the server number is changed(web1, web2,web3, etc) and the number I write to the index file.I am using ansible_local. 
  

vagrantfile


   
Vagrant.configure("2") do |config|
   
   
        N
= 5
       
(1..N).each do |machine_id|
            config
.vm.define "web#{machine_id}" do |machine|
            machine
.vm.box = "ubuntu/trusty64"
            machine
.vm.hostname = "web"
            machine
.vm.network "private_network", ip: "10.0.0.#{10+machine_id}"
         
end
           
        config
.vm.provision "ansible_local" do |ansible|
        ansible
.playbook = "playbook.yml"
        ansible
.become = true
        ansible
.extra_vars = { machine: "web#{machine_id}" }
        ansible
.groups = {
             
"group1" => ["web"],
       
}
         
end
       
end
       



playbook code





   
- hosts: web1
      become
: true
      tasks
:
       
- name: ensure nginx is at the latest version
          apt
: name=nginx state=latest
       
- name: start nginx
          service
:
              name
: nginx
              state
: started
       
- name: add server name to nginx html
          lineinfile
:
           dest
: /usr/share/nginx/html/index.html
           insertafter
: "^<p><em>Thank you for using nginx."
           line
: <p>hello from web server-1"</p>

Reply all
Reply to author
Forward
0 new messages