Getting Started – Converting Puppet to Ansible
As organizations grow and evolve, so do their infrastructure needs. One area that often requires attention is configuration management, the process of ensuring that all servers and devices in a network are configured consistently and correctly. Two popular tools for configuration management are Puppet and Ansible. In this blog post, we will explore the process of converting from Puppet to Ansible, highlighting the key differences between the two tools and providing step-by-step examples with outputs.
Puppet and Ansible both provide automation for infrastructure management, but there are some key differences between the two. Puppet uses a declarative language, which means that the user specifies the desired end state of the system, and Puppet makes the necessary changes to reach that state. Ansible, on the other hand, uses an imperative language, which means that the user specifies the actions to be taken to reach the desired state. This can make Ansible more intuitive for users who are familiar with programming concepts.
Another key difference is that Puppet uses a client-server architecture, while Ansible uses a push-based architecture. This means that in Puppet, the client (the server being configured) pulls the configuration from the server, while in Ansible, the configuration is pushed to the client. This can make Ansible more suitable for environments with a large number of servers or devices, as it eliminates the need for a dedicated Puppet server.
Now that we have a basic understanding of the differences between Puppet and Ansible, let’s dive into an example of converting from Puppet to Ansible.
Puppet to Ansible – Example 1
Installing and configuring Apache In Puppet, the code for installing and configuring Apache would look something like this:
package { 'httpd':
ensure => installed,
}
file { '/etc/httpd/conf/httpd.conf':
ensure => file,
owner => 'root',
group => 'root',
mode => '0644',
content => template('module/httpd.conf.erb'),
}
In Ansible, the equivalent code would look like this:
- name: Install Apache
yum:
name: httpd
state: present
- name: Configure Apache
template:
src: httpd.conf.j2
dest: /etc/httpd/conf/httpd.conf
owner: root
group: root
mode: 0644
As you can see, the Ansible code is more concise and easier to read. The yum
module is used to install Apache, and the template
module is used to configure it.
Puppet to Ansible – Example 2
Creating a user In Puppet, the code for creating a user would look something like this:
user { 'johndoe':
ensure => present,
uid => '1000',
gid => '1000',
home => '/home/johndoe',
shell => '/bin/bash',
managehome => true,
}
In Ansible, the equivalent code would look like this:
- name: Create user johndoe
user:
name: johndoe
uid: 1000
gid: 1000
home: /home/johndoe
shell: /bin/bash
state: present
createhome: yes
As you can see, the Ansible code is again more concise and easier to read. The user
module is used to create the user, and the state
option is used to ensure that the user is present. The createhome
option is used to create the home directory for the user.
Converting from Puppet to Ansible can be a daunting task, but with a clear understanding of the key differences between the two tools and the use of proper syntax, it can be a relatively straightforward process. By switching to Ansible, organizations can streamline their configuration management and gain the benefits of its push-based architecture and intuitive imperative language.
When applying an Ansible playbook, you will typically use the ansible-playbook
command followed by the name of the playbook file. Here is an example of running an Ansible playbook named playbook.yml
:
$ ansible-playbook playbook.yml
PLAY [all] *********************************************************************
TASK [Gathering Facts] *********************************************************
ok: [192.168.1.1]
ok: [192.168.1.2]
TASK [Install Apache] **********************************************************
ok: [192.168.1.1]
ok: [192.168.1.2]
TASK [Configure Apache] *******************************************************
ok: [192.168.1.1]
ok: [192.168.1.2]
TASK [Create user johndoe] ***************************************************
ok: [192.168.1.1]
ok: [192.168.1.2]
PLAY RECAP *********************************************************************
192.168.1.1 : ok=4 changed=0 unreachable=0 failed=0
192.168.1.2 : ok=4 changed=0 unreachable=0 failed=0
This output shows that the playbook was run successfully on two hosts, IP addresses 192.168.1.1 and 192.168.1.2. The tasks that were executed are listed, and the status of each task is shown as “ok”. The changed
field in the output shows the number of tasks that made changes to the system, while the unreachable
and failed
fields show the number of hosts that were unreachable or failed during the playbook execution.
You can also use the -v
option to increase the verbosity of the output and see more details about the tasks that were executed:
$ ansible-playbook -v playbook.yml
This will give you more detailed output, such as the specific commands that were run and their output, which can be helpful for troubleshooting or understanding what the playbook is doing.
Another option you can use is -i
or --inventory-file
to specify the inventory file, which is a file that contains the list of hosts that the playbook will be applied to.
$ ansible-playbook -i inventory.ini playbook.yml
You can also use -u
or --user
to specify the user to connect as, and -k
or --ask-pass
to prompt for the password to use for authentication.
$ ansible-playbook -u user -k playbook.yml
Summary
In summary, the ansible-playbook
command is used to apply Ansible playbooks. The output provides information about the execution of the playbook, including the tasks that were executed and their status. Additionally, you can use options like -v
for verbosity, -i
for specifying the inventory file, -u
for specifying the user, and -k
for prompting for the password.
Overall, while both Puppet and Ansible have their own strengths and weaknesses, it’s important to evaluate and compare their features, use cases, and your organization’s needs before making a decision. Ansible’s simplicity and flexibility makes it a great choice for organizations looking to streamline and automate their infrastructure management.
Comment below if you want to see more!
Looking for more?
Check out our DevOps and SRE section.
Thanks for reading!
Leave a Reply
You must be logged in to post a comment.