Ansible is a popular automation tool used by sysadmins and developers to get computers in a specific state. Ansible modules are, in a way, what commands are to a Linux computer. They provide solutions to specific problems, and one common task when maintaining computers is keeping them updated and consistent. In this article, I show you how to install software packages with Ansible.

Requirements

To run Ansible ad hoc commands and playbooks on managed nodes, a remote user with SSH access is needed on each managed node. For this example, I'll call that user tux. Tux needs permission to run commands with elevated privileges to install packages. Therefore the file /etc/sudoers.d/tux with the following content must exist on all managed nodes:

tux ALL=(ALL) NOPASSWD:ALL

Use visudo to edit the /etc/sudoers file entries.

Package installation

Suppose you've found a cool new package called sysstat that you now want to install on all your hosts at once. That's an easy job for Ansible:

$ ansible all --user tux --become \
--module-name dnf -a’name=sysstat state=latest’

And you're done. 

[ You might also like: How to create an Ansible Playbook ]

That one line is called an Ansible ad hoc command. These are usually one-time tasks that you don't expect to repeat. However, in the interest of reusability, it’s better to put such instructions into an Ansible playbook, like this:

---
- hosts: all
  tasks:
  - name: Make sure the current version of ‘sysstat’ is installed.
    dnf:
      name: sysstat
      state: latest

Save this playbook to a file called install_packages.yml, and then you can run it with the following command:

$ ansible-playbook -u tux -b install_packages.yml

You can put it on a schedule with cron, making sure the desired package gets installed and reinstalled if someone removes it.

Install several packages

What if you want to install a list of packages on all of your hosts? That's easy, too:

---
- hosts: all
  tasks:
  - name: Package installation
    dnf:
      name:
        - sysstat
        - httpd
        - mariadb-server
      state: latest

Run the playbook:

$ ansible-playbook -u tux -b install_packages.yml

I'm sure you get the idea. Need more packages? Just add them to the list.

Wrap up

Managing software with Ansible is pretty straightforward. You can use the DNF module to manage the actual installation and a YAML-based Ansible playbook to distribute the installation instructions to your managed nodes. This is another excellent example of how Ansible makes life easier for sysadmins who manage many systems.

[ Get this free ebook: Managing your Kubernetes clusters for dummies. ]


執筆者紹介

Jörg has been a Sysadmin for over ten years now. His fields of operation include Virtualization (VMware), Linux System Administration and Automation (RHEL), Firewalling (Forcepoint), and Loadbalancing (F5). He is a member of the Red Hat Accelerators Community and author of his personal blog at https://www.my-it-brain.de.

UI_Icon-Red_Hat-Close-A-Black-RGB

チャンネル別に見る

automation icon

自動化

テクノロジー、チームおよび環境に関する IT 自動化の最新情報

AI icon

AI (人工知能)

お客様が AI ワークロードをどこでも自由に実行することを可能にするプラットフォームについてのアップデート

open hybrid cloud icon

オープン・ハイブリッドクラウド

ハイブリッドクラウドで柔軟に未来を築く方法をご確認ください。

security icon

セキュリティ

環境やテクノロジー全体に及ぶリスクを軽減する方法に関する最新情報

edge icon

エッジコンピューティング

エッジでの運用を単純化するプラットフォームのアップデート

Infrastructure icon

インフラストラクチャ

世界有数のエンタープライズ向け Linux プラットフォームの最新情報

application development icon

アプリケーション

アプリケーションの最も困難な課題に対する Red Hat ソリューションの詳細

Virtualization icon

仮想化

オンプレミスまたは複数クラウドでのワークロードに対応するエンタープライズ仮想化の将来についてご覧ください