Run Ansible Adhoc commands or playbook in Local mode

If you are trying to run Ansible Adhoc commands or playbook in Local mode, these articles will help you.

Ansible can’t run playbooks locally without ssh if ansible_connection=local is defined in the hosts file, although it can run playbooks locally without ssh with connection: local in the playbook or with flag –connection=local.

# # # # # # # # # # # # Method 1 # # # # # # # # # # # # # # # # # #

ansible 127.0.0.1 -m file -a "dest=/opt/a.txt mode=600 state=touch"
ansible 127.0.0.1 -m file -a "dest=/opt/a.txt mode=600 state=file"

# # # # # # # # # # # # Method 2 # # # # # # # # # # # # # # # # # #

Inventory file
127.0.0.1 ansible_connection=local
localhost ansible_connection=local

# # # # # # # # # # # # Method 3 # # # # # # # # # # # # # # # # # #

---

- name: run the playbook tasks on the localhost
hosts: 127.0.0.1
connection: local
become: yes
tasks:

- name: print out the hostname of target
command: hostname

- name: ensure aptitude is installed
command: apt-get -y install aptitude

- name: update the apt package index i.e. apt-get update
apt: update_cache=yes

- name: upgrade system packages i.e. apt-get upgrade
apt: upgrade=yes

Tagged : / / / /

Ansible Adhoc Commands Lab & Excercise – Part 1

Execution Mode – Local

  1. Write a Ansible Adhoc Commands to create a group called “deploy”

  2. Write a Ansible Adhoc Commands to create a user called “deploy-user” which is part of group called “group” and with /bin/bash shell.

  3. Write a Ansble Adhoc commands install package named “httpd” in RHEL/centos.

  4. Write a Ansible Adhoc commands to start and enable the service named “httpd”

  5. Write a Ansible commands to create a file called “index.html” in /var/www/html with some dummy html contents.

  6. Write a Ansible commands to reboot a self machine.

  7. Write a Ansible commands to install a package called “git”, “wget”.

  8. Write a Ansible Adhoc commands to clone git repo. thttps://github.com/scmgalaxy/ansible-role-template

Tagged : / / / /