Introduction to Ansible
Ansible is an open source tool that makes it easy to automate deployment with only a few lines of code and doesn’t require an agent installed on the remote host.
Setting up an environment to work with Ansible
While Ansible is available on most Linux distributions, it can also be installed within a virtual environment. The only requirement is that you have a recent Python 3 installed.
$ python3 -m venv venv
$ source venv/bin/activate
(venv) $ pip install ansible
Collecting ansible
Downloading ansible-5.6.0.tar.gz (35.5 MB)
|████████████████████████████████| 35.5 MB 10.4 MB/s
Collecting ansible-core~=2.12.4
Downloading ansible-core-2.12.4.tar.gz (7.8 MB)
|████████████████████████████████| 7.8 MB 17.8 MB/s
...
(venv) $ ansible --version
ansible [core 2.12.4]
config file = /etc/ansible/ansible.cfg
configured module search path = ['/workspace/mastering-linux.com/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /workspace/mastering-linux.com/venv/lib64/python3.10/site-packages/ansible
ansible collection location = /workspace/mastering-linux.com/.ansible/collections:/usr/share/ansible/collections
executable location = /workspace/mastering-linux.com/venv/bin/ansible
python version = 3.10.4 (main, Mar 25 2022, 00:00:00) [GCC 11.2.1 20220127 (Red Hat 11.2.1-9)]
jinja version = 3.1.1
libyaml = True
(venv) $ ansible-config dump
ACTION_WARNINGS(default) = True
AGNOSTIC_BECOME_PROMPT(default) = True
ALLOW_WORLD_READABLE_TMPFILES(default) = False
ANSIBLE_CONNECTION_PATH(default) = None
Configuring Ansible
/etc/ansible/ansible.cfg is a configuration file that contains the default settings for Ansible. These settings can be overridden by specifying a different configuration file in the current working directory or by specifying a different configuration file in the ANSIBLE_CONFIG environment variable. In the example below the checking of the SSH host key is disabled.
(venv) $ cat ansible.cfg
[defaults]
host_key_checking = False
With ansible --version
you can see which configuration file is used.
(venv) $ ansible --version
ansible [core 2.12.4]
config file = /workspace/mastering-linux.com/ansible.cfg
configured module search path = ['/workspace/mastering-linux.com/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
ansible python module location = /workspace/mastering-linux.com/venv/lib64/python3.10/site-packages/ansible
ansible collection location = /workspace/mastering-linux.com/.ansible/collections:/usr/share/ansible/collections
executable location = /workspace/mastering-linux.com/venv/bin/ansible
python version = 3.10.4 (main, Mar 25 2022, 00:00:00) [GCC 11.2.1 20220127 (Red Hat 11.2.1-9)]
Documentation
(venv) $ ansible-doc user
(venv) $ ansible-doc --list | wc -l
(venv) $ ansible-doc -s user
Adhoc commands
(venv) $ ansible localhost -m ping
localhost | SUCCESS => {
"changed": false,
"ping": "pong"
}
Using ansible-console REPL Shell
(venv) $ ansible-console --help
(venv) $ ansible-console -i environment/prod
(venv) $ ansible-console -b