<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Tu Van Nguyen</title>
    <description>The latest articles on DEV Community by Tu Van Nguyen (@tuvannguyen).</description>
    <link>https://dev.to/tuvannguyen</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F368520%2F16441b6d-739f-4399-a1e7-d4140288ce3c.png</url>
      <title>DEV Community: Tu Van Nguyen</title>
      <link>https://dev.to/tuvannguyen</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/tuvannguyen"/>
    <language>en</language>
    <item>
      <title>Configure a Web Server with Ansible Playbook</title>
      <dc:creator>Tu Van Nguyen</dc:creator>
      <pubDate>Fri, 24 Apr 2020 19:27:39 +0000</pubDate>
      <link>https://dev.to/tuvannguyen/configure-a-web-server-with-ansible-playbook-38jb</link>
      <guid>https://dev.to/tuvannguyen/configure-a-web-server-with-ansible-playbook-38jb</guid>
      <description>&lt;p&gt;If you ever need to configure an army of servers, then Ansible playbooks are your best friend. Playbooks are YAML files that contain desired state of the "hosts", the remote machines that Ansible controls. In this tutorial, we are going to create a playbook that sets up a web server on a virtual machine. This virtual machine is a managed node for Ansible, and you can see &lt;a href="https://dev.to/tuvannguyen/quickstart-ansible-with-virtual-machines-5ep8"&gt;how to set it up in this other tutorial&lt;/a&gt;. &lt;/p&gt;

&lt;h1&gt;
  
  
  Write the webpage
&lt;/h1&gt;

&lt;p&gt;This is just a very simple html page that we'll write on the local machine. Name it index.html. Later on, this file will be transferred to the managed node.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;header&amp;gt;
 &amp;lt;h1&amp;gt;Hello World. I am an nginx application. Thanks for visiting me!&amp;lt;/h1&amp;gt;
&amp;lt;/header&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h1&gt;
  
  
  Write the Playbook
&lt;/h1&gt;

&lt;p&gt;The web server we'll be using is Nginx. We will write a simple playbook that tells Ansible that the managed host should have nginx installed and running. We communicate this by writing tasks, a task being the unit of action in Ansible. Tasks related to installation use the yum module for the package manager of CentOS on the virtual machine. The third task adds the index.html file from the local machine to the managed node. Then in the fifth task, we open tcp port 80 so that we can access the webpage from the local machine's browser. The final task starts the nginx service.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#nginx.yml
---
 - hosts: 192.168.56.3
   user: root
   tasks:
    - name: Add epel-repo
      yum:
       name: epel-release
       state: present

    - name: Install Nginx
      yum:
       name: nginx
       state: present

    - name: add index.html file
      template:
       src: ./index.html
       dest: /usr/share/nginx/html/index.html

    - name: allow all access to tcp port 80
      firewalld:
       port: 80/tcp
       zone: public
       state: enabled

    - name: Start Nginx
      service:
       name: nginx
       state: started
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h1&gt;
  
  
  Testing
&lt;/h1&gt;

&lt;p&gt;Now we can run the playbook and test the web server from the local machine or control node.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ansible-playbook nginx.yml
curl 192.168.56.3:80
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;You can also view the webpage by going to &lt;code&gt;192.168.56.3:80&lt;/code&gt; on your web browser.&lt;/p&gt;

&lt;p&gt;And that's all you need to do to set up the web server. After using Ansible for a while, consider getting a That Was Easy button for every time you can sit back and let Ansible do all the work for you. &lt;/p&gt;

</description>
      <category>ansible</category>
      <category>devops</category>
      <category>linux</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Quickstart Ansible With Virtual Machines</title>
      <dc:creator>Tu Van Nguyen</dc:creator>
      <pubDate>Tue, 21 Apr 2020 18:39:21 +0000</pubDate>
      <link>https://dev.to/tuvannguyen/quickstart-ansible-with-virtual-machines-5ep8</link>
      <guid>https://dev.to/tuvannguyen/quickstart-ansible-with-virtual-machines-5ep8</guid>
      <description>&lt;p&gt;Compared to similar tools such as Puppet and Chef, Ansible is probably the best tool to get started in configuration management. It is agentless, so it doesn't require client software to be installed on the machines that will be managed by Ansible. Instead, Ansible controls the other machines through OpenSSH. This does mean that Ansible needs to be installed on a Linux machine. With this simple tutorial, you can get started with setting up remote machines to be controlled with Ansible.&lt;/p&gt;

