Published on

Installing Ansible

Authors
  • Name
    Eche Ngbede
    Twitter

// Checking the repo list for the latest Ansible platform

sudo subscription-manager repos --list | grep ansible

sudo subscription-manager repos --enable=ansible-automation-platform-2.3-for-rhel-9-x86_64-rpms
sudo dnf repolist
sudo dnf install ansible-core
sudo dnf install ansible-navigator
ansible --version

Defining inventory

An inventory file is a list of host names or IP addresses. The hosts can be assigned to groups, which can be managed collectively; Groups can contain child groups, and hosts can be members of multiple groups. Static inventory is a text file, and dynamic inventory is defined by a script. The location of the inventory is controlled by the current ansible conf file /etc/ansible/ansible.cfg. by default /etc/ansible/hosts

web01.com
web02.com
db01.com
db02.com
nfs.example.com
sftp.example.com
10.34.90.56

# Defined two host groups
[dbservers]
db01.com
db02.com

[webserver]
web01.cm
web02.com

# Hosts can be defined in multiple groups

[dbservers]
db01.com
db02.com

[webserver]
web01.cm
web02.com

[production]
db01.com
web01.com
10.34.90.56
sftp.example.com

[west-dc]
web02.com
db01.com
10.34.90.56

# nested group
[azure-east]
sfp.example.com
db02.com

[azure-west]
nfs.example.com
web02.com

[azure:children]
azure-east
azure-west
ansible all--list-hosts

The ansible and ansible-playbook commands that you use to run Ansible ad hoc commands and playbooks later in the course can also specify the location of an inventory file on the command line with the --inventory PATHNAME or -i PATHNAME option, where PATHNAME is the path to the desired inventory file.

# Configure managed node

$ ansible -i inventory all -u <user> -k -b -K -m user -a "name=ansible"
$ ansible -i inventory all -u <user> -k -b -K -m shell -a "echo password | passwd --stdin ansible"
$ ansible all -u <user> -k -b -K -m shell -a " echo 'ansible ALL=(ALL) NOPASSWD:ALL' > /etc/sudoers.d/ansible"

# -u - user name on the remote server
# -k - password (password base authentic)
# -b - become  (sudo alternative)
# -K - enter password
$ ssh-keygen
$ ssh-copy-id user@server

ansible config file

• [defaults] sets defaults for Ansible operation • [privilege_escalation] configures how Ansible performs privilege escalation on managed hosts

[defaults]
inventory = ./inventory
remote_user = user
ask_pass = false

[privilege_escalation]
become = true
become_method = sudo
become_user = root
become_ask_pass = false
  • inventory - Specifies the path to the inventory file.
  • remote_user - The name of the user to log in as on the managed hosts. If not specified, the current user's name is used.
  • ask_pass - Whether or not to prompt for an SSH password. Can be false if using SSH public key authentication.
  • become - Whether to automatically switch user on the managed host (typically to root) after connecting. This can also be specified by a play.
  • become_method - How to switch user (typically sudo, which is the default, but su is an option).
  • become_user - The user to switch to on the managed host (typically root, which is the default).
  • become_ask_pass- Whether to prompt for a password for your become_method. Defaults to false.