Thursday, December 3, 2020

LAB2 - Running ansible ad-hoc commands

 Running ansible ad-hoc commands


1. Get ansible version
# ansible --version
- Get ansible/python version
- Get the config file location
- and get the inventory file location

2. Check the communication with managed nodes
# ansible all -m ping

3. Copying files to worker node
- Lets google for copy module: ansible copy file module
- search for ansible service module and review the doc and review the example as well.


4. Lets install httpd manually first on one of the worker node
# yum install httpd
# systemctl start httpd
# cd /var/www/html
# cat > index.html
Welcome to my web site !!!

5. go to browser and test it. You should have access to the page.
If you encounter proble, disable firewall, selinux for testing purpose.

6. Now, remove httpd
# systemctl stop httpd
# yum remove httpd -y
# rpm -q httpd # verify
# ls -ld /var/www/html # you can't list this dir

7. Now, go to control node and run ad-hoc command
# ansible --version
# ansible all --list-hosts
# cat > index.html
Welcome to ansible pushed page !!!
# ansible all -m package -a "name=httpd state=present"
# ansible all -m copy -a "src=index.html dest=/var/www/html/"
# ansible all -m service -a "name=httpd state=started"
# ansible worker2 -m service -a "name=httpd state=started"
# ansible worker2 -m copy -a "src=copyme.html dest=/var/www/html/"
go to worker2 node and check
# systemctl status httpd
[root@master ~]# cat copyme.html
Copied from ctrl node
[root@worker2 html]# cat copyme.html
Copied from ctrl node

# ansible 192.169.10.51 -m service -a "name=httpd state=started
-------------------------------
Now, lets go ahead and setup web server.

1. Install httpd package
# ansible all -m package -a "name=httpd state=present"
We are using package module here.

2. Copy webpage to web server
# ansible all -m copy -a "src=copyme.html dest=/var/www/html/"

3. Start web server httpd on managed node from control node
# ansbile all -m service -a "name=httpd state=started"

8. Lets automate this manu task by writing a config file
# cat webpage.yaml
- hosts: myweb
  tasks:
    - package: "name: "httpd state=present"
    - copy: "src=copyme.html dest=/var/www/html/"
    - service: "name=httpd state=started"

# ansible-playbook webpage.yaml




[root@master ~]# cat webserver.yaml
- hosts: all
  tasks:
  - file:
      state: directory
      path: "/dvd1"

  - mount:
      src: "/dev/cdrom"
      path: "/dvd1"
      state: mounted
      fstype: "iso9660"
# add entry to fstab
# task is a list of three task such as file, mount and yum.
# these belongs to same block of code, so same space..

  - yum_repository:
      baseurl: "/dvd1/AppStream"
      name: "mydvd1"
      description: "My yum repo"
      gpgcheck: no

  - yum_repository:
      baseurl: "/dvd1/BaseOS"
      name: "mydvd2"
      description: "My yum repo 2"
      gpgcheck: no

  - package:
      name: "httpd"
      state: present
  - copy:
      dest: "/var/www/html/index.html"
      content: "Welcome to my web page. Enjoy !!!"

  - firewalld:
      port: "80/tcp"
      state: enabled
      permanent: yes
      immediate: yes
[root@master ~]#
[root@master ~]# cat myhosts
#[masterserver]
#master ansible_user=sam

#[WebServer]
#worker1
#worker1 ansible_user=sam
#worker2 ansible_user=sam
[mylb]
master  ansible_user=root ansible_ssh_pass=changeme ansible_connection=ssh
[myweb]
worker1 ansible_user=root ansible_ssh_pass=changeme ansible_connection=ssh
worker2 ansible_user=root ansible_ssh_pass=changeme ansible_connection=ssh
[root@master ~]#

[root@master ~]# cat lb.yaml
- hosts: myweb  # myweb comes from
  tasks:
  - package:
      name: "httpd"
  - copy:
      dest: "/var/www/html/index.html"
      content: " for lb testing"

  - service:
      name: "httpd"
      state: restarted

- hosts: mylb
  tasks:
  - name: "Install LB software"
    package:
      name: "haproxy"

  - template:
      dest: "/etc/haproxy/haproxy.cfg"
      src: "haproxy.cfg"

  - service:
      name: "haproxy"
      state: restarted
[root@master ~]#

[root@master ~]# cat repo.docker.repo
- hosts: 192.168.10.50
  tasks:
  - name: setting up docker yum confuration
    yum_repository:
      name: df
      description: EPEL yum repo
      file: external_repo
      baseurl: https://download.docker.con/linux/centos/7/x86_64/stable/
      gpgcheck: no

# install the package
  - package:
      name: "docker-ce"
      state: present

  - command: "yum install docker-ce --nobest -v"

[root@master ~]#

[root@master ~]# cat copyme.txt
Copied from ctrl node
[root@master ~]#

[root@master ~]# cat web.html
Welcome to my page for ansible
[root@master ~]#

[root@master ~]# cat pod-definition.yml
apiVersion: v1

kind: Pod

metadata:
  name: myapp-pod
  labels:
    app: myapp

spec:
  containers:
     - name: nginx-container
       image: nginx
[root@master ~]#


No comments:

Post a Comment

Git branch show detached HEAD

  Git branch show detached HEAD 1. List your branch $ git branch * (HEAD detached at f219e03)   00 2. Run re-set hard $ git reset --hard 3. ...