Hello Automation specialists, This post is for trying all the Ansible Command-line tools, it's like a cheat sheet for ansible CLI with executed examples to better understand their usage.
Ansible command-line utilities can be executed only on the box where the Ansible engine is installed and running.
ansible
ansible -i inventory.yml all --list-hosts # all ansible -i inventory.yml dbserver --list-hosts #Specific group ansible -i inventory.yml common --list-hosts # same as all if inventory is yml
Listing host using ansible command |
# inventory set in the ansible.cfg ansible all --list-hosts # a group specific ansible web --list-hosts # checking for a specific host in a group ansible web --list-hosts -l 192.168.33.220
The ansible command list host option with limiting |
ansible-config
Ansible uses configuration definations in the ansible.cfg file located in the /etc/ansible/ansible.cfgansible-config --help show helpThe state of your Ansible configuration settings
ansible-config view
ansible-config list
Ansible configuration list |
You can see another list of configurations but you can also store it in a file
ansible-config dump
ansible configuration dump |
You can get the list of overloaded variable values using :
ansible-config dump --only-changeCustom configurations
ansible-config command on awx docker container |
ansible-console
This command allows for running ad-hoc commands or tasks against a chosen inventory from a shell with built-in tab auto-completion. It will supports several Linux like commands and you can modify its configuration at the runtime. You can get full help about some of the modules which is very simple in this shell.
ansible-console help for 'copy' command |
ansible-console root@all ansibleprompt$ copy /tmp/hello.txt /home/ansibleuser
Task like copying files from the remote managed clients is simple.
You may be confused some commands same as in Linux, but the meaning is different in Ansible. Example 'cd' which change the hosts instead of changing the directory.
ansible-console help of cd command |
You can explore any module from ansible-console and then you can add that into your playbook as task.
ansible-doc
To get help on the plug-ins and modules you can use this ansible-doc command.
ansible-doc modulenameExample1:
ansible-doc stat
ansible-doc command applied against stat module |
Example 2: To get the list of all modules
ansible-doc -l
ansible-doc for list of modules |
ansible-galaxy
The ansible-galaxy command will be used to create roles directory structure offline, you can also download community provided roles from the Galaxy website in online mode
ansible-galaxy search elk Searching for a module is similar to the search for image in docker cli. For example search for 'elk' role on Galaxy
ansible-galaxy search elk
searching for elk roles on galaxy |
ansible-galaxy search elk
elk installation using ansible-galaxy |
ansible-inventory
If there are any host specific variable used then you can use --vars option against a host.
ansible-inventory --host 192.168.33.220 --varsWhich doesnot returns empty because the default inventory having only hosts and groups. # When you define inventory file as:
[vagrant@mstr prod]$ cat prod_inv_host_group [prod] localhost [db] 192.168.33.200 [web] 192.168.33.210 ansible_user=vagrant 192.168.33.220 ansible_user=devadminthen you can see the host vars as follows:
ansible-inventory -i prod_inv_host_group --host 192.168.33.220 --vars# to get the graph which is similar to linux tree command to show the connection between groups and their host association
ansible-inventory --graph
ansible-inventory vars and graph |
ansible-playbook
The actual Ansible automation will be happen with this command. You need to write task in a play then we can execute it by providing the playbook yaml file as argument.--- - name: Hello from DevOpsHunter hosts: localhost tasks: - name: Greeting message debug: msg: "Welcome to DevOps Jungle... Start Hunting..."
No comments:
Post a Comment