&lt;h4&gt;
  
  
  System Requirements
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Linux local machine (e.g Debian, Ubuntu, CentOs)&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.ansible.com/ansible/latest/installation_guide/intro_installation.html"&gt;Ansible installed on host OS&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.virtualbox.org/wiki/Downloads"&gt;Virtualbox Installed&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Create the Virtual Machine
&lt;/h1&gt;

&lt;p&gt;Download the ISO file for the guest OS. This tutorial uses the &lt;a href="https://wiki.centos.org/Download"&gt;CentOS 7 Minimal ISO image&lt;/a&gt; because it's lightweight and installs without a GUI. So, it'll take up little memory and install quickly. To ensure the downloaded ISO image is secure, verify the file's SHA256 checksum: &lt;br&gt;
&lt;code&gt;echo "&amp;lt;known SHA 256 sum of the file&amp;gt; &amp;lt;ISO file name&amp;gt;" | sha256sum -c&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.virtualbox.org/manual/ch01.html"&gt;Create the guest machine on virtualbox&lt;/a&gt;, and install the OS onto the guest. The guest machine can have the following specs:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;OS: CentOS 7 Minimal&lt;/li&gt;
&lt;li&gt;Base Memory: 1024 MB&lt;/li&gt;
&lt;li&gt;Hard Disk: 15 GB, Dynamically Allocated&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;After login in as root, create a user such as "testuser" and specify the password. You need to create this user on the guest machine.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;useradd testuser
passwd testuser
usermod -aG wheel testuser #makes testuser sudoer on CentOS
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Next, &lt;a href="https://dev.to/isabolic99/how-to-set-host-only-adapter-on-vm-virtual-box-2jka"&gt;add a host-only adapter&lt;/a&gt; to its network configurations. This allows the host to communicate with the guest on a static IP, so that Ansible can communicate with the guest through SSH.&lt;/p&gt;

&lt;p&gt;The IPv4 address for the host adapter can be left as the default 192.168.56.1. But the address that Ansible will use to connect to the machine will be slightly different. This address can be found by running &lt;code&gt;ip address&lt;/code&gt; on the guest. From the output below, the address is 192.168.56.3.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[testuser@localhost ~]$ ip address
...
3: eth0: &amp;lt;BROADCAST,MULTICAST,UP,LOWER_UP&amp;gt; mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 08:00:27:c3:b8:38 brd ff:ff:ff:ff:ff:ff
    inet 192.168.56.3/24 brd 192.168.56.255 scope global noprefixroute dynamic eth0

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Test the network connection to the virtual machine using &lt;code&gt;ping&lt;/code&gt; and &lt;code&gt;ssh&lt;/code&gt;. If the connection succeeds, the guest is ready to be used with Ansible.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ping 192.168.56.1 # make sure the host-only adapter is working
ssh testuser@192.168.56.3 # make sure host can ssh to virtual machine
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h1&gt;
  
  
  Test Ansible with the Virtual Machine
&lt;/h1&gt;

&lt;p&gt;Add the virtual machine to an Ansible inventory by the IPv4 address to &lt;code&gt;/etc/ansible/hosts&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Virtual Machine Managed Nodes
192.168.56.3
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now, we can run some basic commands on Ansible to see if it can control the virtual machine.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ansible all -m ping -u testuser --ask-pass
ansible all -a "/bin/echo hello" -u testuser --ask-pass
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h4&gt;
  
  
  Now Give Ansible Access with SSH-keys
&lt;/h4&gt;

&lt;p&gt;To make using ansible a bit easier and more secure, we'll be replacing user-password authentication with SSH keys. We'll need a user on the virtual machine with the same name as the user on the local machine. This tutorial will just use root, but it's highly recommended to create a separate user.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#On the local machine

su #switch user
ssh-keygen #generate SSH key-pair
ssh-copy-id root@192.168.56.3 # copy ssh key to virtual machine

ssh root@192.168.56.3 #test connection
ansible all -m ping #test ansible
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Congratulations! Now you can get started learning how to get Ansible to command remote machines to do more complex tasks.&lt;/p&gt;

</description>
      <category>devops</category>
      <category>virtualization</category>
      <category>ansible</category>
      <category>linux</category>
    </item>
  </channel>
</rss>
