Topics
Red Hat Ansible. Ansible is an open source community project sponsored by Red Hat, it's the simplest way to automate IT. Ansible is the only automation language that can be used across entire IT teams from systems and network administrators to developers. The preferred way to install Ansible on a Mac is with pip. The instructions can be found in Installing Ansible with pip. If you are running macOS version 10.12 or older, then you should upgrade to the latest pip to connect to the Python Package Index securely. Here's howto install virtualenv: on Mac: sudo easyinstall virtualenv1.10.1 on RedHat: sudo yum install -y python-virtualenv Here is how to setup ansible with virtualenv: virtualenv MyAnsible MyAnsible/bin/pip install ansible virtualenv -relocatable MyAnsible – bbaassssiiee Jan 29 '14 at 8:28.
- Installing content
- Roles
This topic describes how to download and install Ansible content from Galaxy.
Use the
ansible-galaxy
command to download roles from the Galaxy server.For example, the following downloads the debops.apt role:When Ansible is first installed, it defaults to installing content in /etc/ansible/roles, which requiresroot privileges.
The first way to override the default behavior is to use the –roles-path option on the command line, asdemonstrated by the following example:
Override the default behavior by setting the environment variable
ANSIBLE_ROLES_PATH
. When set, theANSIBLE_ROLES_PATH variable is used during playbook execution to locate installed roles, and by ansible-galaxy
to determine where to install roles. It can be set to a single directory path, or to a list of paths(e.g., /etc/ansible/roles:~/.ansible/roles). If set to a list, ansible-galaxy
will install roles tothe first writable path.![Mac Mac](/uploads/1/3/4/1/134188192/135412255.jpg)
Ansible also supports a configuration file,where
roles_path
can be set. Setting the value of roles_path
behaves the same as setting theANSIBLE_ROLES_PATH environment variable.When the Galaxy server imports a role, it imports any git tags matching the Semantic Version format asversions. In turn, a specific version of a role can be downloaded by specifying one of the imported tags.
To see the available versions, locate the role on the search page, and click on the name to view more details. Youcan also navigate directly to the role using the /<namespace>/<role name>. For example, to view therole geerlingguy.apache, go to https://galaxy.ansible.com/geerlingguy/apache.
Install a specific version of a role by appending a comma and a version tag. For example, the following installs v1.0.0 of therole.
It’s also possible to point directly to the git repository and specify a branch name or commit hash as the version.For example, the following installs a specific commit:
You can use the
ansible-galaxylist
command to list all the roles and role versions you have installed.Multiple roles can be installed by listing them in a requirements.yml file. The format of the file isYAML, and the file extension must be either .yml or .yaml.
Use the following command to install roles included in requirements.yml:
Each role in the file will have one or more of the following attributes:
- src
- The source of the role, and a required attribute. Specify a role from Galaxy by using the formatnamespace.role_name, or provide a URL to a repository within a git based SCM.
- scm
- If the src is a URL, specify the SCM. Only git or hg are supported. Defaults to git.
- version:
- The version of the role to download. Provide a tag value, commit hash, or branch name.Defaults to master.
- name:
- Download the role to a specific name. Defaults to the Galaxy name when downloading from Galaxy,or the name of the repository, when src is a URL.
The following example provides a guide for listing roles in a requirements.yml file:
Using the include directive, additional YAML files can be included into a single requirements.ymlfile. For large projects, this provides the ability to split a large file into multiple smaller files.
For example, a project may have a requirements.yml file, and a webserver.yml file. The followingshows the contents of the requirements.yml file:
Below are the contents of the webserver.yml file:
To install all the roles from both files, pass the root file, in this case requirements.yml on thecommand line, as demonstrated by the following:
Roles can be dependent on roles, and when a role is installed, any dependencies are automatically installedas well.
Dependencies are listed in a role’s
meta/main.yml
file, using the top-level dependencies keyword.The following shows an example meta/main.yml
file with dependent roles:If the source of a role is Galaxy, specify the role in the format namespace.role_name, as shown in theabove example. The more complex format used in requirements.yml is also supported, as demonstrated bythe following:
To understand how dependencies are handled during playbook execution, view the Role Dependencies topic atthe Ansible doc site.
Note
Galaxy expects all role dependencies to exist in Galaxy, and therefore dependencies to be specifiedusing the namespace.role_name format.
See Installing collections in the Ansible documentation for detailed information about installing and using collections from Galaxy.
At my company, one of our services is to develop and maintain Wordpress websites for our clients. We host our clients' website using Digitalocean and we want to separate customer environment easily so we use different droplet for each customer. However, the pre-installed version from the DigitalOcean image is 7.0.X and it was deprecated.
Save by Ansible
Ansible is an IT Automation tool by Redhat which lets you automate IT tasks with ease. And did I mention, its free
While you can do a load of things using Ansible, we will only focus on upgrading PHP on our servers. Be sure to check out what you can do with ansible afterward.
Installation
First, you gotta install Ansible control node. On a bigger scale, this would be on a server who manages other ansible nodes (other servers) through SSH. However, for our case; we will use our own computer as a control node.
Ansible works on any machine that can run Python 2 or 3, except for Windows. Use the Linux subsystem or something else if you are running Windows.
For Mac users, the preferred way is to install ansible using pip
If you are using Ubuntu 16.04 or 18.04 run the command below
For other distros or OS, check out the official installation tutorial
Setup your inventory
In Ansible, you identify the location of your servers in the inventory file located at
/etc/ansible/hosts
Create your inventory file
Above, you created a hosts group called wordpress which contains website-a and website-b
You can also set ansible_password= but I recommended you to set up the public key
Create your playbook
Next up we'll create an orchestration file to tell ansible what to do. Create a file name
upgrade-php.yml
What happening here is we're telling ansible to run set of tasks (roles) geerlingguy.php-versions, and geerlingguy.php.
Just like NPM or pip, you need to download the package from an online repo in order to use it. Run these commands
This awesome role by geerlingguy purge old PHP versions, add the appropriate repository, install your PHP and install all the recommendations modules. You can review what the role does here
Upgrade the server PHP
Finally, we are ready to upgrade PHP on our servers. Run this command:
Ansible Apt Install
Wait for the playbook to finish and your PHP version should be upgraded to 7.3 ???
Ansible Install Package
Ansible has a lot more awesome features that make your sysadmin mundane tasks go away in one command. I'm new to ansible and wish to have known about it sooner. If you have any cool ansible project, then please tell me about it in the comment.