Issue
I have a playbook with a bunch of tasks:
vars:
params_ENV_SERVER: "{{ lookup('env', 'ENV_SERVER') }}"
params_UML_SUFFIX: "{{ lookup('env', 'UML_SUFFIX') }}"
tasks:
- name: delete previous files
shell: ssh deploy@{{ params_ENV_SERVER }} sudo rm -rf /opt/jenkins-files/*
become: true
become_user: deploy
- name: create build dir
shell: ssh deploy@{{ params_ENV_SERVER }} sudo mkdir -p /opt/jenkins-files/build
become: true
become_user: deploy
- name: chown build dir
shell: ssh deploy@{{ params_ENV_SERVER }} sudo chown -R deploy:deploy /opt/jenkins-files
become: true
become_user: deploy
Which I calling from Jenkinsfile for PROD and QA env-s:
withEnv(["ENV_SERVER=192.168.1.30","UML_SUFFIX=stage-QA"]) {
sh "ansible-playbook nginx-depl.yml --limit 127.0.0.1"
}
withEnv(["ENV_SERVER=192.168.1.130","UML_SUFFIX=stage-PROD"]) {
sh "ansible-playbook nginx-depl.yml --limit 127.0.0.1"
Is it possible to modify playbook somehow, to execute on QA all tasks and on PROD only 2-nd and 3-rd?
Solution
Is this what are you looking for?
- name: delete previous files
shell: ssh deploy@{{ params_ENV_SERVER }} sudo rm -rf /opt/jenkins-files/*
become: true
become_user: deploy
when: "params_UML_SUFFIX == 'stage-QA'"
- name: create build dir
shell: ssh deploy@{{ params_ENV_SERVER }} sudo mkdir -p /opt/jenkins-files/build
become: true
become_user: deploy
when: "params_UML_SUFFIX == 'stage-QA'" or
"params_UML_SUFFIX == 'stage-PROD'"
- name: chown build dir
shell: ssh deploy@{{ params_ENV_SERVER }} sudo chown -R deploy:deploy /opt/jenkins-files
become: true
become_user: deploy
when: "params_UML_SUFFIX == 'stage-QA'" or
"params_UML_SUFFIX == 'stage-PROD'"
Optionally, "Ansible-way" would be creating the inventory
shell> cat hosts
[prod]
192.168.1.130
[qa]
192.168.1.30
and declare all hosts in the playbook
shell> cat playbook.yml
- hosts: all
tasks:
- debug:
msg: "Delete previous files.
Execute module file on {{ inventory_hostname }}"
when: inventory_hostname in groups.qa
- debug:
msg: "Create build dir.
Execute module file on {{ inventory_hostname }}"
when: inventory_hostname in groups.qa or
inventory_hostname in groups.prod
- debug:
msg: "Chown build dir.
Execute module file on {{ inventory_hostname }}"
when: inventory_hostname in groups.qa or
inventory_hostname in groups.prod
You can omit "become: true" and "become_user: deploy" and declare the remote user on the command-line. For example
shell> ansible-playbook -u deploy -i hosts playbook.yml
gives (abridged)
TASK [debug] ****
skipping: [192.168.1.130]
ok: [192.168.1.30] =>
msg: Delete previous files. Execute module file on 192.168.1.30
TASK [debug] ****
ok: [192.168.1.130] =>
msg: Create build dir. Execute module file on 192.168.1.130
ok: [192.168.1.30] =>
msg: Create build dir. Execute module file on 192.168.1.30
TASK [debug] ****
ok: [192.168.1.30] =>
msg: Chown build dir. Execute module file on 192.168.1.30
ok: [192.168.1.130] =>
msg: Chown build dir. Execute module file on 192.168.1.130
You can limit the execution to particular hosts or groups. For example, the command below would execute on prod group only
shell> ansible-playbook -u deploy -i hosts playbook.yml --limit prod
gives (abridged)
TASK [debug] ****
skipping: [192.168.1.130]
TASK [debug] ****
ok: [192.168.1.130] =>
msg: Create build dir. Execute module file on 192.168.1.130
TASK [debug] ****
ok: [192.168.1.130] =>
msg: Chown build dir. Execute module file on 192.168.1.130
Notes
- "Ansible-way" is to execute modules on the remote hosts.
- Replace the debug tasks with file
- Integrate into one tasks "create build dir" and "chown build dir"
- If you run the playbook as user deploy you can omit the parameter "-u deploy"
Answered By - Vladimir Botka
Answer Checked By - Clifford M. (JavaFixing Volunteer)