Skip to content

Commit c911762

Browse files
authored
feat: ansible - update installation options (#1420)
1 parent 6e101c6 commit c911762

File tree

1 file changed

+62
-31
lines changed

1 file changed

+62
-31
lines changed

docs/books/learning_ansible/01-basic.md

Lines changed: 62 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ It uses the **SSH** protocol to remotely configure Linux clients or the **WinRM*
3737

3838
The opening of SSH or WinRM flows to all clients from the Ansible server, makes it a critical element of the architecture that must be carefully monitored.
3939

40-
As Ansible is push-based, it will not keep the state of its targeted servers between each of its executions. On the contrary, it will perform new state checks each time it is executed. It is said to be stateless.
40+
As Ansible is mainly push-based, it will not keep the state of its targeted servers between each of its executions. On the contrary, it will perform new state checks each time it is executed. It is said to be stateless.
4141

4242
It will help you with:
4343

@@ -85,26 +85,59 @@ To offer a graphical interface to your daily use of Ansible, you can install som
8585

8686
## Installation on the management server
8787

88-
Ansible is available in the _EPEL_ repository but comes as version 2.9.21, which is quite old now. You can see how this is done by following along here, but skip the actual installation steps, as we will be installing the latest version. The _EPEL_ is required for both versions, so you can go ahead and install that now:
88+
Ansible is available in the _EPEL_ repository, but may sometimes be too old for the current version, and you'll want to work with a more recent version.
89+
90+
We will therefore consider two types of installation:
91+
92+
* the one based on EPEL repositories
93+
* one based on the `pip` python package manager
94+
95+
The _EPEL_ is required for both versions, so you can go ahead and install that now:
8996

9097
* EPEL installation:
9198

9299
```
93100
$ sudo dnf install epel-release
94101
```
95-
If we were installing Ansible from the _EPEL_ we could do the following:
102+
103+
### Installation from EPEL
104+
105+
If we install Ansible from the _EPEL_, we can do the following:
96106

97107
```
98108
$ sudo dnf install ansible
109+
```
110+
111+
And then verify the installation:
112+
113+
```
99114
$ ansible --version
100-
2.9.21
115+
ansible [core 2.14.2]
116+
config file = /etc/ansible/ansible.cfg
117+
configured module search path = ['/home/rocky/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
118+
ansible python module location = /usr/lib/python3.11/site-packages/ansible ansible collection location = /home/rocky/.ansible/collections:/usr/share/ansible/collections
119+
executable location = /usr/bin/ansible
120+
python version = 3.11.2 (main, Jun 22 2023, 04:35:24) [GCC 8.5.0 20210514
121+
(Red Hat 8.5.0-18)] (/usr/bin/python3.11)
122+
jinja version = 3.1.2
123+
libyaml = True
124+
125+
$ python3 --version
126+
Python 3.6.8
101127
```
128+
129+
Please note that ansible comes with its own version of python, different from the system version of python (here 3.11.2 vs 3.6.8). You'll need to take this into account when pip-installing the python modules required for your installation (e.g. `pip3.11 install PyVMomi`).
130+
131+
### Installation from python pip
132+
102133
As we want to use a newer version of Ansible, we will install it from `python3-pip`:
103134

104135
!!! Note
105136

106137
Remove Ansible if you have installed it previously from _EPEL_.
107138

139+
At this stage, we can choose to install ansible with the version of python we want.
140+
108141
```
109142
$ sudo dnf install python38 python38-pip python38-wheel python3-argcomplete rust cargo curl
110143
```
@@ -114,35 +147,32 @@ $ sudo dnf install python38 python38-pip python38-wheel python3-argcomplete rust
114147
`python3-argcomplete` is provided by _EPEL_. Please install epel-release if not done yet.
115148
This package will help you complete Ansible commands.
116149

117-
Before we actually install Ansible, we need to tell Rocky Linux that we want to use the newly installed version of Python. The reason is that if we continue to the install without this, the default python3 (version 3.6 as of this writing), will be used instead of the newly installed version 3.8. Set the version you want to use by entering the following command:
118-
119-
```
120-
sudo alternatives --set python /usr/bin/python3.8
121-
sudo alternatives --set python3 /usr/bin/python3.8
122-
```
123-
124150
We can now install Ansible:
125151

126152
```
127-
$ sudo pip3 install ansible
128-
$ sudo activate-global-python-argcomplete
153+
$ pip3.8 install --user ansible
154+
$ activate-global-python-argcomplete --user
129155
```
130156

131157
Check your Ansible version:
132158

133159
```
134160
$ ansible --version
135-
ansible [core 2.11.2]
161+
ansible [core 2.13.11]
136162
config file = None
137-
configured module search path = ['/home/ansible/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
138-
ansible python module location = /usr/local/lib/python3.8/site-packages/ansible
139-
ansible collection location = /home/ansible/.ansible/collections:/usr/share/ansible/collections
140-
executable location = /usr/local/bin/ansible
141-
python version = 3.8.6 (default, Jun 29 2021, 21:14:45) [GCC 8.4.1 20200928 (Red Hat 8.4.1-1)]
142-
jinja version = 3.0.1
163+
configured module search path = ['/home/rocky/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
164+
ansible python module location = /home/rocky/.local/lib/python3.8/site-packages/ansible
165+
ansible collection location = /home/rocky/.ansible/collections:/usr/share/ansible/collections
166+
executable location = /home/rocky/.local/bin/ansible
167+
python version = 3.8.16 (default, Jun 25 2023, 05:53:51) [GCC 8.5.0 20210514 (Red Hat 8.5.0-18)]
168+
jinja version = 3.1.2
143169
libyaml = True
144170
```
145171

172+
!!! NOTE
173+
174+
The manually installed version in our case is older than the version packaged by RPM because we used an older version of python. This observation will vary with time and the age of the distribution and the python version of course.
175+
146176
## Configuration files
147177

148178
The server configuration is located under `/etc/ansible`.
@@ -152,19 +182,10 @@ There are two main configuration files:
152182
* The main configuration file `ansible.cfg` where the commands, modules, plugins, and ssh configuration reside;
153183
* The client machine management inventory file `hosts` where the clients, and groups of clients are declared.
154184

155-
As we installed Ansible with `pip`, those files do not exist. We will have to create them by hand.
156-
157-
An example of the `ansible.cfg` [is given here](https://github.com/ansible/ansible/blob/devel/examples/ansible.cfg) and an example of the `hosts` [file here](https://github.com/ansible/ansible/blob/devel/examples/hosts).
158-
159-
```
160-
$ sudo mkdir /etc/ansible
161-
$ sudo curl -o /etc/ansible/ansible.cfg https://raw.githubusercontent.com/ansible/ansible/devel/examples/ansible.cfg
162-
$ sudo curl -o /etc/ansible/hosts https://raw.githubusercontent.com/ansible/ansible/devel/examples/hosts
163-
```
164-
165-
You can also use the `ansible-config` command to generate a new configuration file:
185+
If Ansible was installed via its RPM package, then the configuration file will have been automatically created. With a `pip` installation, this file does not exist. We'll have to create it by hand thanks to the `ansible-config` command:
166186

167187
```
188+
$ ansible-config -h
168189
usage: ansible-config [-h] [--version] [-v] {list,dump,view,init} ...
169190
170191
View ansible configuration.
@@ -185,6 +206,16 @@ ansible-config init --disabled > /etc/ansible/ansible.cfg
185206

186207
The `--disabled` option allows you to comment out the set of options by prefixing them with a `;`.
187208

209+
!!! NOTE
210+
211+
You can also choose to embed the ansible configuration in your code repository, with ansible loading the configuration files it finds in the following order (processing the first file it finds and ignoring the rest):
212+
213+
* if the environment variable `$ANSIBLE_CONFIG` is set, load the specified file.
214+
* `ansible.cfg` if exists in the current directory.
215+
* `~/.ansible.cfg` if exists (in the user’s home directory).
216+
217+
If none of these three files is found, the default file is loaded.
218+
188219
### The inventory file `/etc/ansible/hosts`
189220

190221
As Ansible will have to work with all your equipment to be configured, it is very important to provide it with one (or more) well-structured inventory file(s), which perfectly matches your organization.

0 commit comments

Comments
 (0)