Automation plays a vital role in a sysadmin's or DevOps administrator's day-to-day life; patching, installing, managing components, automating network devices, and dealing with containers are tasks that automation can handle. Due to the sequential behavior of scripting, many sysadmins have not considered automation to be a promising tool. Ansible, as an automation tool that executes in parallel, appears to be changing that trend and becoming widely used.
Ansible uses ad hoc commands and playbooks to achieve automation. Ad hoc commands are mostly single linear commands that can execute from controllers. Ad hoc commands make Ansible useful even when a sysadmin needs to perform a one-time activity.
This article examines different aspects and options available in Ansible ad hoc commands such as --limit
, -B
, -a
, -m
, and -b
.
Consideration: Inventory details are mentioned in the myinventory
file, as shown here:
cat myinventory
[app]
localhost
172.6.7.8
172.6.7.80
[multi]
172.6.7.4
172.6.7.10
172.6.7.8
A few examples of ad hoc commands are included in this line of code:
$ ansible app -b -m yum -a "name=MySQL-python state=present"
These commands install a MySQL-python
package on the app group. The -b
option runs an operation with become
. You can provide the username using –become-user
. The -m
option specifies modules such as: copy
, user
, yum
, and more. The -a
option is an attribute of a specific module.
Here is another example, which executes only on the IP address specified (172.6.7.10
):
$ ansible app -b -a "systemctl status ntpd" --limit "172.6.7.10"
The --limit
option limits the selected hosts to a different pattern, and you can also use wildcards like *
, !
, and &
(as shown in the examples below).
The first example command below executes the ad hoc command on any node that ends with a .4
IP address. The second example executes on an app group on the IP 172.6.7.80
. The last example executes on any machine from both groups. In this case, 172.6.7.8
is present in both groups.
$ ansible app -b -a "systemctl status ntpd" --limit "*.4"
$ ansible app -b -a "systemctl status ntpd" --limit "app:!172.6.7.80”
$ ansible app -b -a "systemctl status ntpd" --limit "app:&multi”
Ad hoc commands can also be used with the user
, stat
, copy
, and file
modules:
$ ansible app -s -m user -a "name=admin state=present"
$ ansible multi -m stat -a "path=/etc/environment"
$ ansible multi -m copy -a "src=/etc/hosts dest=/tmp/hosts"
$ ansible multi -m file -a "dest=/tmp/test mode=644 state=directory"
In some cases, an Ansible ad hoc command may take a while to execute because it needs to wait for a long time. Using the -B
option, you can easily schedule the commands in the background:
$ ansible multi -b -B 3600 -a "yum -y update"
Managed-host logging is easy to achieve using the tail
/tailf
commands:
$ ansible multi -b -a "tail /var/log/messages"
[ Need more on Ansible? Take a free technical overview course from Red Hat. Ansible Essentials: Simplicity in Automation Technical Overview. ]
Wrapping up
Using ad hoc commands, a sysadmin can verify the desired state of a particular task before and after execution. The options in this article may make life easier while dealing with Ansible automation.
Sull'autore
Ashutosh is an open source software advocate, docker community leader, Fedora active contributor, Red Hat / SUSE Certified Instructor with 19 years of experience as a trainer and consultant.
Ricerca per canale
Automazione
Novità sull'automazione IT di tecnologie, team e ambienti
Intelligenza artificiale
Aggiornamenti sulle piattaforme che consentono alle aziende di eseguire carichi di lavoro IA ovunque
Hybrid cloud open source
Scopri come affrontare il futuro in modo più agile grazie al cloud ibrido
Sicurezza
Le ultime novità sulle nostre soluzioni per ridurre i rischi nelle tecnologie e negli ambienti
Edge computing
Aggiornamenti sulle piattaforme che semplificano l'operatività edge
Infrastruttura
Le ultime novità sulla piattaforma Linux aziendale leader a livello mondiale
Applicazioni
Approfondimenti sulle nostre soluzioni alle sfide applicative più difficili
Serie originali
Raccontiamo le interessanti storie di leader e creatori di tecnologie pensate per le aziende
Prodotti
- Red Hat Enterprise Linux
- Red Hat OpenShift
- Red Hat Ansible Automation Platform
- Servizi cloud
- Scopri tutti i prodotti
Strumenti
- Formazione e certificazioni
- Il mio account
- Supporto clienti
- Risorse per sviluppatori
- Trova un partner
- Red Hat Ecosystem Catalog
- Calcola il valore delle soluzioni Red Hat
- Documentazione
Prova, acquista, vendi
Comunica
- Contatta l'ufficio vendite
- Contatta l'assistenza clienti
- Contatta un esperto della formazione
- Social media
Informazioni su Red Hat
Red Hat è leader mondiale nella fornitura di soluzioni open source per le aziende, tra cui Linux, Kubernetes, container e soluzioni cloud. Le nostre soluzioni open source, rese sicure per un uso aziendale, consentono di operare su più piattaforme e ambienti, dal datacenter centrale all'edge della rete.
Seleziona la tua lingua
Red Hat legal and privacy links
- Informazioni su Red Hat
- Opportunità di lavoro
- Eventi
- Sedi
- Contattaci
- Blog di Red Hat
- Diversità, equità e inclusione
- Cool Stuff Store
- Red Hat Summit