Dear Readers.
In this article,we will see Ways to Push Code From Ansible Server to Hosts.
Generally we push the code from ansible server to hosts by using following ways.
- Ad-Hoc Commands
- Single Module
- Modules (More than one module)
1. Ad-Hoc Commands
- The Ad-Hoc Commands are simple linux commands.
- The Ad-Hoc command is the one-liner ansible command that performs one task on the target host.
- Use ad-hoc tasks really quick & don’t want to save for later
- These are quick one-liner without writing a playbook.
- Use ad-hoc tasks really quick & don’t want to save for later.
- These are quick one-liner without writing a playbook.
- It allows you to execute simple one-line task against one or group of hosts defined on the inventory file configuration.
- An Ad-Hoc command will only have two parameters, the group of a host that you want to perform the task and the Ansible module to run.
- The Ad-Hoc command gives you more advantage for exploring ansible itself.
Syntax for Ad-Hoc
1 |
ansible [options] |
To run an arbitrary, we need to mention -a in the command
1 |
ansible -a |
To run anything with sudo, we need to mention -b in the command
1 |
ansible -b -a |
Examples
See the list of all hosts and all groups of inventory
1 2 3 4 5 |
[kt-ansible@ip-172-31-15-116 ~]$ ansible all --list-hosts hosts (2): 172.31.11.251 172.31.2.38 |
See the list of all hosts inside the group “Ktexperts-Group”
1 2 3 4 5 |
[kt-ansible@ip-172-31-15-116 ~]$ ansible Ktexperts-Group --list-hosts hosts (2): 172.31.11.251 172.31.2.38 |
See the list of files/directories in 2 hosts
Here, we need to use “-a” to run an arbitrary cmd.
1 2 3 4 5 |
[kt-ansible@ip-172-31-15-116 ~]$ ansible Ktexperts-Group -a "ls" 172.31.11.251 | CHANGED | rc=0 >> 172.31.2.38 | CHANGED | rc=0 >> |
Install the package “httpd” in 2 hosts
We need to run with sudo to install the package.
Use -b in the following command to get sudo privileges.
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 |
[kt-ansible@ip-172-31-15-116 ~]$ ansible Ktexperts-Group -b -a "yum install httpd -y" 172.31.11.251 | CHANGED | rc=0 >> Loaded plugins: extras_suggestions, langpacks, priorities, update-motd Resolving Dependencies --> Running transaction check ---> Package httpd.x86_64 0:2.4.41-1.amzn2.0.1 will be installed --> Processing Dependency: httpd-tools = 2.4.41-1.amzn2.0.1 for package: httpd-2.4.41-1.amzn2.0.1.x86_64 --> Processing Dependency: httpd-filesystem = 2.4.41-1.amzn2.0.1 for package: httpd-2.4.41-1.amzn2.0.1.x86_64 --> Processing Dependency: system-logos-httpd for package: httpd-2.4.41-1.amzn2.0.1.x86_64 --> Processing Dependency: mod_http2 for package: httpd-2.4.41-1.amzn2.0.1.x86_64 --> Processing Dependency: httpd-filesystem for package: httpd-2.4.41-1.amzn2.0.1.x86_64 --> Processing Dependency: /etc/mime.types for package: httpd-2.4.41-1.amzn2.0.1.x86_64 --> Processing Dependency: libaprutil-1.so.0()(64bit) for package: httpd-2.4.41-1.amzn2.0.1.x86_64 --> Processing Dependency: libapr-1.so.0()(64bit) for package: httpd-2.4.41-1.amzn2.0.1.x86_64 --> Running transaction check ---> Package apr.x86_64 0:1.6.3-5.amzn2.0.2 will be installed ---> Package apr-util.x86_64 0:1.6.1-5.amzn2.0.2 will be installed --> Processing Dependency: apr-util-bdb(x86-64) = 1.6.1-5.amzn2.0.2 for package: apr-util-1.6.1-5.amzn2.0.2.x86_64 ---> Package generic-logos-httpd.noarch 0:18.0.0-4.amzn2 will be installed ---> Package httpd-filesystem.noarch 0:2.4.41-1.amzn2.0.1 will be installed ---> Package httpd-tools.x86_64 0:2.4.41-1.amzn2.0.1 will be installed ---> Package mailcap.noarch 0:2.1.41-2.amzn2 will be installed ---> Package mod_http2.x86_64 0:1.15.3-2.amzn2 will be installed --> Running transaction check ---> Package apr-util-bdb.x86_64 0:1.6.1-5.amzn2.0.2 will be installed --> Finished Dependency Resolution Dependencies Resolved ================================================================================ Package Arch Version Repository Size ================================================================================ Installing: httpd x86_64 2.4.41-1.amzn2.0.1 amzn2-core 1.3 M Installing for dependencies: apr x86_64 1.6.3-5.amzn2.0.2 amzn2-core 118 k apr-util x86_64 1.6.1-5.amzn2.0.2 amzn2-core 99 k apr-util-bdb x86_64 1.6.1-5.amzn2.0.2 amzn2-core 19 k generic-logos-httpd noarch 18.0.0-4.amzn2 amzn2-core 19 k httpd-filesystem noarch 2.4.41-1.amzn2.0.1 amzn2-core 23 k httpd-tools x86_64 2.4.41-1.amzn2.0.1 amzn2-core 87 k mailcap noarch 2.1.41-2.amzn2 amzn2-core 31 k mod_http2 x86_64 1.15.3-2.amzn2 amzn2-core 146 k Transaction Summary ================================================================================ Install 1 Package (+8 Dependent packages) Total download size: 1.8 M Installed size: 5.1 M Downloading packages: -------------------------------------------------------------------------------- Total 10 MB/s | 1.8 MB 00:00 Running transaction check Running transaction test Transaction test succeeded Running transaction Installing : apr-1.6.3-5.amzn2.0.2.x86_64 1/9 Installing : apr-util-bdb-1.6.1-5.amzn2.0.2.x86_64 2/9 Installing : apr-util-1.6.1-5.amzn2.0.2.x86_64 3/9 Installing : httpd-tools-2.4.41-1.amzn2.0.1.x86_64 4/9 Installing : generic-logos-httpd-18.0.0-4.amzn2.noarch 5/9 Installing : mailcap-2.1.41-2.amzn2.noarch 6/9 Installing : httpd-filesystem-2.4.41-1.amzn2.0.1.noarch 7/9 Installing : mod_http2-1.15.3-2.amzn2.x86_64 8/9 Installing : httpd-2.4.41-1.amzn2.0.1.x86_64 9/9 Verifying : apr-util-1.6.1-5.amzn2.0.2.x86_64 1/9 Verifying : apr-util-bdb-1.6.1-5.amzn2.0.2.x86_64 2/9 Verifying : httpd-2.4.41-1.amzn2.0.1.x86_64 3/9 Verifying : httpd-filesystem-2.4.41-1.amzn2.0.1.noarch 4/9 Verifying : mod_http2-1.15.3-2.amzn2.x86_64 5/9 Verifying : apr-1.6.3-5.amzn2.0.2.x86_64 6/9 Verifying : mailcap-2.1.41-2.amzn2.noarch 7/9 Verifying : generic-logos-httpd-18.0.0-4.amzn2.noarch 8/9 Verifying : httpd-tools-2.4.41-1.amzn2.0.1.x86_64 9/9 Installed: httpd.x86_64 0:2.4.41-1.amzn2.0.1 Dependency Installed: apr.x86_64 0:1.6.3-5.amzn2.0.2 apr-util.x86_64 0:1.6.1-5.amzn2.0.2 apr-util-bdb.x86_64 0:1.6.1-5.amzn2.0.2 generic-logos-httpd.noarch 0:18.0.0-4.amzn2 httpd-filesystem.noarch 0:2.4.41-1.amzn2.0.1 httpd-tools.x86_64 0:2.4.41-1.amzn2.0.1 mailcap.noarch 0:2.1.41-2.amzn2 mod_http2.x86_64 0:1.15.3-2.amzn2 Complete! 172.31.2.38 | CHANGED | rc=0 >> Loaded plugins: extras_suggestions, langpacks, priorities, update-motd Resolving Dependencies --> Running transaction check ---> Package httpd.x86_64 0:2.4.41-1.amzn2.0.1 will be installed --> Processing Dependency: httpd-tools = 2.4.41-1.amzn2.0.1 for package: httpd-2.4.41-1.amzn2.0.1.x86_64 --> Processing Dependency: httpd-filesystem = 2.4.41-1.amzn2.0.1 for package: httpd-2.4.41-1.amzn2.0.1.x86_64 --> Processing Dependency: system-logos-httpd for package: httpd-2.4.41-1.amzn2.0.1.x86_64 --> Processing Dependency: mod_http2 for package: httpd-2.4.41-1.amzn2.0.1.x86_64 --> Processing Dependency: httpd-filesystem for package: httpd-2.4.41-1.amzn2.0.1.x86_64 --> Processing Dependency: /etc/mime.types for package: httpd-2.4.41-1.amzn2.0.1.x86_64 --> Processing Dependency: libaprutil-1.so.0()(64bit) for package: httpd-2.4.41-1.amzn2.0.1.x86_64 --> Processing Dependency: libapr-1.so.0()(64bit) for package: httpd-2.4.41-1.amzn2.0.1.x86_64 --> Running transaction check ---> Package apr.x86_64 0:1.6.3-5.amzn2.0.2 will be installed ---> Package apr-util.x86_64 0:1.6.1-5.amzn2.0.2 will be installed --> Processing Dependency: apr-util-bdb(x86-64) = 1.6.1-5.amzn2.0.2 for package: apr-util-1.6.1-5.amzn2.0.2.x86_64 ---> Package generic-logos-httpd.noarch 0:18.0.0-4.amzn2 will be installed ---> Package httpd-filesystem.noarch 0:2.4.41-1.amzn2.0.1 will be installed ---> Package httpd-tools.x86_64 0:2.4.41-1.amzn2.0.1 will be installed ---> Package mailcap.noarch 0:2.1.41-2.amzn2 will be installed ---> Package mod_http2.x86_64 0:1.15.3-2.amzn2 will be installed --> Running transaction check ---> Package apr-util-bdb.x86_64 0:1.6.1-5.amzn2.0.2 will be installed --> Finished Dependency Resolution Dependencies Resolved ================================================================================ Package Arch Version Repository Size ================================================================================ Installing: httpd x86_64 2.4.41-1.amzn2.0.1 amzn2-core 1.3 M Installing for dependencies: apr x86_64 1.6.3-5.amzn2.0.2 amzn2-core 118 k apr-util x86_64 1.6.1-5.amzn2.0.2 amzn2-core 99 k apr-util-bdb x86_64 1.6.1-5.amzn2.0.2 amzn2-core 19 k generic-logos-httpd noarch 18.0.0-4.amzn2 amzn2-core 19 k httpd-filesystem noarch 2.4.41-1.amzn2.0.1 amzn2-core 23 k httpd-tools x86_64 2.4.41-1.amzn2.0.1 amzn2-core 87 k mailcap noarch 2.1.41-2.amzn2 amzn2-core 31 k mod_http2 x86_64 1.15.3-2.amzn2 amzn2-core 146 k Transaction Summary ================================================================================ Install 1 Package (+8 Dependent packages) Total download size: 1.8 M Installed size: 5.1 M Downloading packages: -------------------------------------------------------------------------------- Total 11 MB/s | 1.8 MB 00:00 Running transaction check Running transaction test Transaction test succeeded Running transaction Installing : apr-1.6.3-5.amzn2.0.2.x86_64 1/9 Installing : apr-util-bdb-1.6.1-5.amzn2.0.2.x86_64 2/9 Installing : apr-util-1.6.1-5.amzn2.0.2.x86_64 3/9 Installing : httpd-tools-2.4.41-1.amzn2.0.1.x86_64 4/9 Installing : generic-logos-httpd-18.0.0-4.amzn2.noarch 5/9 Installing : mailcap-2.1.41-2.amzn2.noarch 6/9 Installing : httpd-filesystem-2.4.41-1.amzn2.0.1.noarch 7/9 Installing : mod_http2-1.15.3-2.amzn2.x86_64 8/9 Installing : httpd-2.4.41-1.amzn2.0.1.x86_64 9/9 Verifying : apr-util-1.6.1-5.amzn2.0.2.x86_64 1/9 Verifying : apr-util-bdb-1.6.1-5.amzn2.0.2.x86_64 2/9 Verifying : httpd-2.4.41-1.amzn2.0.1.x86_64 3/9 Verifying : httpd-filesystem-2.4.41-1.amzn2.0.1.noarch 4/9 Verifying : mod_http2-1.15.3-2.amzn2.x86_64 5/9 Verifying : apr-1.6.3-5.amzn2.0.2.x86_64 6/9 Verifying : mailcap-2.1.41-2.amzn2.noarch 7/9 Verifying : generic-logos-httpd-18.0.0-4.amzn2.noarch 8/9 Verifying : httpd-tools-2.4.41-1.amzn2.0.1.x86_64 9/9 Installed: httpd.x86_64 0:2.4.41-1.amzn2.0.1 Dependency Installed: apr.x86_64 0:1.6.3-5.amzn2.0.2 apr-util.x86_64 0:1.6.1-5.amzn2.0.2 apr-util-bdb.x86_64 0:1.6.1-5.amzn2.0.2 generic-logos-httpd.noarch 0:18.0.0-4.amzn2 httpd-filesystem.noarch 0:2.4.41-1.amzn2.0.1 httpd-tools.x86_64 0:2.4.41-1.amzn2.0.1 mailcap.noarch 0:2.1.41-2.amzn2 mod_http2.x86_64 0:1.15.3-2.amzn2 Complete! |
2. Single Module
Single Module
Ansible modules are discrete units of code which can be used from the command line.
You are able to perform tasks without creating a playbook first, such as rebooting servers, managing services, editing the line configuration, copy a file to only one host, install only one package.
By using single module,we can perform only one task at a time.
If you want to execute more than one module at a time,we need to execute modules.
Syntex for Single Module
To run an arbitrary cmd use -a & use -m to run a module
1 |
ansible [group] -m <module name> |
1 |
ansible [group] -m <module name> -a "arbitrary" |
Examples
Verify the host are pinging or not
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
[kt-ansible@ip-172-31-15-116 ~]$ ansible Ktexperts-Group -m ping 172.31.11.251 | SUCCESS => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": false, "ping": "pong" } 172.31.2.38 | SUCCESS => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": false, "ping": "pong" } |
See the hostname of 2 hosts
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 |
3. Modules (Run more than one Module)
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.
Playbook Structure
Each playbook is a collection of one or more plays.
Playbooks are structured by using Plays. There can be more than one play inside a playbook.
The function of the play is to map a set of instructions which is defined against a particular host.
A YAML starts with — (3 hyphens) always.
Syntex for running the playbook
We write the code in a file then we execute the file by using below command.
1 |
ansible-playbook <playbook name> |
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 |
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