Ansible

Ansible Ad-Hoc Commands | How to Run Ansible Ad-Hoc Commands

Explore Ad Hoc Commands

This ansible tutorial about ansible adhoc command and How to Run Ansible Ad-Hoc Commands.

You can also visity the below topics.
Ansible Tutorial | What is Ansible | Learning Ansible basics

What is Ansible Configuration Files Explained with Examples

How to install ansible on centos 7 | Ansible Lab Setup

After you have successfully configured the Ansible Controller and remote hosts, you can run Ansible ad hoc commands and playbooks as the automon user without being prompted for the password.
In this tutorial , you explore ad hoc commands to manage remote hosts.

You configure ansible.cfg and the static inventory needed to complete this lab. You use -u to specify the automon user and –private-key to specify the private key.

  • Verify remote user:

                        [automon@myaansible ~]$ ansible node1 -a whoami
                        node1 | CHANGED | rc=0 >>
                        root

    • The command is expected to show root because it is using the default ansible.cfg and the SSH keys defined in the default /etc/ansible/hosts static inventory.
  • Create a directory called my_first_project as your working directory for all future labs and an ansible.cfg file with a [defaults] section for specifying user-specific settings.

    • [automon@myaansible ~]$ mkdir my_first_project
    • [automon@myaansible ~]$ cd my_first_project
    • [automon@myaansible my_first_project]$ cat << EOF > ansible.cfg
      > [defaults]
      > inventory = /home/automon/my_first_project/hosts
      > remote_user = automon
      > private_key_file = /home/automon/.ssh/id_rsa
      > host_key_checking = false
      > EOF
  • Verify the contents of the ansible.cfg file:
    • [automon@myaansible my_first_project]$ cat ansible.cfg
      [defaults]
      inventory = /home/automon/my_first_project/hosts
      remote_user = automon
      private_key_file = /home/automon/.ssh/id_rsa
      host_key_checking = false
  • Create /home/automon/my_first_project/hosts as the static inventory, which contains the hostnames of all of the remote hosts:
    • [automon@myaansible my_first_project]$ cat << EOF > hosts
      > node1
      > EOF
  • Verify the contents of the hosts file:
    • [automon@myaansible my_first_project]$ cat hosts
      node1

Run Ansible Ad-Hoc Commands

This is first ansible adhoc command. Test the server connectivity with the ansible ping module.

  • [automon@myaansible my_first_project]$ ansible all -m ping

Execute an ansible adhoc command on localhost to identify the user account used by Ansible to perform operations on managed hosts.

  • [automon@myaansible my_first_project]$ ansible localhost -m command -a ‘id’
    localhost | CHANGED | rc=0 >>
    uid=1005(automon) gid=1005(automon) groups=1005(automon),10(wheel) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023

Execute an ansible adhoc command to display the contents of the /etc/resolv.conf file on node1 as the automon user.

  • [automon@myaansible my_first_project]$ ansible node1 -m command -a ‘cat /etc/resolv.conf’
    node1 | CHANGED | rc=0 >>
    # Generated by NetworkManager
    nameserver 192.168.29.1
    nameserver 8.8.8.8
    nameserver 8.8.4.4
    # NOTE: the libc resolver may not support more than 3 nameservers.
    # The nameservers listed below may not be recognized.
    nameserver 2405:201:d002:1a8::c0a8:1d01

Execute an ad hoc command to check remote system uptime.

  • [automon@myaansible my_first_project]$ ansible node1 -m shell -a “uptime”
    node1 | CHANGED | rc=0 >>
    03:30:46 up 2:27, 3 users, load average: 0.00, 0.05, 0.05

Execute an ad hoc command using the copy module and the automon account to change the contents of the /etc/issue file to include the message “#This is my ansible remote system” on all of the remote hosts, but escalate the root user’s privileges using -b or –become or else you will received insufficient permissions error.

  • [automon@myaansible my_first_project]$ ansible all -m copy -a ‘content=”##This is my ansible remote system\n” dest=/etc/issue’ –become
    node1 | CHANGED => {
    “ansible_facts”: {
    “discovered_interpreter_python”: “/usr/bin/python”
    },
    “changed”: true,
    “checksum”: “c285b8ff41ead6d26c3044e4f2f87ec8539704cb”,
    “dest”: “/etc/issue”,
    “gid”: 0,
    “group”: “root”,
    “md5sum”: “d1c8a7d8c660db3b812cdbce38e756a2”,
    “mode”: “0644”,
    “owner”: “root”,
    “secontext”: “system_u:object_r:etc_t:s0”,
    “size”: 35,
    “src”: “/home/automon/.ansible/tmp/ansible-tmp-1632639477.22-2282-58642497555213/source”,
    “state”: “file”,
    “uid”: 0
    }

