Security Automation with Ansible 2
上QQ阅读APP看书,第一时间看更新

Setting up Jenkins

Let's use an Ansible playbook to install Jenkins and get started with it. 

The following code snippet is a snippet of an Ansible playbook we wrote for setting up Jenkins in the Ubuntu 16.04 OS.

Once the setup has been done, playbook returns the default administrator password required to log in to the application for the first time:

- name: installing jenkins in ubuntu 16.04
hosts: "192.168.1.7"
remote_user: ubuntu
gather_facts: False
become: True

tasks:
- name: install python 2
raw: test -e /usr/bin/python || (apt -y update && apt install -y python-minimal)

- name: install curl and git
apt: name={{ item }} state=present update_cache=yes

with_items:
- curl
- git

  - name: adding jenkins gpg key
    apt_key:
      url: https://pkg.jenkins.io/debian/jenkins-ci.org.key
      state: present

  - name: jeknins repository to system
    apt_repository:
      repo: http://pkg.jenkins.io/debian-stable binary/
      state: present

  - name: installing jenkins
    apt:
      name: jenkins
      state: present
      update_cache: yes

- name: adding jenkins to startup service: name: jenkins state: started enabled: yes - name: printing jenkins default administration password command: cat /var/lib/jenkins/secrets/initialAdminPassword register: jenkins_default_admin_password - debug: msg: "{{ jenkins_default_admin_password.stdout }}"

To set up Jenkins, run the following command. Where 192.168.1.7 is the server IP address where Jenkins will be installed:

ansible-playbook -i '192.168.1.7,' site.yml --ask-sudo-pass

Now we can configure Jenkins to install plugins, run scheduled jobs, and do many other things. First, we have to navigate to the Jenkins dashboard by browsing to http://192.168.1.7:8080 and providing the auto-generated password. If the playbook runs without any errors, it will display the password at the end of the play:

Create the new user by filling in the details and confirming to log in to the Jenkins console:

Now we can install custom plugins in Jenkins, navigate to the Manage Jenkins tab, select Manage Plugins, then navigate to the Available tab. In the Filter: enter the plugin name as Ansible. Then select the checkbox and click Install without restart:

Now we are ready to work with the Ansible plugin for Jenkins. Create a new project in the main dashboard, give it a name, and select Freestyle project to proceed:

Now we can configure the build options, this is where Jenkins will give us more flexibility to define our own triggers, build instructions, and post build scripts:

The preceding screenshot is an example of a build invoking an Ansible ad-hoc command. This can be modified to ansible-playbook or any other scripts based on certain events.

The Jenkins Ansible plugin also provides useful features such as configuring advanced commands and passing credentials, keys from Jenkins itself.

Once the build triggers based on an event, this can be sent to some artifact storage, it can also be available in the Jenkins build console output:

This is a really very powerful way to perform dynamic operations such as triggering automated server and stacks setup based on a code push to the repository, as well as scheduled scans and automated reporting.