[root@jenkins_master ~]# cat /usr/local/jenkins/playbook/jenkins_ansible_test.yml
---
- name: Run the test playbook
gather_facts: false
hosts: localhost
tasks:
- name: Get the username running this playbook
become: false
local_action: commandwhoami
register: username_on_the_host
- name: Display the User name
ansible.builtin.debug:
msg: "Hello User {{ username_on_the_host.stdout }} from Jenkins and Ansible!!"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
构建项目
查看输出结果
Started by user admin
[Pipeline] Start of Pipeline
[Pipeline]node
Running on Jenkins in /usr/local/jenkins/workspace/Jenkins Pipeline With Ansible Playbook
[Pipeline]{[Pipeline] ws
Running in /usr/local/jenkins/build
[Pipeline]{[Pipeline] timestamps
[Pipeline]{[Pipeline] stage
[Pipeline]{(Ansible PlayBook)[Pipeline] script
[Pipeline]{[Pipeline] ansiblePlaybook
16:14:22 [build] $ /usr/bin/ansible-playbook /usr/local/jenkins/playbook/jenkins_ansible_test.yml -i /etc/ansible/hosts
16:14:23
16:14:23 PLAY [Run the test playbook] ***************************************************
16:14:23
16:14:23 TASK [Get the username running this playbook] **********************************
16:14:23 changed: [localhost -> localhost]16:14:23
16:14:23 TASK [Display the User name] ***************************************************
16:14:23 ok: [localhost]=>{16:14:23 "msg":"Hello User jenkins from Jenkins and Ansible!!"16:14:23 }16:14:23
16:14:23 PLAY RECAP *********************************************************************
16:14:23 localhost :ok=2changed=1unreachable=0failed=0skipped=0rescued=0ignored=016:14:23
[Pipeline]}[Pipeline] // script
[Pipeline]}[Pipeline] // stage
[Pipeline]}[Pipeline] // timestamps
[Pipeline]}[Pipeline] // ws
[Pipeline]}[Pipeline] // node[Pipeline] End of Pipeline
Finished: SUCCESS