Dear Readers,
In this article,we will see Push Code From Ansible Server to Hosts by using Single Module.
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.
Steps to Follow
- Connect to Ansible Linux EC2 Terminal through Putty.
- See the full information of 2 hosts (Gathering facts).
- See the hostname of 2 hosts.
- Install package “httpd” inside 2 nodes inside the 2 hosts “Ansible-Node-1” and “Ansible-Node-2”.
- Upgrade package “httpd” inside 2 nodes inside the 2 hosts “Ansible-Node-1” and “Ansible-Node-2”.
- Remove package “httpd” inside 2 nodes inside the 2 hosts “Ansible-Node-1” and “Ansible-Node-2”.
- Create a file “ktexperts” inside the 2 hosts “Ansible-Node-1” and “Ansible-Node-2”.
- Remove a file “ktexperts” inside the 2 hosts “Ansible-Node-1” and “Ansible-Node-2”.
- Create a directory “ktexperts-dir” inside the 2 hosts “Ansible-Node-1” and “Ansible-Node-2”.
- Remove a directory “ktexperts-dir” inside the 2 hosts “Ansible-Node-1” and “Ansible-Node-2”
- Create a user “rammy” inside the 2 hosts “Ansible-Node-1” and “Ansible-Node-2”.
- Create a group “Ktexperts-DevOps” inside the 2 hosts “Ansible-Node-1” and “Ansible-Node-2”.
- Add user “rammy” to group “Ktexperts-DevOps”.
- Remove user “rammy” inside the 2 hosts “Ansible-Node-1” and “Ansible-Node-2”.
- Remove group “Ktexperts-DevOps” inside the 2 hosts “Ansible-Node-1” and “Ansible-Node-2”.
- Download the package from the internet to 2 hosts.
- Copy file “ktexperts” from ansible server to 2 hosts.
- Start the service “httpd” in 2 hosts.
1. Connect to Ansible Linux EC2 Terminal through Putty
1 2 3 4 5 6 7 8 9 10 11 |
Using username "ec2-user". Authenticating with public key "imported-openssh-key" __| __|_ ) _| ( / Amazon Linux 2 AMI ___|\___|___| https://aws.amazon.com/amazon-linux-2/ 5 package(s) needed for security, out of 13 available Run "sudo yum update" to apply all updates. [ec2-user@ip-172-31-6-116~]$ |
Switch to the root user
1 2 |
[ec2-user@ip-172-31-15-116 ~]$ sudo su [root@ip-172-31-15-116 ec2-user]# |
Switch to user “kt-ansible”
1 2 |
[root@ip-172-31-15-116 ec2-user]# su - kt-ansible [kt-ansible@ip-172-31-15-116 ~]$ |
Setup Module
To gets information about the network or hardware or OS version or memory-related information the setup module will help to gather the same about the target machines.
2. See the full information of 2 hosts (Gathering facts)
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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 |
[kt-ansible@ip-172-31-15-116 ~]$ ansible Ktexperts-Group -m setup 172.31.11.251 | SUCCESS => { "ansible_facts": { "ansible_all_ipv4_addresses": [ "172.31.11.251" ], "ansible_all_ipv6_addresses": [ "fe80::8ed:27ff:feaf:a02" ], "ansible_apparmor": { "status": "disabled" }, "ansible_architecture": "x86_64", "ansible_bios_date": "08/24/2006", "ansible_bios_version": "4.2.amazon", "ansible_cmdline": { "BOOT_IMAGE": "/boot/vmlinuz-4.14.158-129.185.amzn2.x86_64", "KEYTABLE": "us", "LANG": "en_US.UTF-8", "biosdevname": "0", "console": "ttyS0,115200n8", "net.ifnames": "0", "nvme_core.io_timeout": "4294967295", "rd.emergency": "poweroff", "rd.shell": "0", "ro": true, "root": "UUID=afcf1342-1d40-41bd-bde9-e4ea5d87e3b6" }, "ansible_date_time": { "date": "2020-02-05", "day": "05", "epoch": "1580900591", "hour": "11", "iso8601": "2020-02-05T11:03:11Z", "iso8601_basic": "20200205T110311615912", "iso8601_basic_short": "20200205T110311", "iso8601_micro": "2020-02-05T11:03:11.615991Z", "minute": "03", "month": "02", "second": "11", "time": "11:03:11", "tz": "UTC", "tz_offset": "+0000", "weekday": "Wednesday", "weekday_number": "3", "weeknumber": "05", "year": "2020" }, "ansible_default_ipv4": { "address": "172.31.11.251", "alias": "eth0", "broadcast": "172.31.15.255", "gateway": "172.31.0.1", "interface": "eth0", "macaddress": "0a:ed:27:af:0a:02", "mtu": 9001, "netmask": "255.255.240.0", "network": "172.31.0.0", "type": "ether" }, "ansible_domain": "ap-south-1.compute.internal", "ansible_effective_group_id": 1001, "ansible_effective_user_id": 1001, "ansible_env": { "HOME": "/home/kt-ansible", "LANG": "en_US.UTF-8", "LESSOPEN": "||/usr/bin/lesspipe.sh %s", "LOGNAME": "kt-ansible", "PATH": "/usr/local/bin:/usr/bin", "PWD": "/home/kt-ansible", "SHELL": "/bin/bash", "SHLVL": "2", "SSH_CLIENT": "172.31.15.116 35246 22", "SSH_CONNECTION": "172.31.15.116 35246 172.31.11.251 22", "SSH_TTY": "/dev/pts/0", "TERM": "xterm", "USER": "kt-ansible", "XDG_RUNTIME_DIR": "/run/user/1001", "XDG_SESSION_ID": "1", "_": "/usr/bin/python" }, "ipv4": { "address": "172.31.11.251", "broadcast": "172.31.15.255", "netmask": "255.255.240.0", "network": "172.31.0.0" }, "ipv6": [ { "address": "fe80::8ed:27ff:feaf:a02", "prefix": "64", "scope": "link" } ], "macaddress": "0a:ed:27:af:0a:02", "module": "xen_netfront", "mtu": 9001, "pciid": "vif-0", "promisc": false, "timestamping": [ "rx_software", "software" ], "type": "ether" }, "ansible_fibre_channel_wwn": [], "ansible_fips": false, "ansible_form_factor": "Other", "ansible_fqdn": "ip-172-31-11-251.ap-south-1.compute.internal", "ansible_hostname": "ip-172-31-11-251", "ansible_hostnqn": "", "ansible_interfaces": [ "lo", }, "hw_timestamp_filters": [], "ipv4": { "address": "127.0.0.1", "broadcast": "host", "netmask": "255.0.0.0", "network": "127.0.0.0" }, "ipv6": [ { "address": "::1", "prefix": "128", "scope": "host" } ], "module_setup": true }, "changed": false 172.31.2.38 | SUCCESS => { "ansible_facts": { "ansible_all_ipv4_addresses": [ "172.31.2.38" ], }, "ansible_date_time": { "date": "2020-02-05", "day": "05", "epoch": "1580900591", "hour": "11", "iso8601": "2020-02-05T11:03:11Z", "iso8601_basic": "20200205T110311618466", "iso8601_basic_short": "20200205T110311", "iso8601_micro": "2020-02-05T11:03:11.618545Z", "minute": "03", "month": "02", "second": "11", "time": "11:03:11", "tz": "UTC", "tz_offset": "+0000", "weekday": "Wednesday", "weekday_number": "3", "weeknumber": "05", "year": "2020" }, "ansible_default_ipv4": { "address": "172.31.2.38", "alias": "eth0", "broadcast": "172.31.15.255", "gateway": "172.31.0.1", "interface": "eth0", "macaddress": "0a:5c:82:4c:40:3e", "mtu": 9001, "netmask": "255.255.240.0", "network": "172.31.0.0", "type": "ether" }, "ansible_default_ipv6": {}, "ansible_device_links": { "ids": {}, "labels": { "xvda1": [ "\\x2f" ] }, "masters": {}, "uuids": { "xvda1": [ "afcf1342-1d40-41bd-bde9-e4ea5d87e3b6" ] } }, "ansible_devices": { "xvda": { "holders": [], "host": "", "links": { "ids": [], "labels": [], "masters": [], "uuids": [] }, "model": null, "partitions": { "xvda1": { "holders": [], "links": { "ids": [], "labels": [ "\\x2f" ], "masters": [], "uuids": [ "afcf1342-1d40-41bd-bde9-e4ea5d87e3b6" ] }, "sectors": "16773087", "sectorsize": 512, "size": "8.00 GB", "start": "4096", "uuid": "afcf1342-1d40-41bd-bde9-e4ea5d87e3b6" } }, "removable": "0", "rotational": "0", "sas_address": null, "sas_device_handle": null, "scheduler_mode": "noop", "sectors": "16777216", "sectorsize": "512", "size": "8.00 GB", "support_discard": "0", "vendor": null, "virtual": 1 } }, "ipv4": { "address": "127.0.0.1", "broadcast": "host", "netmask": "255.0.0.0", "network": "127.0.0.0" }, "ipv6": [ { "address": "::1", "prefix": "128", "scope": "host" } ], "mtu": 65536, "promisc": false, "timestamping": [ "tx_software", "rx_software", "software" ], "type": "loopback" }, "ansible_local": {}, "ansible_lsb": {}, "ansible_machine": "x86_64", "ansible_machine_id": "ec2eb2a972ed3b680c44e709588d1e20", "ansible_memfree_mb": 750, "ansible_memory_mb": { "nocache": { "free": 896, "used": 87 }, "real": { "free": 750, "total": 983, "used": 233 }, "swap": { "cached": 0, "free": 0, "total": 0, "used": 0 } "module_setup": true }, "changed": false |
Command Module
3. See the hostname of 2 hosts
The command module simply executes a specific command on the target machine and gives the output.
1 2 3 4 5 6 7 8 |
[kt-ansible@ip-172-31-15-116 ~]$ ansible Ktexperts-Group -m command -a 'hostname' 172.31.2.38 | CHANGED | rc=0 >> ip-172-31-2-38.ap-south-1.compute.internal 172.31.11.251 | CHANGED | rc=0 >> ip-172-31-11-251.ap-south-1.compute.internal |
yum Module
4. Install package “httpd” inside 2 nodes inside the 2 hosts “Ansible-Node-1” and “Ansible-Node-2”
We can push code from Ansible Server to hosts by using a single module.
Here,
-a ——–> To run arbitrary.
-m ——-> To run the module.
-b ———> To become root.
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 -m yum -a "pkg=httpd state=present" 172.31.11.251 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "changes": { "installed": [ "httpd" ] }, "msg": "", "rc": 0, "results": [ "Loaded plugins: extras_suggestions, langpacks, priorities, update-motd\nResolving Dependencies\n--> Running transaction check\n---> Package httpd.x86_64 0:2.4.41-1.amzn2.0.1 will be installed\n--> Processing Dependency: mod_http2 for package: httpd-2.4.41-1.amzn2.0.1.x86_64\n--> Running transaction check\n---> Package mod_http2.x86_64 0:1.15.3-2.amzn2 will be installed\n--> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package Arch Version Repository Size\n================================================================================\nInstalling:\n httpd x86_64 2.4.41-1.amzn2.0.1 amzn2-core 1.3 M\nInstalling for dependencies:\n mod_http2 x86_64 1.15.3-2.amzn2 amzn2-core 146 k\n\nTransaction Summary\n================================================================================\nInstall 1 Package (+1 Dependent package)\n\nTotal download size: 1.5 M\nInstalled size: 4.3 M\nDownloading packages:\n--------------------------------------------------------------------------------\nTotal 9.1 MB/s | 1.5 MB 00:00 \nRunning transaction check\nRunning transaction test\nTransaction test succeeded\nRunning transaction\n Installing : httpd-2.4.41-1.amzn2.0.1.x86_64 1/2 \n Installing : mod_http2-1.15.3-2.amzn2.x86_64 2/2 \n Verifying : mod_http2-1.15.3-2.amzn2.x86_64 1/2 \n Verifying : httpd-2.4.41-1.amzn2.0.1.x86_64 2/2 \n\nInstalled:\n httpd.x86_64 0:2.4.41-1.amzn2.0.1 \n\nDependency Installed:\n mod_http2.x86_64 0:1.15.3-2.amzn2 \n\nComplete!\n" ] } [WARNING]: Platform linux on host 172.31.2.38 is using the discovered Python interpreter at /usr/bin/python, but future installation of another Python interpreter could change this. See https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information. 172.31.2.38 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "changes": { "installed": [ "httpd" ] }, "msg": "", "rc": 0, "results": [ "Loaded plugins: extras_suggestions, langpacks, priorities, update-motd\nResolving Dependencies\n--> Running transaction check\n---> Package httpd.x86_64 0:2.4.41-1.amzn2.0.1 will be installed\n--> Processing Dependency: mod_http2 for package: httpd-2.4.41-1.amzn2.0.1.x86_64\n--> Running transaction check\n---> Package mod_http2.x86_64 0:1.15.3-2.amzn2 will be installed\n--> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package Arch Version Repository Size\n================================================================================\nInstalling:\n httpd x86_64 2.4.41-1.amzn2.0.1 amzn2-core 1.3 M\nInstalling for dependencies:\n mod_http2 x86_64 1.15.3-2.amzn2 amzn2-core 146 k\n\nTransaction Summary\n================================================================================\nInstall 1 Package (+1 Dependent package)\n\nTotal download size: 1.5 M\nInstalled size: 4.3 M\nDownloading packages:\n--------------------------------------------------------------------------------\nTotal 9.7 MB/s | 1.5 MB 00:00 \nRunning transaction check\nRunning transaction test\nTransaction test succeeded\nRunning transaction\n Installing : httpd-2.4.41-1.amzn2.0.1.x86_64 1/2 \n Installing : mod_http2-1.15.3-2.amzn2.x86_64 2/2 \n Verifying : mod_http2-1.15.3-2.amzn2.x86_64 1/2 \n Verifying : httpd-2.4.41-1.amzn2.0.1.x86_64 2/2 \n\nInstalled:\n httpd.x86_64 0:2.4.41-1.amzn2.0.1 \n\nDependency Installed:\n mod_http2.x86_64 0:1.15.3-2.amzn2 \n\nComplete!\n" ] } |
Verify the package “httpd” inside the 2 Nodes “Ansible-Node-1” and “Ansible-Node-2”
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 |
5. Upgrade package “httpd” inside 2 hosts inside the 2 Nodes “Ansible-Node-1” and “Ansible-Node-2”
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 -m yum -a "pkg=httpd state=latest" 172.31.2.38 | SUCCESS => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": false, "changes": { "installed": [], "updated": [] }, "msg": "", "rc": 0, "results": [ "All packages providing httpd are up to date", "" ] } 172.31.11.251 | SUCCESS => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": false, "changes": { "installed": [], "updated": [] }, "msg": "", "rc": 0, "results": [ "All packages providing httpd are up to date", "" ] } |
Verify the version of the package “httpd” inside the 2 Nodes “Ansible-Node-1” and “Ansible-Node-2”
1 2 3 4 5 6 7 8 9 |
kt-ansible@ip-172-31-15-116 ~]$ ansible Ktexperts-Group -b -a "httpd -v" 172.31.11.251 | CHANGED | rc=0 >> Server version: Apache/2.4.41 () Server built: Oct 22 2019 22:59:04 172.31.2.38 | CHANGED | rc=0 >> Server version: Apache/2.4.41 () Server built: Oct 22 2019 22:59:04 |
6. Remove package “httpd” inside 2 hosts inside the 2 Nodes “Ansible-Node-1” and “Ansible-Node-2”
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 -m yum -a "pkg=httpd state=absent" 172.31.11.251 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "changes": { "removed": [ "httpd" ] }, "msg": "", "rc": 0, "results": [ "Loaded plugins: extras_suggestions, langpacks, priorities, update-motd\nResolving Dependencies\n--> Running transaction check\n---> Package httpd.x86_64 0:2.4.41-1.amzn2.0.1 will be erased\n--> Processing Dependency: httpd-mmn = 20120211x8664 for package: mod_http2-1.15.3-2.amzn2.x86_64\n--> Running transaction check\n---> Package mod_http2.x86_64 0:1.15.3-2.amzn2 will be erased\n--> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package Arch Version Repository Size\n================================================================================\nRemoving:\n httpd x86_64 2.4.41-1.amzn2.0.1 @amzn2-core 4.0 M\nRemoving for dependencies:\n mod_http2 x86_64 1.15.3-2.amzn2 @amzn2-core 378 k\n\nTransaction Summary\n================================================================================\nRemove 1 Package (+1 Dependent package)\n\nInstalled size: 4.3 M\nDownloading packages:\nRunning transaction check\nRunning transaction test\nTransaction test succeeded\nRunning transaction\n Erasing : httpd-2.4.41-1.amzn2.0.1.x86_64 1/2 \n Erasing : mod_http2-1.15.3-2.amzn2.x86_64 2/2 \n Verifying : mod_http2-1.15.3-2.amzn2.x86_64 1/2 \n Verifying : httpd-2.4.41-1.amzn2.0.1.x86_64 2/2 \n\nRemoved:\n httpd.x86_64 0:2.4.41-1.amzn2.0.1 \n\nDependency Removed:\n mod_http2.x86_64 0:1.15.3-2.amzn2 \n\nComplete!\n" ] } 172.31.2.38 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "changes": { "removed": [ "httpd" ] }, "msg": "", "rc": 0, "results": [ "Loaded plugins: extras_suggestions, langpacks, priorities, update-motd\nResolving Dependencies\n--> Running transaction check\n---> Package httpd.x86_64 0:2.4.41-1.amzn2.0.1 will be erased\n--> Processing Dependency: httpd-mmn = 20120211x8664 for package: mod_http2-1.15.3-2.amzn2.x86_64\n--> Running transaction check\n---> Package mod_http2.x86_64 0:1.15.3-2.amzn2 will be erased\n--> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package Arch Version Repository Size\n================================================================================\nRemoving:\n httpd x86_64 2.4.41-1.amzn2.0.1 @amzn2-core 4.0 M\nRemoving for dependencies:\n mod_http2 x86_64 1.15.3-2.amzn2 @amzn2-core 378 k\n\nTransaction Summary\n================================================================================\nRemove 1 Package (+1 Dependent package)\n\nInstalled size: 4.3 M\nDownloading packages:\nRunning transaction check\nRunning transaction test\nTransaction test succeeded\nRunning transaction\n Erasing : httpd-2.4.41-1.amzn2.0.1.x86_64 1/2 \n Erasing : mod_http2-1.15.3-2.amzn2.x86_64 2/2 \n Verifying : mod_http2-1.15.3-2.amzn2.x86_64 1/2 \n Verifying : httpd-2.4.41-1.amzn2.0.1.x86_64 2/2 \n\nRemoved:\n httpd.x86_64 0:2.4.41-1.amzn2.0.1 \n\nDependency Removed:\n mod_http2.x86_64 0:1.15.3-2.amzn2 \n\nComplete!\n" ] } |
Verify the package “httpd” inside the 2 hosts “Ansible-Node-1” and “Ansible-Node-2”
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 |
File Module
This module is used to create and remove files, directories.
7. Create a file “ktexperts” inside the 2 hosts “Ansible-Node-1” and “Ansible-Node-2”
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 ~]$ ansible Ktexperts-Group -b -m file -a "name=ktexperts state=touch" 172.31.11.251 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "dest": "ktexperts", "gid": 0, "group": "root", "mode": "0644", "owner": "root", "size": 0, "state": "file", "uid": 0 } 172.31.2.38 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "dest": "ktexperts", "gid": 0, "group": "root", "mode": "0644", "owner": "root", "size": 0, "state": "file", "uid": 0 } |
Verify file “ktexperts” inside the 2 hosts “Ansible-Node-1” and “Ansible-Node-2”
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 "ls" 172.31.2.38 | CHANGED | rc=0 >> kt-1 kt-2 kt-3 kt-4 ktexperts 172.31.11.251 | CHANGED | rc=0 >> kt-1 kt-2 kt-3 kt-4 ktexperts |
8. Remove a file “ktexperts” inside the 2 hosts “Ansible-Node-1” and “Ansible-Node-2”
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
kt-ansible@ip-172-31-15-116 ~]$ ansible Ktexperts-Group -b -m file -a "name=ktexperts state=absent" 172.31.11.251 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "path": "ktexperts", "state": "absent" } 172.31.2.38 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "path": "ktexperts", "state": "absent" } |
Verify file “ktexperts” inside the 2 Nodes “Ansible-Node-1” and “Ansible-Node-2”
1 2 3 4 5 6 7 8 9 10 11 12 13 |
kt-ansible@ip-172-31-15-116 ~]$ ansible Ktexperts-Group -b -a "ls" 172.31.2.38 | CHANGED | rc=0 >> kt-1 kt-2 kt-3 kt-4 172.31.11.251 | CHANGED | rc=0 >> kt-1 kt-2 kt-3 kt-4 |
9. Create a directory “ktexperts-dir” inside the 2 hosts “Ansible-Node-1” and “Ansible-Node-2”
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 ~]$ ansible Ktexperts-Group -b -m file -a "name=ktexperts-dir state=directory" 172.31.2.38 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "gid": 0, "group": "root", "mode": "0755", "owner": "root", "path": "ktexperts-dir", "size": 6, "state": "directory", "uid": 0 } 172.31.11.251 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "gid": 0, "group": "root", "mode": "0755", "owner": "root", "path": "ktexperts-dir", "size": 6, "state": "directory", "uid": 0 } |
Verify a directory “ktexperts-dir” inside the 2 hosts “Ansible-Node-1” and “Ansible-Node-2”
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 "ls" 172.31.11.251 | CHANGED | rc=0 >> kt-1 kt-2 kt-3 kt-4 ktexperts-dir 172.31.2.38 | CHANGED | rc=0 >> kt-1 kt-2 kt-3 kt-4 ktexperts-dir |
10. Remove a directory “ktexperts-dir” inside the 2 hosts “Ansible-Node-1” and “Ansible-Node-2”
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 Ktexperts-Group -b -m file -a "name=ktexperts-dir state=absent" 172.31.11.251 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "path": "ktexperts-dir", "state": "absent" } 172.31.2.38 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "path": "ktexperts-dir", "state": "absent" } |
Verify a directory “ktexperts-dir” inside the 2 hosts “Ansible-Node-1” and “Ansible-Node-2”
1 2 3 4 5 6 7 8 9 10 11 12 13 |
kt-ansible@ip-172-31-15-116 ~]$ ansible Ktexperts-Group -a "ls" 172.31.2.38 | CHANGED | rc=0 >> kt-1 kt-2 kt-3 kt-4 172.31.11.251 | CHANGED | rc=0 >> kt-1 kt-2 kt-3 kt-4 |
User Module
Using this module one can create or delete users.
11. Create a user “rammy” inside the 2 hosts “Ansible-Node-1” and “Ansible-Node-2”
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 |
kt-ansible@ip-172-31-15-116 ~]$ ansible Ktexperts-Group -b -m user -a "name=rammy state=present" 172.31.2.38 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "comment": "", "create_home": true, "group": 1002, "home": "/home/rammy", "name": "rammy", "shell": "/bin/bash", "state": "present", "system": false, "uid": 1002 } 172.31.11.251 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "comment": "", "create_home": true, "group": 1002, "home": "/home/rammy", "name": "rammy", "shell": "/bin/bash", "state": "present", "system": false, "uid": 1002 } |
Verify a user “rammy” inside the 2 hosts “Ansible-Node-1” and “Ansible-Node-2”
1 2 3 4 5 6 7 |
kt-ansible@ip-172-31-15-116 ~]$ ansible Ktexperts-Group -b -a "tail -1 /etc/passwd" 172.31.2.38 | CHANGED | rc=0 >> rammy:x:1002:1002::/home/rammy:/bin/bash 172.31.11.251 | CHANGED | rc=0 >> rammy:x:1002:1002::/home/rammy:/bin/bash |
Group Module
Using this module one can create or delete groups.
12. Create a group “Ktexperts-DevOps” inside the 2 hosts “Ansible-Node-1” and “Ansible-Node-2”
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 -m group -a "name=ktexperts-DevOps state=present" 172.31.11.251 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "gid": 1003, "name": "ktexperts-DevOps", "state": "present", "system": false } 172.31.2.38 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "gid": 1003, "name": "ktexperts-DevOps", "state": "present", "system": false } |
Verify a group “Ktexperts-DevOps” inside the 2 hosts “Ansible-Node-1” and “Ansible-Node-2”
1 2 3 4 5 6 7 |
kt-ansible@ip-172-31-15-116 ~]$ ansible Ktexperts-Group -b -a "tail -1 /etc/group" 172.31.11.251 | CHANGED | rc=0 >> ktexperts-DevOps:x:1003: 172.31.2.38 | CHANGED | rc=0 >> ktexperts-DevOps:x:1003: |
13. Add user “rammy” to group “Ktexperts-DevOps”
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 -m user -a "name=rammy groups=ktexperts-DevOps" 172.31.2.38 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "append": false, "changed": true, "comment": "", "group": 1003, "groups": "ktexperts-DevOps", "home": "/home/rammy", "move_home": false, "name": "rammy", "shell": "/bin/bash", "state": "present", "uid": 1002 } 172.31.11.251 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "append": false, "changed": true, "comment": "", "group": 1003, "groups": "ktexperts-DevOps", "home": "/home/rammy", "move_home": false, "name": "rammy", "shell": "/bin/bash", "state": "present", "uid": 1002 } |
Verify user “rammy” inside 2 hosts Ansible-Node-1″ and “Ansible-Node-2”
1 2 3 4 5 6 7 8 |
kt-ansible@ip-172-31-15-116 ~]$ ansible Ktexperts-Group -b -a "tail -1 /etc/group" 172.31.2.38 | CHANGED | rc=0 >> ktexperts-DevOps:x:1003:rammy 172.31.11.251 | CHANGED | rc=0 >> ktexperts-DevOps:x:1003:rammy |
14. Remove user “rammy” inside the 2 hosts “Ansible-Node-1” and “Ansible-Node-2”
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 |
kt-ansible@ip-172-31-15-116 ~]$ ansible Ktexperts-Group -b -m user -a "name=rammy state=absent" 172.31.11.251 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "force": false, "name": "rammy", "remove": false, "state": "absent", "stderr": "userdel: group rammy not removed because it is not the primary group of user rammy.\n", "stderr_lines": [ "userdel: group rammy not removed because it is not the primary group of user rammy." ] } 172.31.2.38 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "force": false, "name": "rammy", "remove": false, "state": "absent", "stderr": "userdel: group rammy not removed because it is not the primary group of user rammy.\n", "stderr_lines": [ "userdel: group rammy not removed because it is not the primary group of user rammy." ] } |
Verify a user “rammy” inside the 2 hosts “Ansible-Node-1” and “Ansible-Node-2”
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 |
15. Remove group “Ktexperts-DevOps” inside the 2 hosts “Ansible-Node-1” and “Ansible-Node-2”
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
[kt-ansible@ip-172-31-15-116 ~]$ ansible Ktexperts-Group -b -m group -a "name=ktexperts-DevOps state=absent" 172.31.11.251 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "name": "ktexperts-DevOps", "state": "absent" } 172.31.2.38 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "name": "ktexperts-DevOps", "state": "absent" } |
Verify group “Ktexperts-DevOps” inside the 2 hosts “Ansible-Node-1” and “Ansible-Node-2”
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.2.38 | CHANGED | rc=0 >> tcpdump:x:72: ec2-user:x:1000: kt-ansible:x:1001: apache:x:48: rammy:x:1002: 172.31.11.251 | CHANGED | rc=0 >> tcpdump:x:72: ec2-user:x:1000: kt-ansible:x:1001: apache:x:48: rammy:x:1002: |
get_url Module
By using the get_url module, we can download packages from the internet.
16. Download the package from the internet to 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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
[kt-ansible@ip-172-31-15-116 ~]$ ansible Ktexperts-Group -b -m get_url -a "url=https://packages.chef.io/files/stable/chef-workstation/0.15.6/el/7/chef-workstation-0.15.6-1.el7.x86_64.rpm dest=/home/kt-ansible" 172.31.2.38 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "checksum_dest": null, "checksum_src": "e64ec414511ef361cdc78e0b3ac6b1b6c4dc8692", "dest": "/home/kt-ansible/chef-workstation-0.15.6-1.el7.x86_64.rpm", "elapsed": 6, "gid": 0, "group": "root", "md5sum": "50996464732303fd53d401861cb92695", "mode": "0644", "msg": "OK (152854907 bytes)", "owner": "root", "size": 152854907, "src": "/home/kt-ansible/.ansible/tmp/ansible-tmp-1580199365.98-240377510091981/tmpUHbB7o", "state": "file", "status_code": 200, "uid": 0, "url": "https://packages.chef.io/files/stable/chef-workstation/0.15.6/el/7/chef-workstation-0.15.6-1.el7.x86_64.rpm" } 172.31.11.251 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "checksum_dest": null, "checksum_src": "e64ec414511ef361cdc78e0b3ac6b1b6c4dc8692", "dest": "/home/kt-ansible/chef-workstation-0.15.6-1.el7.x86_64.rpm", "elapsed": 7, "gid": 0, "group": "root", "md5sum": "50996464732303fd53d401861cb92695", "mode": "0644", "msg": "OK (152854907 bytes)", "owner": "root", "size": 152854907, "src": "/home/kt-ansible/.ansible/tmp/ansible-tmp-1580199365.97-105379482309776/tmpwYDcSN", "state": "file", "status_code": 200, "uid": 0, "url": "https://packages.chef.io/files/stable/chef-workstation/0.15.6/el/7/chef-workstation-0.15.6-1.el7.x86_64.rpm" } |
Verify the package inside the 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 "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 >> chef-workstation-0.15.6-1.el7.x86_64.rpm kt-1 kt-2 kt-3 kt-4 |
Copy Module
It is used for copying files to multiple hosts.
17. Copy file “ktexperts” from ansible server to 2 hosts
Create a file “ktexperts” in ansible server
1 2 3 4 |
[kt-ansible@ip-172-31-15-116 ~]$ touch ktexperts [kt-ansible@ip-172-31-15-116 ~]$ ls kt-conditionals.yml ktexperts kt-loops.yml kt-tasks.yml kt-vault.yml kt-dry.yml kt-handlers.yml kt-target.yml kt-variables.yml playbook |
using below command to copy a file from ansible server to 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 27 28 29 30 31 32 33 34 35 36 37 |
[kt-ansible@ip-172-31-15-116 ~]$ ansible Ktexperts-Group -m copy -a "src=ktexperts dest=/home/kt-ansible/ktexperts" 172.31.2.38 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709", "dest": "/home/kt-ansible/ktexperts", "gid": 1001, "group": "kt-ansible", "md5sum": "d41d8cd98f00b204e9800998ecf8427e", "mode": "0664", "owner": "kt-ansible", "size": 0, "src": "/home/kt-ansible/.ansible/tmp/ansible-tmp-1580903530.68-168651447825940/source", "state": "file", "uid": 1001 } 172.31.11.251 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709", "dest": "/home/kt-ansible/ktexperts", "gid": 1001, "group": "kt-ansible", "md5sum": "d41d8cd98f00b204e9800998ecf8427e", "mode": "0664", "owner": "kt-ansible", "size": 0, "src": "/home/kt-ansible/.ansible/tmp/ansible-tmp-1580903530.68-114472742435264/source", "state": "file", "uid": 1001 } |
Verify file “ktexperts” in 2 hosts
1 2 3 4 5 6 7 |
[kt-ansible@ip-172-31-15-116 ~]$ ansible Ktexperts-Group -a "ls ktexperts" 172.31.11.251 | CHANGED | rc=0 >> ktexperts 172.31.2.38 | CHANGED | rc=0 >> ktexperts |
Service Module
By using this module, we can start and stop the services.
18. Start the service “httpd” in 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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 |
[kt-ansible@ip-172-31-15-116 ~]$ ansible Ktexperts-Group -b -m service -a "name=httpd state=started" 172.31.11.251 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "name": "httpd", "state": "started", "status": { "ActiveEnterTimestampMonotonic": "0", "ActiveExitTimestampMonotonic": "0", "ActiveState": "inactive", "After": "network.target remote-fs.target basic.target -.mount nss-lookup.target systemd-journald.socket system.slice httpd-init.service tmp.mount", "AllowIsolate": "no", "AmbientCapabilities": "0", "AssertResult": "no", "AssertTimestampMonotonic": "0", "Before": "shutdown.target", "BlockIOAccounting": "no", "BlockIOWeight": "18446744073709551615", "CPUAccounting": "no", "CPUQuotaPerSecUSec": "infinity", "CPUSchedulingPolicy": "0", "CPUSchedulingPriority": "0", "CPUSchedulingResetOnFork": "no", "CPUShares": "18446744073709551615", "CanIsolate": "no", "CanReload": "yes", "CanStart": "yes", "CanStop": "yes", "CapabilityBoundingSet": "18446744073709551615", "ConditionResult": "no", "ConditionTimestampMonotonic": "0", "Conflicts": "shutdown.target", "ControlPID": "0", "DefaultDependencies": "yes", "Delegate": "no", "Description": "The Apache HTTP Server", "DevicePolicy": "auto", "Documentation": "man:httpd.service(8)", "Environment": "LANG=C", "ExecMainCode": "0", "ExecMainExitTimestampMonotonic": "0", "ExecMainPID": "0", "ExecMainStartTimestampMonotonic": "0", "ExecMainStatus": "0", "ExecReload": "{ path=/usr/sbin/httpd ; argv[]=/usr/sbin/httpd $OPTIONS -k graceful ; ignore_errors=no ; start_time=[n/a] ; stop_time=[n/a] ; pid=0 ; code=(null) ; status=0/0 }", "ExecStart": "{ path=/usr/sbin/httpd ; argv[]=/usr/sbin/httpd $OPTIONS -DFOREGROUND ; ignore_errors=no ; start_time=[n/a] ; stop_time=[n/a] ; pid=0 ; code=(null) ; status=0/0 }", "FailureAction": "none", "FileDescriptorStoreMax": "0", "FragmentPath": "/usr/lib/systemd/system/httpd.service", "GuessMainPID": "yes", "IOScheduling": "0", "Id": "httpd.service", "IgnoreOnIsolate": "no", "IgnoreOnSnapshot": "no", "IgnoreSIGPIPE": "yes", "InactiveEnterTimestampMonotonic": "0", "InactiveExitTimestampMonotonic": "0", "JobTimeoutAction": "none", "JobTimeoutUSec": "0", "KillMode": "mixed", "KillSignal": "28", "LimitAS": "18446744073709551615", "LimitCORE": "18446744073709551615", "LimitCPU": "18446744073709551615", "LimitDATA": "18446744073709551615", "LimitFSIZE": "18446744073709551615", "LimitLOCKS": "18446744073709551615", "LimitMEMLOCK": "65536", "LimitMSGQUEUE": "819200", "LimitNICE": "0", "LimitNOFILE": "4096", "LimitNPROC": "3792", "LimitRSS": "18446744073709551615", "LimitRTPRIO": "0", "LimitRTTIME": "18446744073709551615", "LimitSIGPENDING": "3792", "LimitSTACK": "18446744073709551615", "LoadState": "loaded", "MainPID": "0", "MemoryAccounting": "no", "MemoryCurrent": "18446744073709551615", "MemoryLimit": "18446744073709551615", "MountFlags": "0", "Names": "httpd.service", "NeedDaemonReload": "no", "Nice": "0", "NoNewPrivileges": "no", "NonBlocking": "no", "NotifyAccess": "main", "OOMScoreAdjust": "0", "OnFailureJobMode": "replace", "PermissionsStartOnly": "no", "PrivateDevices": "no", "PrivateNetwork": "no", "PrivateTmp": "yes", "ProtectHome": "no", "ProtectSystem": "no", "RefuseManualStart": "no", "RefuseManualStop": "no", "RemainAfterExit": "no", "Requires": "basic.target -.mount", "RequiresMountsFor": "/var/tmp", "Restart": "no", "RestartUSec": "100ms", "Result": "success", "RootDirectoryStartOnly": "no", "RuntimeDirectoryMode": "0755", "SameProcessGroup": "no", "SecureBits": "0", "SendSIGHUP": "no", "SendSIGKILL": "yes", "Slice": "system.slice", "StandardError": "inherit", "StandardInput": "null", "StandardOutput": "journal", "StartLimitAction": "none", "StartLimitBurst": "5", "StartLimitInterval": "10000000", "StartupBlockIOWeight": "18446744073709551615", "StartupCPUShares": "18446744073709551615", "StatusErrno": "0", "StopWhenUnneeded": "no", "SubState": "dead", "SyslogLevelPrefix": "yes", "SyslogPriority": "30", "SystemCallErrorNumber": "0", "TTYReset": "no", "TTYVHangup": "no", "TTYVTDisallocate": "no", "TasksAccounting": "no", "TasksCurrent": "18446744073709551615", "TasksMax": "18446744073709551615", "TimeoutStartUSec": "1min 30s", "TimeoutStopUSec": "1min 30s", "TimerSlackNSec": "50000", "Transient": "no", "Type": "notify", "UMask": "0022", "UnitFilePreset": "disabled", "UnitFileState": "disabled", "Wants": "system.slice httpd-init.service", "WatchdogTimestampMonotonic": "0", "WatchdogUSec": "0" } } 172.31.2.38 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "name": "httpd", "state": "started", "status": { "ActiveEnterTimestampMonotonic": "0", "ActiveExitTimestampMonotonic": "0", "ActiveState": "inactive", "After": "nss-lookup.target httpd-init.service systemd-journald.socket basic.target remote-fs.target -.mount system.slice network.target tmp.mount", "AllowIsolate": "no", "AmbientCapabilities": "0", "AssertResult": "no", "AssertTimestampMonotonic": "0", "Before": "shutdown.target", "BlockIOAccounting": "no", "BlockIOWeight": "18446744073709551615", "CPUAccounting": "no", "CPUQuotaPerSecUSec": "infinity", "CPUSchedulingPolicy": "0", "CPUSchedulingPriority": "0", "CPUSchedulingResetOnFork": "no", "CPUShares": "18446744073709551615", "CanIsolate": "no", "CanReload": "yes", "CanStart": "yes", "CanStop": "yes", "CapabilityBoundingSet": "18446744073709551615", "ConditionResult": "no", "ConditionTimestampMonotonic": "0", "Conflicts": "shutdown.target", "ControlPID": "0", "DefaultDependencies": "yes", "Delegate": "no", "Description": "The Apache HTTP Server", "DevicePolicy": "auto", "Documentation": "man:httpd.service(8)", "Environment": "LANG=C", "ExecMainCode": "0", "ExecMainExitTimestampMonotonic": "0", "ExecMainPID": "0", "ExecMainStartTimestampMonotonic": "0", "ExecMainStatus": "0", "ExecReload": "{ path=/usr/sbin/httpd ; argv[]=/usr/sbin/httpd $OPTIONS -k graceful ; ignore_errors=no ; start_time=[n/a] ; stop_time=[n/a] ; pid=0 ; code=(null) ; status=0/0 }", "ExecStart": "{ path=/usr/sbin/httpd ; argv[]=/usr/sbin/httpd $OPTIONS -DFOREGROUND ; ignore_errors=no ; start_time=[n/a] ; stop_time=[n/a] ; pid=0 ; code=(null) ; status=0/0 }", "FailureAction": "none", "FileDescriptorStoreMax": "0", "FragmentPath": "/usr/lib/systemd/system/httpd.service", "GuessMainPID": "yes", "IOScheduling": "0", "Id": "httpd.service", "IgnoreOnIsolate": "no", "IgnoreOnSnapshot": "no", "IgnoreSIGPIPE": "yes", "InactiveEnterTimestampMonotonic": "0", "InactiveExitTimestampMonotonic": "0", "JobTimeoutAction": "none", "JobTimeoutUSec": "0", "KillMode": "mixed", "KillSignal": "28", "LimitAS": "18446744073709551615", "LimitCORE": "18446744073709551615", "LimitCPU": "18446744073709551615", "LimitDATA": "18446744073709551615", "LimitFSIZE": "18446744073709551615", "LimitLOCKS": "18446744073709551615", "LimitMEMLOCK": "65536", "LimitMSGQUEUE": "819200", "LimitNICE": "0", "LimitNOFILE": "4096", "LimitNPROC": "3792", "LimitRSS": "18446744073709551615", "LimitRTPRIO": "0", "LimitRTTIME": "18446744073709551615", "LimitSIGPENDING": "3792", "LimitSTACK": "18446744073709551615", "LoadState": "loaded", "MainPID": "0", "MemoryAccounting": "no", "MemoryCurrent": "18446744073709551615", "MemoryLimit": "18446744073709551615", "MountFlags": "0", "Names": "httpd.service", "NeedDaemonReload": "no", "Nice": "0", "NoNewPrivileges": "no", "NonBlocking": "no", "NotifyAccess": "main", "OOMScoreAdjust": "0", "OnFailureJobMode": "replace", "PermissionsStartOnly": "no", "PrivateDevices": "no", "PrivateNetwork": "no", "PrivateTmp": "yes", "ProtectHome": "no", "ProtectSystem": "no", "RefuseManualStart": "no", "RefuseManualStop": "no", "RemainAfterExit": "no", "Requires": "basic.target -.mount", "RequiresMountsFor": "/var/tmp", "Restart": "no", "RestartUSec": "100ms", "Result": "success", "RootDirectoryStartOnly": "no", "RuntimeDirectoryMode": "0755", "SameProcessGroup": "no", "SecureBits": "0", "SendSIGHUP": "no", "SendSIGKILL": "yes", "Slice": "system.slice", "StandardError": "inherit", "StandardInput": "null", "StandardOutput": "journal", "StartLimitAction": "none", "StartLimitBurst": "5", "StartLimitInterval": "10000000", "StartupBlockIOWeight": "18446744073709551615", "StartupCPUShares": "18446744073709551615", "StatusErrno": "0", "StopWhenUnneeded": "no", "SubState": "dead", "SyslogLevelPrefix": "yes", "SyslogPriority": "30", "SystemCallErrorNumber": "0", "TTYReset": "no", "TTYVHangup": "no", "TTYVTDisallocate": "no", "TasksAccounting": "no", "TasksCurrent": "18446744073709551615", "TasksMax": "18446744073709551615", "TimeoutStartUSec": "1min 30s", "TimeoutStopUSec": "1min 30s", "TimerSlackNSec": "50000", "Transient": "no", "Type": "notify", "UMask": "0022", "UnitFilePreset": "disabled", "UnitFileState": "disabled", "Wants": "httpd-init.service system.slice", "WatchdogTimestampMonotonic": "0", "WatchdogUSec": "0" } } |
Verify the status of service “httpd” in 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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
[kt-ansible@ip-172-31-15-116 ~]$ ansible Ktexperts-Group -b -m service -a "service httpd status" [WARNING]: Invalid characters were found in group names but not replaced, use -vvvv to see details ERROR! this task 'service' has extra params, which is only allowed in the following modules: shell, win_shell, include_vars, add_host, raw, include_role, meta, set_fact, include, import_tasks, script, import_role, include_tasks, group_by, command, win_command [kt-ansible@ip-172-31-15-116 ~]$ [kt-ansible@ip-172-31-15-116 ~]$ ansible Ktexperts-Group -b -a "service httpd status" [WARNING]: Invalid characters were found in group names but not replaced, use -vvvv to see details [WARNING]: Consider using the service module rather than running 'service'. If you need to use command because service is insufficient you can add 'warn: false' to this command task or set 'command_warnings=False' in ansible.cfg to get rid of this message. [WARNING]: Platform linux on host 172.31.11.251 is using the discovered Python interpreter at /usr/bin/python, but future installation of another Python interpreter could change this. See https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information. 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 Wed 2020-02-05 12:05:42 UTC; 3min 0s ago Docs: man:httpd.service(8) Main PID: 6958 (httpd) Status: "Total requests: 0; Idle/Busy workers 100/0;Requests/sec: 0; Bytes served/sec: 0 B/sec" CGroup: /system.slice/httpd.service ├─6958 /usr/sbin/httpd -DFOREGROUND ├─6959 /usr/sbin/httpd -DFOREGROUND ├─6960 /usr/sbin/httpd -DFOREGROUND ├─6961 /usr/sbin/httpd -DFOREGROUND ├─6962 /usr/sbin/httpd -DFOREGROUND └─6963 /usr/sbin/httpd -DFOREGROUND Feb 05 12:05:41 ip-172-31-11-251.ap-south-1.compute.internal systemd[1]: Starting The Apache HTTP Server... Feb 05 12:05:42 ip-172-31-11-251.ap-south-1.compute.internal systemd[1]: Started The Apache HTTP Server.Redirecting to /bin/systemctl status httpd.service [WARNING]: Platform linux on host 172.31.2.38 is using the discovered Python interpreter at /usr/bin/python, but future installation of another Python interpreter could change this. See https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information. 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 Wed 2020-02-05 12:05:42 UTC; 3min 0s ago Docs: man:httpd.service(8) Main PID: 6918 (httpd) Status: "Total requests: 0; Idle/Busy workers 100/0;Requests/sec: 0; Bytes served/sec: 0 B/sec" CGroup: /system.slice/httpd.service ├─6918 /usr/sbin/httpd -DFOREGROUND ├─6919 /usr/sbin/httpd -DFOREGROUND ├─6920 /usr/sbin/httpd -DFOREGROUND ├─6921 /usr/sbin/httpd -DFOREGROUND ├─6922 /usr/sbin/httpd -DFOREGROUND └─6923 /usr/sbin/httpd -DFOREGROUND Feb 05 12:05:41 ip-172-31-2-38.ap-south-1.compute.internal systemd[1]: Starting The Apache HTTP Server... Feb 05 12:05:42 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” in 2 hosts
1 |
ansible Ktexperts-Group -b -m service -a "name=httpd state=stopped" |
Restart the service “httpd” in 2 hosts
1 |
ansible Ktexperts-Group -b -m service -a "name=httpd state=restarted" |
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