Execute an ansible adhoc command using the copy module and the automon account to copy the file from ansible hosts to all of the remote hosts.

  • [automon@myaansible my_first_project]$ ansible all -m copy -a ‘src=/home/automon/my_first_project/test.sh dest=/home/automon/’
    node1 | CHANGED => {
    “ansible_facts”: {
    “discovered_interpreter_python”: “/usr/bin/python”
    },
    “changed”: true,
    “checksum”: “ac4516de7ee3d181b8d811bbed3781db23cbf07b”,
    “dest”: “/home/automon/test.sh”,
    “gid”: 1001,
    “group”: “automon”,
    “md5sum”: “ad8242e4904d6feeeafdc432d60cc05d”,
    “mode”: “0664”,
    “owner”: “automon”,
    “secontext”: “unconfined_u:object_r:user_home_t:s0”,
    “size”: 7,
    “src”: “/home/automon/.ansible/tmp/ansible-tmp-1632641959.77-2878-72076777087980/source”,
    “state”: “file”,
    “uid”: 1001
    }

Execute an ansible adhoc command to Installing Apache on remote hosts using the adhoc command.

  • [automon@myaansible my_first_project]$ ansible node1 -m yum -a “name=httpd state=present” –become
    node1 | CHANGED => {
    “ansible_facts”: {
    “discovered_interpreter_python”: “/usr/bin/python”
    },
    “changed”: true,
    “changes”: {
    “installed”: [
    “httpd”
    ]
    },
    “msg”: “”,
    “rc”: 0,
    “results”: [
    “Loaded plugins: fastestmirror\nLoading mirror speeds from cached hostfile\n * base: mirrors.piconets.webwerks.in\n * extras: mirrors.piconets.webwerks.in\n * updates: mirrors.piconets.webwerks.in\nResolving Dependencies\n–> Running transaction check\n—> Package httpd.x86_64 0:2.4.6-97.el7.centos will be installed\n–> Processing Dependency: httpd-tools = 2.4.6-97.el7.centos for package: httpd-2.4.6-97.el7.centos.x86_64\n–> Running transaction check\n—> Package httpd-tools.x86_64 0:2.4.6-97.el7.centos will be installed\n–> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package Arch Version Repository Size\n================================================================================\nInstalling:\n httpd x86_64 2.4.6-97.el7.centos updates 2.7 M\nInstalling for dependencies:\n httpd-tools x86_64 2.4.6-97.el7.centos updates 93 k\n\nTransaction Summary\n================================================================================\nInstall 1 Package (+1 Dependent package)\n\nTotal download size: 2.8 M\nInstalled size: 9.5 M\nDownloading packages:\n——————————————————————————–\nTotal 2.7 MB/s | 2.8 MB 00:01 \nRunning transaction check\nRunning transaction test\nTransaction test succeeded\nRunning transaction\n Installing : httpd-tools-2.4.6-97.el7.centos.x86_64 1/2 \n Installing : httpd-2.4.6-97.el7.centos.x86_64 2/2 \n Verifying : httpd-2.4.6-97.el7.centos.x86_64 1/2 \n Verifying : httpd-tools-2.4.6-97.el7.centos.x86_64 2/2 \n\nInstalled:\n httpd.x86_64 0:2.4.6-97.el7.centos \n\nDependency Installed:\n httpd-tools.x86_64 0:2.4.6-97.el7.centos \n\nComplete!\n”
    ]
    }

Execute an ad hoc command to uninstall Apache on remote hosts using the adhoc command.

  • [automon@myaansible my_first_project]$  ansible node1 -m yum -a “name=httpd state=absent” –become
    node1 | CHANGED => {
    “ansible_facts”: {
    “discovered_interpreter_python”: “/usr/bin/python”
    },
    “changed”: true,
    “changes”: {
    “removed”: [
    “httpd”
    ]
    },
    “msg”: “”,
    “rc”: 0,
    “results”: [
    “Loaded plugins: fastestmirror\nResolving Dependencies\n–> Running transaction check\n—> Package httpd.x86_64 0:2.4.6-97.el7.centos will be erased\n–> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package Arch Version Repository Size\n================================================================================\nRemoving:\n httpd x86_64 2.4.6-97.el7.centos @updates 9.4 M\n\nTransaction Summary\n================================================================================\nRemove 1 Package\n\nInstalled size: 9.4 M\nDownloading packages:\nRunning transaction check\nRunning transaction test\nTransaction test succeeded\nRunning transaction\n Erasing : httpd-2.4.6-97.el7.centos.x86_64 1/1 \n Verifying : httpd-2.4.6-97.el7.centos.x86_64 1/1 \n\nRemoved:\n httpd.x86_64 0:2.4.6-97.el7.centos \n\nComplete!\n”
    ]
    }

Related Posts

Leave a Reply

Your email address will not be published.