In this article,we will see Push Code From Ansible Server to Hosts by using Modules.
We push the code from ansible server to hosts by using modules.
We execute the modules by running playbooks.
Modules
- Ansible modules are discrete units of code which can be used from the command line or in a playbook task.
- The modules also referred to as task plugins or library plugins in the Ansible.
- Ansible ships with several modules that are called module library, which can be executed directly or remote hosts through the playbook.
- We can execute the modules from the command line.
- Ansible executes each module, usually on the remote target node, and collects return values.
- Each module supports taking arguments.
- Nearly all modules take key=value arguments, space delimited.
- All modules return JSON format data.
- This means modules can be written in any programming language.
- Modules should be idempotent, and should avoid making any changes if they detect that the current state matches the desired final state.
- we can run mutiple modules by using playbooks.
Playbooks
- We execute the multiple modules by using playbooks.
- Playbooks are the files where the Ansible code is written.
- Playbooks are written in YAML format.
- YAML means “Yet Another Markup Language,” so there is not much syntax needed.
- Playbooks are one of the core features of Ansible and tell Ansible what to execute, and it is used in complex scenarios.
- They offer increased flexibility.
- Each playbook is composed of one or more ‘plays’ in a list.
- Through a playbook, you can designate specific roles to some of the hosts and other roles to other hosts.
- By doing this, you can orchestrate multiple servers in very different scenarios, all in one playbook.
- In the playbook we have many sections are included as show below.
Sections in Ansible Playbooks
- Target Section
- Task Section
- Variables Section
- Handler Section
- Loops Section
- Conditionals Section
Target Section
Gathering Facts
We can collect the information about remoting hosts by using gather_facts module.
Create a playbook “kt-target.yml”
1 2 3 4 5 6 7 |
[kt-ansible@ip-172-31-15-116 ~]$ vi kt-target.yml --- # My First YAML playbook - hosts: Ktexperts-Group user: ansible become: yes # yes or no connection: ssh # ssh or paramico gather_facts: yes # yes or no |
Note
:wq! —– to quit.
Run ansible-playbook to call the playbook “kt-target.yml”
1 2 3 4 5 6 7 8 9 10 11 |
[kt-ansible@ip-172-31-15-116 ~]$ ansible-playbook kt-target.yml PLAY [Ktexperts-Group] ******************************************************************************************************************************************************************* TASK [Gathering Facts] ******************************************************************************************************************************************************************* ok: [172.31.2.38] ok: [172.31.11.251] PLAY RECAP ******************************************************************************************************************************************************************************* 172.31.11.251 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 172.31.2.38 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 |
Task Section
Install the package “httpd”
Create a playbook “kt-tasks.yml”
1 2 3 4 5 6 7 8 9 |
[kt-ansible@ip-172-31-15-116 ~]$ vi kt-tasks.yml --- # My tasks YAML playbook (Target and Tasks)i - hosts: Ktexperts-Group user: kt-ansible become: yes connection: ssh tasks: - name: Install HTTPD on Amazon Linux action: yum name=httpd state=installed |
Note
:wq! —– to quit.
Run ansible-playbook to call the playbook “kt-tasks.yml”
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
[kt-ansible@ip-172-31-15-116 ~]$ ansible-playbook kt-tasks.yml PLAY [Ktexperts-Group] ******************************************************************************************************************************************************************* TASK [Gathering Facts] ******************************************************************************************************************************************************************* ok: [172.31.2.38] ok: [172.31.11.251] TASK [Install HTTPD on Amazon Linux] ***************************************************************************************************************************************************** ok: [172.31.11.251] ok: [172.31.2.38] PLAY RECAP ******************************************************************************************************************************************************************************* 172.31.11.251 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 172.31.2.38 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 |
Verify the package inside 2 hosts(Nodes)
1 2 3 4 5 6 7 |
[kt-ansible@ip-172-31-15-116 ~]$ ansible Ktexperts-Group -b -a "which httpd" 172.31.2.38 | CHANGED | rc=0 >> /sbin/httpd 172.31.11.251 | CHANGED | rc=0 >> /sbin/httpd |
Note
:wq! —– to quit.
Remove package “httpd” in 2 hosts
Modify the playbook “kt-tasks.yml”
1 2 3 4 5 6 7 8 9 10 |
[kt-ansible@ip-172-31-15-116 ~]$ vi kt-tasks.yml --- # My tasks YAML playbook (Target and Tasks)i - hosts: Ktexperts-Group user: kt-ansible become: yes connection: ssh tasks: - name: Install HTTPD on Amazon Linux #action: yum name=httpd state=installed action: yum name=httpd state=removed |
Note
:wq! —– to quit.
Run ansible-playbook to call the playbook “kt-tasks.yml”
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
[kt-ansible@ip-172-31-15-116 ~]$ ansible-playbook kt-tasks.yml PLAY [Ktexperts-Group] ******************************************************************************************************************************************************************* TASK [Gathering Facts] ******************************************************************************************************************************************************************* ok: [172.31.11.251] ok: [172.31.2.38] TASK [Install HTTPD on Amazon Linux] ***************************************************************************************************************************************************** changed: [172.31.11.251] changed: [172.31.2.38] PLAY RECAP ******************************************************************************************************************************************************************************* 172.31.11.251 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 172.31.2.38 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 |
Verify the package inside 2 hosts
1 2 3 4 5 6 7 |
[kt-ansible@ip-172-31-15-116 ~]$ ansible Ktexperts-Group -b -a "which httpd" 172.31.11.251 | FAILED | rc=1 >> which: no httpd in (/sbin:/bin:/usr/sbin:/usr/bin)non-zero return code 172.31.2.38 | FAILED | rc=1 >> which: no httpd in (/sbin:/bin:/usr/sbin:/usr/bin)non-zero return code |
Variables Section
Install the package “httpd” by using variables
Create a cookbook “kt-variables.yml”
1 2 3 4 5 6 7 8 9 10 11 |
[kt-ansible@ip-172-31-15-116 ~]$ vi kt-variables.yml --- # My Variables YAML playbook(Target,Variables and Tasks) - hosts: Ktexperts-Group user: kt-ansible become: yes connection: ssh vars: pkgname: httpd tasks: - name: Install the package "httpd" server on Amazon Linux action: yum name='{{pkgname}}' state=installed |
Note
:wq! —– to quit.
Run ansible-playbook to call the playbook “kt-variables.yml”
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
[kt-ansible@ip-172-31-15-116 ~]$ ansible-playbook kt-variables.yml PLAY [Ktexperts-Group] ******************************************************************************************************************************************************************* TASK [Gathering Facts] ******************************************************************************************************************************************************************* ok: [172.31.2.38] ok: [172.31.11.251] TASK [Install the package "httpd" server on Amazon Linux] ******************************************************************************************************************************** changed: [172.31.2.38] changed: [172.31.11.251] PLAY RECAP ******************************************************************************************************************************************************************************* 172.31.11.251 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 172.31.2.38 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 |
Verify the package inside 2 hosts(Nodes)
1 2 3 4 5 6 7 |
[kt-ansible@ip-172-31-15-116 ~]$ ansible Ktexperts-Group -b -a "which httpd" 172.31.2.38 | CHANGED | rc=0 >> /sbin/httpd 172.31.11.251 | CHANGED | rc=0 >> /sbin/httpd |
Remove package “httpd”
Modify the playbook “kt-variables.yml”
1 2 3 4 5 6 7 8 9 10 11 12 13 |
[kt-ansible@ip-172-31-15-116 ~]$ vi kt-variables.yml --- # My Variables YAML playbook(Target,Variables and Tasks) - hosts: Ktexperts-Group user: kt-ansible become: yes connection: ssh vars: pkgname: httpd tasks: # - name: Install the package "httpd" on Amazon Linux Server # action: yum name='{{pkgname}}' state=installed - name: Remove the package "httpd" on Amazon Linux Server action: yum name='{{pkgname}}' state=removed |
Note
:wq! —– to quit.
Run ansible-playbook to call the playbook “kt-variables.yml”
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
[kt-ansible@ip-172-31-15-116 ~]$ ansible-playbook kt-variables.yml PLAY [Ktexperts-Group] ******************************************************************************************************************************************************************* TASK [Gathering Facts] ******************************************************************************************************************************************************************* ok: [172.31.2.38] ok: [172.31.11.251] TASK [Remove the package "httpd" on Amazon Linux Server] ********************************************************************************************************************************* changed: [172.31.11.251] changed: [172.31.2.38] PLAY RECAP ******************************************************************************************************************************************************************************* 172.31.11.251 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 172.31.2.38 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 |
Verify the package inside 2 hosts
1 2 3 4 5 6 7 |
[kt-ansible@ip-172-31-15-116 ~]$ ansible Ktexperts-Group -b -a "which httpd" 172.31.11.251 | FAILED | rc=1 >> which: no httpd in (/sbin:/bin:/usr/sbin:/usr/bin)non-zero return code 172.31.2.38 | FAILED | rc=1 >> which: no httpd in (/sbin:/bin:/usr/sbin:/usr/bin)non-zero return code |
Handler Section
Install the package “httpd’ and Start the service “httpd”
Create a playbook “kt-handlers.yml”
1 2 3 4 5 6 7 8 9 10 11 12 13 |
[kt-ansible@ip-172-31-15-116 ~]$ vi kt-handlers.yml --- # My Handlers YAML playbook (Target,Tasks and Handlers) - hosts: Ktexperts-Group user: kt-ansible become: yes connection: ssh tasks: - name: Install the package "httpd" on Amazon Linux Server action: yum name=httpd state=installed notify: start the service "httpd" #is called only if the action is ran & successful # handlers: - name: start the service "httpd" #has to match the notify name. Otherwise throws error # action: service name=httpd state=started |
Note
:wq! —– to quit.
Run ansible-playbook to call the playbook “kt-handlers.yml”
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
[kt-ansible@ip-172-31-15-116 ~]$ ansible-playbook kt-handlers.yml PLAY [Ktexperts-Group] **************************************************************************************************************************************** TASK [Gathering Facts] **************************************************************************************************************************************** ok: [172.31.11.251] ok: [172.31.2.38] TASK [Install the package "httpd" on Amazon Linux Server] ***************************************************************************************************** changed: [172.31.11.251] changed: [172.31.2.38] RUNNING HANDLER [start the service "httpd"] ******************************************************************************************************************* changed: [172.31.2.38] changed: [172.31.11.251] PLAY RECAP **************************************************************************************************************************************************** 172.31.11.251 : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 172.31.2.38 : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 |
Verify the package “httpd’ and status of the service “httpd”
Verify the package “httpd’
1 2 3 4 5 6 7 |
[kt-ansible@ip-172-31-15-116 ~]$ ansible Ktexperts-Group -b -a "which httpd" 172.31.11.251 | CHANGED | rc=0 >> /sbin/httpd 172.31.2.38 | CHANGED | rc=0 >> /sbin/httpd |
Verify the status of service “httpd”
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
[kt-ansible@ip-172-31-15-116 ~]$ ansible Ktexperts-Group -b -a "service httpd status" 172.31.11.251 | CHANGED | rc=0 >> ● httpd.service - The Apache HTTP Server Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled) Active: active (running) since Tue 2020-01-28 10:26:51 UTC; 18s ago Docs: man:httpd.service(8) Main PID: 15021 (httpd) Status: "Total requests: 0; Idle/Busy workers 100/0;Requests/sec: 0; Bytes served/sec: 0 B/sec" CGroup: /system.slice/httpd.service ├─15021 /usr/sbin/httpd -DFOREGROUND ├─15022 /usr/sbin/httpd -DFOREGROUND ├─15023 /usr/sbin/httpd -DFOREGROUND ├─15024 /usr/sbin/httpd -DFOREGROUND ├─15025 /usr/sbin/httpd -DFOREGROUND └─15026 /usr/sbin/httpd -DFOREGROUND Jan 28 10:26:51 ip-172-31-11-251.ap-south-1.compute.internal systemd[1]: Starting The Apache HTTP Server... Jan 28 10:26:51 ip-172-31-11-251.ap-south-1.compute.internal systemd[1]: Started The Apache HTTP Server.Redirecting to /bin/systemctl status httpd.service 172.31.2.38 | CHANGED | rc=0 >> ● httpd.service - The Apache HTTP Server Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled) Active: active (running) since Tue 2020-01-28 10:26:51 UTC; 18s ago Docs: man:httpd.service(8) Main PID: 13860 (httpd) Status: "Total requests: 0; Idle/Busy workers 100/0;Requests/sec: 0; Bytes served/sec: 0 B/sec" CGroup: /system.slice/httpd.service ├─13860 /usr/sbin/httpd -DFOREGROUND ├─13861 /usr/sbin/httpd -DFOREGROUND ├─13862 /usr/sbin/httpd -DFOREGROUND ├─13863 /usr/sbin/httpd -DFOREGROUND ├─13864 /usr/sbin/httpd -DFOREGROUND └─13865 /usr/sbin/httpd -DFOREGROUND Jan 28 10:26:51 ip-172-31-2-38.ap-south-1.compute.internal systemd[1]: Starting The Apache HTTP Server... Jan 28 10:26:51 ip-172-31-2-38.ap-south-1.compute.internal systemd[1]: Started The Apache HTTP Server.Redirecting to /bin/systemctl status httpd.service |
Stop the service “httpd” inside 2 hosts
1 2 3 4 5 6 7 |
[kt-ansible@ip-172-31-15-116 ~]$ ansible Ktexperts-Group -b -a "service httpd stop" 172.31.2.38 | CHANGED | rc=0 >> Redirecting to /bin/systemctl stop httpd.service 172.31.11.251 | CHANGED | rc=0 >> Redirecting to /bin/systemctl stop httpd.service |
Again Run ansible-playbook to call the playbook “kt-handlers.yml”
It won’t start service
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
[kt-ansible@ip-172-31-15-116 ~]$ ansible-playbook kt-handlers.yml PLAY [Ktexperts-Group] **************************************************************************************************************************************** TASK [Gathering Facts] **************************************************************************************************************************************** ok: [172.31.2.38] ok: [172.31.11.251] TASK [Install the package "httpd" on Amazon Linux Server] ***************************************************************************************************** ok: [172.31.2.38] ok: [172.31.11.251] PLAY RECAP **************************************************************************************************************************************************** 172.31.11.251 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 172.31.2.38 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 |
Verify the status of service “httpd”
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
[kt-ansible@ip-172-31-15-116 ~]$ ansible Ktexperts-Group -b -a "service httpd status" 172.31.11.251 | FAILED | rc=3 >> ● httpd.service - The Apache HTTP Server Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled) Active: inactive (dead) Docs: man:httpd.service(8) Jan 28 09:40:22 ip-172-31-11-251.ap-south-1.compute.internal systemd[1]: Stopping The Apache HTTP Server... Jan 28 09:40:23 ip-172-31-11-251.ap-south-1.compute.internal systemd[1]: Stopped The Apache HTTP Server. Jan 28 10:23:41 ip-172-31-11-251.ap-south-1.compute.internal systemd[1]: Starting The Apache HTTP Server... Jan 28 10:23:41 ip-172-31-11-251.ap-south-1.compute.internal systemd[1]: Started The Apache HTTP Server. Jan 28 10:26:02 ip-172-31-11-251.ap-south-1.compute.internal systemd[1]: Stopping The Apache HTTP Server... Jan 28 10:26:03 ip-172-31-11-251.ap-south-1.compute.internal systemd[1]: Stopped The Apache HTTP Server. Jan 28 10:26:51 ip-172-31-11-251.ap-south-1.compute.internal systemd[1]: Starting The Apache HTTP Server... Jan 28 10:26:51 ip-172-31-11-251.ap-south-1.compute.internal systemd[1]: Started The Apache HTTP Server. Jan 28 10:42:28 ip-172-31-11-251.ap-south-1.compute.internal systemd[1]: Stopping The Apache HTTP Server... Jan 28 10:42:29 ip-172-31-11-251.ap-south-1.compute.internal systemd[1]: Stopped The Apache HTTP Server.Redirecting to /bin/systemctl status httpd.servicenon-zero return code 172.31.2.38 | FAILED | rc=3 >> ● httpd.service - The Apache HTTP Server Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled) Active: inactive (dead) Docs: man:httpd.service(8) Jan 28 09:40:22 ip-172-31-2-38.ap-south-1.compute.internal systemd[1]: Stopping The Apache HTTP Server... Jan 28 09:40:23 ip-172-31-2-38.ap-south-1.compute.internal systemd[1]: Stopped The Apache HTTP Server. Jan 28 10:23:41 ip-172-31-2-38.ap-south-1.compute.internal systemd[1]: Starting The Apache HTTP Server... Jan 28 10:23:41 ip-172-31-2-38.ap-south-1.compute.internal systemd[1]: Started The Apache HTTP Server. Jan 28 10:26:02 ip-172-31-2-38.ap-south-1.compute.internal systemd[1]: Stopping The Apache HTTP Server... Jan 28 10:26:03 ip-172-31-2-38.ap-south-1.compute.internal systemd[1]: Stopped The Apache HTTP Server. Jan 28 10:26:51 ip-172-31-2-38.ap-south-1.compute.internal systemd[1]: Starting The Apache HTTP Server... Jan 28 10:26:51 ip-172-31-2-38.ap-south-1.compute.internal systemd[1]: Started The Apache HTTP Server. Jan 28 10:42:28 ip-172-31-2-38.ap-south-1.compute.internal systemd[1]: Stopping The Apache HTTP Server... Jan 28 10:42:29 ip-172-31-2-38.ap-south-1.compute.internal systemd[1]: Stopped The Apache HTTP Server.Redirecting to /bin/systemctl status httpd.servicenon-zero return code |
Remove the package “httpd” inside 2 hosts
Modify playbook “kt-handlers.yml”
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
[kt-ansible@ip-172-31-15-116 ~]$ vi kt-handlers.yml --- # My Handlers YAML playbook (Target,Tasks and Handlers) - hosts: Ktexperts-Group user: kt-ansible become: yes connection: ssh tasks: # - name: Install the package "httpd" on Amazon Linux Server # action: yum name=httpd state=installed # notify: start the service "httpd" #is called only if the action is ran & successful # # handlers: # - name: start the service "httpd" #has to match the notify name. Otherwise throws error # # action: service name=httpd state=started - name: Remove the package "httpd" on Amazon Linux Server action: yum name=httpd state=absent |
Note
:wq! —– to quit.
Run ansible-playbook to call the playbook “kt-handlers.yml”
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
[kt-ansible@ip-172-31-15-116 ~]$ ansible-playbook kt-handlers.yml PLAY [Ktexperts-Group] **************************************************************************************************************************************** TASK [Gathering Facts] **************************************************************************************************************************************** ok: [172.31.11.251] ok: [172.31.2.38] TASK [Remove the package "httpd" on Amazon Linux Server] ****************************************************************************************************** changed: [172.31.11.251] changed: [172.31.2.38] PLAY RECAP **************************************************************************************************************************************************** 172.31.11.251 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 172.31.2.38 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 |
Verity the package “httpd” inside 2 hosts
1 2 3 4 5 6 7 8 |
[kt-ansible@ip-172-31-15-116 ~]$ ansible Ktexperts-Group -b -a "which httpd" 172.31.11.251 | FAILED | rc=1 >> which: no httpd in (/sbin:/bin:/usr/sbin:/usr/bin)non-zero return code 172.31.2.38 | FAILED | rc=1 >> which: no httpd in (/sbin:/bin:/usr/sbin:/usr/bin)non-zero return code |
Dry Run
Check whether the playbook is formatted correctly.
Test how the playbook is going to behave without running the tasks.
Create a playbook “kt-dry.yml”
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
[kt-ansible@ip-172-31-15-116 ~]$ vi kt-dry.yml --- # My Handlers YAML playbook (Target,Tasks and Handlers) - hosts: Ktexperts-Group user: kt-ansible become: yes connection: ssh tasks: - name: Install the package "httpd" on Amazon Linux Server action: yum name=httpd state=installed - name: Create a file "ktexperts" on Amazon Linux Server action: file name=ktexperts state=touch - name: Create a directory "ktexperts-directory" on Amazon Linux Server action: file name=ktexperts-directory state=directory - name: Create a user "Ramesh" on Amazon Linux Server action: user name=Ramesh - name: Create a group "Ktexperts" on Amazon Linux Server action: group name=ktexperts - name: Add user "Ramesh" to group "ktexperts" on Amazon Linux Server action: user name=Ramesh groups=ktexperts |
Note
:wq! —– to quit.
Run ansible-playbook to call the playbook “kt-dry.yml”
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
[kt-ansible@ip-172-31-15-116 ~]$ ansible-playbook kt-dry.yml --check PLAY [Ktexperts-Group] **************************************************************************************************************************************** TASK [Gathering Facts] **************************************************************************************************************************************** ok: [172.31.11.251] ok: [172.31.2.38] TASK [Install the package "httpd" on Amazon Linux Server] ***************************************************************************************************** ok: [172.31.11.251] ok: [172.31.2.38] TASK [Create a file "ktexperts" on Amazon Linux Server] ******************************************************************************************************* changed: [172.31.11.251] changed: [172.31.2.38] TASK [Create a directory "ktexperts-directory" on Amazon Linux Server] **************************************************************************************** changed: [172.31.11.251] changed: [172.31.2.38] TASK [Create a user "Ramesh" on Amazon Linux Server] ********************************************************************************************************** changed: [172.31.11.251] changed: [172.31.2.38] TASK [Create a group "Ktexperts" on Amazon Linux Server] ****************************************************************************************************** changed: [172.31.11.251] changed: [172.31.2.38] TASK [Add user "Ramesh" to group "ktexperts" on Amazon Linux Server] ****************************************************************************************** changed: [172.31.11.251] changed: [172.31.2.38] PLAY RECAP **************************************************************************************************************************************************** 172.31.11.251 : ok=7 changed=5 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 172.31.2.38 : ok=7 changed=5 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 |
Note
It will be used to test the playbook before the run.
Verify files/directories (ktexperts ,ktexperts-directory) inside 2 hosts
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
[kt-ansible@ip-172-31-15-116 ~]$ ansible Ktexperts-Group -b -a "ls" 172.31.11.251 | CHANGED | rc=0 >> chef-workstation-0.15.6-1.el7.x86_64.rpm kt-1 kt-2 kt-3 kt-4 172.31.2.38 | CHANGED | rc=0 >> kt-1 kt-2 kt-3 kt-4 |
Verify user “Ramesh” inside 2 hosts
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
[kt-ansible@ip-172-31-15-116 ~]$ ansible Ktexperts-Group -b -a "tail -5 /etc/passwd" 172.31.11.251 | CHANGED | rc=0 >> chrony:x:997:995::/var/lib/chrony:/sbin/nologin tcpdump:x:72:72::/:/sbin/nologin ec2-user:x:1000:1000:EC2 Default User:/home/ec2-user:/bin/bash kt-ansible:x:1001:1001::/home/kt-ansible:/bin/bash apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin 172.31.2.38 | CHANGED | rc=0 >> chrony:x:997:995::/var/lib/chrony:/sbin/nologin tcpdump:x:72:72::/:/sbin/nologin ec2-user:x:1000:1000:EC2 Default User:/home/ec2-user:/bin/bash kt-ansible:x:1001:1001::/home/kt-ansible:/bin/bash apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin |
Verify group”ktexperts” inside 2 hosts
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
[kt-ansible@ip-172-31-15-116 ~]$ ansible Ktexperts-Group -b -a "tail -5 /etc/group" 172.31.11.251 | CHANGED | rc=0 >> tcpdump:x:72: ec2-user:x:1000: kt-ansible:x:1001: apache:x:48: rammy:x:1002: 172.31.2.38 | CHANGED | rc=0 >> tcpdump:x:72: ec2-user:x:1000: kt-ansible:x:1001: apache:x:48: rammy:x:1002: |
Loops Section
Create files, directories, users and groups by using loops section
Create a playbook “kt-loops.yml”
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
[kt-ansible@ip-172-31-15-116 ~]$ vi kt-loops.yml --- #My Loop Playbook (Target,Tasks with loops) - hosts: Ktexperts-Group user: kt-ansible become: yes connection: ssh tasks: - name: add a list of files file: name='{{ item }}' state=touch with_items: - ktexperts-1 - ktexperts-2 - ktexperts-3 - name: add a list of directories file: name='{{ item }}' state=directory with_items: - ktexperts-dir-1 - ktexperts-dir-2 - ktexperts-dir-3 - name: add a list of users user: name='{{ item }}' state=present with_items: - Vinod - Ajay - Ramesh - name: add a list of groups group: name='{{ item }}' state=present with_items: - Ktexperts-1 - Ktexperts-2 - Ktexperts-3 |
Note
:wq! —– to quit.
Run ansible-playbook to call the playbook “kt-loops.yml”
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
[kt-ansible@ip-172-31-15-116 ~]$ ansible-playbook kt-loops.yml PLAY [Ktexperts-Group] **************************************************************************************************************************************** TASK [Gathering Facts] **************************************************************************************************************************************** ok: [172.31.2.38] ok: [172.31.11.251] TASK [add a list of files] ************************************************************************************************************************************ changed: [172.31.11.251] => (item=ktexperts-1) changed: [172.31.2.38] => (item=ktexperts-1) changed: [172.31.11.251] => (item=ktexperts-2) changed: [172.31.2.38] => (item=ktexperts-2) changed: [172.31.11.251] => (item=ktexperts-3) changed: [172.31.2.38] => (item=ktexperts-3) TASK [add a list of directories] ****************************************************************************************************************************** changed: [172.31.11.251] => (item=ktexperts-dir-1) changed: [172.31.2.38] => (item=ktexperts-dir-1) changed: [172.31.11.251] => (item=ktexperts-dir-2) changed: [172.31.2.38] => (item=ktexperts-dir-2) changed: [172.31.11.251] => (item=ktexperts-dir-3) changed: [172.31.2.38] => (item=ktexperts-dir-3) TASK [add a list of users] ************************************************************************************************************************************ changed: [172.31.2.38] => (item=Vinod) changed: [172.31.11.251] => (item=Vinod) changed: [172.31.2.38] => (item=Ajay) changed: [172.31.11.251] => (item=Ajay) changed: [172.31.2.38] => (item=Ramesh) changed: [172.31.11.251] => (item=Ramesh) TASK [add a list of groups] *********************************************************************************************************************************** changed: [172.31.11.251] => (item=Ktexperts-1) changed: [172.31.2.38] => (item=Ktexperts-1) changed: [172.31.2.38] => (item=Ktexperts-2) changed: [172.31.11.251] => (item=Ktexperts-2) changed: [172.31.2.38] => (item=Ktexperts-3) changed: [172.31.11.251] => (item=Ktexperts-3) PLAY RECAP **************************************************************************************************************************************************** 172.31.11.251 : ok=5 changed=4 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 172.31.2.38 : ok=5 changed=4 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 |
Verify all modules inside the 2 hosts
Verify files/directories inside the 2 hosts
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
[kt-ansible@ip-172-31-15-116 ~]$ ansible Ktexperts-Group -b -a "ls" 172.31.11.251 | CHANGED | rc=0 >> chef-workstation-0.15.6-1.el7.x86_64.rpm kt-1 kt-2 kt-3 kt-4 ktexperts-1 ktexperts-2 ktexperts-3 ktexperts-dir-1 ktexperts-dir-2 ktexperts-dir-3 172.31.2.38 | CHANGED | rc=0 >> kt-1 kt-2 kt-3 kt-4 ktexperts-1 ktexperts-2 ktexperts-3 ktexperts-dir-1 ktexperts-dir-2 ktexperts-dir-3 |
Verify users inside the 2 hosts
1 2 3 4 5 6 7 8 9 10 11 |
[kt-ansible@ip-172-31-15-116 ~]$ ansible Ktexperts-Group -b -a "tail -3 /etc/passwd" 172.31.2.38 | CHANGED | rc=0 >> Vinod:x:1002:1003::/home/Vinod:/bin/bash Ajay:x:1003:1004::/home/Ajay:/bin/bash Ramesh:x:1004:1005::/home/Ramesh:/bin/bash 172.31.11.251 | CHANGED | rc=0 >> Vinod:x:1002:1003::/home/Vinod:/bin/bash Ajay:x:1003:1004::/home/Ajay:/bin/bash Ramesh:x:1004:1005::/home/Ramesh:/bin/bash |
Verify groups inside the 2 hosts
1 2 3 4 5 6 7 8 9 10 11 |
[kt-ansible@ip-172-31-15-116 ~]$ ansible Ktexperts-Group -b -a "tail -3 /etc/group" 172.31.11.251 | CHANGED | rc=0 >> Ktexperts-1:x:1006: Ktexperts-2:x:1007: Ktexperts-3:x:1008: 172.31.2.38 | CHANGED | rc=0 >> Ktexperts-1:x:1006: Ktexperts-2:x:1007: Ktexperts-3:x:1008: |
Conditionals Section
Install the package “httpd” in Redhat family by using conditionals section
Create a playbook “kt-conditionals.yml”
1 2 3 4 5 6 7 8 9 10 11 12 13 |
[kt-ansible@ip-172-31-15-116 ~]$ vi kt-conditionals.yml --- # My Conditionals playbook (Target,Tasks with condition) - hosts: Ktexperts-Group user: kt-ansible become: yes connection: ssh tasks: - name: Install apache for debian command: apt-get -y install apache2 when: ansible_os_family == "Debian" - name: Install apache for redhat command: yum -y install httpd when: ansible_os_family == "RedHat" |
Note
:wq! —– to quit.
Run ansible-playbook to call the playbook “kt-conditionals.yml”
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
[kt-ansible@ip-172-31-15-116 ~]$ ansible-playbook kt-conditionals.yml PLAY [Ktexperts-Group] **************************************************************************************************************************************** TASK [Gathering Facts] **************************************************************************************************************************************** ok: [172.31.2.38] ok: [172.31.11.251] TASK [Install apache for debian] ****************************************************************************************************************************** skipping: [172.31.11.251] skipping: [172.31.2.38] TASK [Install apache for redhat] ****************************************************************************************************************************** changed: [172.31.11.251] changed: [172.31.2.38] PLAY RECAP **************************************************************************************************************************************************** 172.31.11.251 : ok=2 changed=1 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0 172.31.2.38 : ok=2 changed=1 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0 |
Verify the package “httpd” inside 2 nodes
1 2 3 4 5 6 7 8 |
[kt-ansible@ip-172-31-15-116 ~]$ ansible Ktexperts-Group -b -a "which httpd" 172.31.11.251 | CHANGED | rc=0 >> /sbin/httpd 172.31.2.38 | CHANGED | rc=0 >> /sbin/httpd |
See the OS Information of 2 Hosts
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
[kt-ansible@ip-172-31-15-116 ~]$ ansible Ktexperts-Group -b -a "cat /etc/os-release" 172.31.2.38 | CHANGED | rc=0 >> NAME="Amazon Linux" VERSION="2" ID="amzn" ID_LIKE="centos rhel fedora" VERSION_ID="2" PRETTY_NAME="Amazon Linux 2" ANSI_COLOR="0;33" CPE_NAME="cpe:2.3:o:amazon:amazon_linux:2" HOME_URL="https://amazonlinux.com/" 172.31.11.251 | CHANGED | rc=0 >> NAME="Amazon Linux" VERSION="2" ID="amzn" ID_LIKE="centos rhel fedora" VERSION_ID="2" PRETTY_NAME="Amazon Linux 2" ANSI_COLOR="0;33" CPE_NAME="cpe:2.3:o:amazon:amazon_linux:2" HOME_URL="https://amazonlinux.com/" |
Thank you for giving your valuable time to read the above information. Please click here to subscribe for further updates
KTEXPERTS is always active on below social media platforms.
Facebook : https://www.facebook.com/ktexperts/
LinkedIn : https://www.linkedin.com/company/ktexperts/
Twitter : https://twitter.com/ktexpertsadmin
YouTube : https://www.youtube.com/c/ktexperts
Instagram : https://www.instagram.com/knowledgesharingplatform