Skip to content

Instantly share code, notes, and snippets.

@lzap
Created November 3, 2021 18:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lzap/9a00adf80a1d823f732951271de749e8 to your computer and use it in GitHub Desktop.
Save lzap/9a00adf80a1d823f732951271de749e8 to your computer and use it in GitHub Desktop.
Example Ansible playbook
---
- name: Ensure vim on localhost1
hosts: localhost1
remote_user: root
tasks:
- name: Ensure vim-minimal is installed
ansible.builtin.yum:
name: vim-minimal
state: latest
- name: Ensure vim-enhanced is installed
ansible.builtin.yum:
name: vim-enhanced
state: latest
- name: Ensure vim on localhost2
hosts: localhost2
remote_user: root
tasks:
- name: Ensure vim-minimal is installed
ansible.builtin.yum:
name: vim-minimal
state: latest
- name: Ensure vim-enhanced is installed
ansible.builtin.yum:
name: vim-enhanced
state: latest
- name: Ensure vim on localhost1 and 2
hosts:
- localhost1
- localhost2
remote_user: root
tasks:
- name: Ensure vim-minimal is installed
ansible.builtin.yum:
name: vim-minimal
state: latest
- name: Ensure vim-enhanced is installed
ansible.builtin.yum:
name: vim-enhanced
state: latest

Create inventory entries for two hosts:

cat /etc/ansible/hosts
localhost1
localhost2

Make sure both localhost1 and localhost2 resolve to 127.0.0.1:

cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4 localhost1 localhost2

Generate SSH key, if you don't have any:

ssh-keygen

Connect to both and accept key fingerprint and install the key:

ssh-copy-id root@localhost1
ssh-copy-id root@localhost2

Finally, run the Ansible playbook:

ansible-playbook harmless-playbook.yaml

When using Foreman with Ansible plugin, do this:

ANSIBLE_CONFIG=/etc/foreman-proxy/ansible.cfg ANSIBLE_LOAD_CALLBACK_PLUGINS=1 ansible-playbook harmless-playbook.yaml
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment