0

I have 2 plays in my sites.yml file:


- name: First play
  hosts: all
  remote_user: root
  roles:
       - role1


- name: Second Play
  hosts: all
  remote_user: ansible
  become: yes
  become_method: sudo
  connection: ssh
  gather_facts: yes
  roles:
     - role2
     - role3

The reason behind this is that, the first play will create ansible user with sudo access. Here, I have used root user to run the play. Then I have to run another play as ansible user.

My question is how to run these play one after another? I first want to run first one then after that second one.

If I run the playbook then it will get stuck because it will first run first play and tries to run second play where it gets stuck.

I am running playbook with the following command:

ansible-playbook -i inventory.ini -l web sites.yml --ask-pass -c paramiko

This is for 1st part.

  • Hello, I don't why the second play won't work, Could you post in the question why it is failing (log, relevant tasks). Having several plays in the same playbook shouldn't be a problem. There is likely a problem in one of the roles you run in the second play, have you tested just running role2 or role3 alone. – Baptiste Mille-Mathias Aug 25 '18 at 17:42
  • @baptistemm I just want to run one play at once. It is because the first one will create ansible user, which will later use second play to run further roles. At this point I wan to skip first play. The title is so because I want to run playbook by skipping second play at first then run it. After it runs successfully I will run second play only by skipping first play. Hope you understand. – Err0rr Aug 26 '18 at 11:00
  • No I don't understand. When you have several plays in a playbooks there are played one by one so I don't see any problem. Read How to Ask and then edit the question to explain clearly the problem you're are trying to solve. – Baptiste Mille-Mathias Aug 26 '18 at 12:16
  • Thanks for your concern. Not willing to change question at this moment. – Err0rr Aug 27 '18 at 6:12
1

Although the comments have highlighted the fact that this playbook is fine, as long as it is designed to do what it needs to do:

The reason behind this is that, the first play will create ansible user with sudo access. Here, I have used root user to run the play. Then I have to run another play as ansible user.

In the first play, you have only one role: role1. If in role1 you have a task that creates the ansible user, then you can use that user to connect the next time. This is actually something many people do, including myself, to put a known baseline on a service:

- name: Ensure Ansible user is present (RedHat)
  user:
   name: ansible
   comment: "ansible user created by bootstrap playbook"
   generate_ssh_key: yes
   groups: wheel
 tags:
 - bootstrap
 when: ansible_os_family=="RedHat"

- name: Ensure Ansible user is present (Debian)
  user:
   name: ansible
   comment: "ansible user created by bootstrap playbook"
   generate_ssh_key: yes
   groups: sudo
  tags:
  - bootstrap
  when: ansible_os_family=="Debian"

- name: update sudoers to ensure ansible user can sudo
  lineinfile:
    dest: /etc/sudoers
    state: present
    regexp: '^ansible'
    line: 'ansible ALL=(ALL) NOPASSWD: ALL'
  tags:
  - bootstrap

This will ensure that the user is added, and included in the right groups, with sudo rights -- it is just an example of how you could achieve this, there are other strategies of course.

My question is how to run these play one after another? I first want to run first one then after that second one.

That's exactly how playbooks work.

If I run the playbook then it will get stuck because it will first run first play and tries to run second play where it gets stuck.

If you do not have a task to configure the user required in the next play, then yes, it will fail (not get stuck). The point is that since Ansible is idempotent, you don't have to worry about skipping the play because if the user is properly configured, the first play will simply have no effect.

However, to answer the actual question

How do I skip a play in an Ansible playbook ?

This is done using the conditional execution using when

  • Thank you for your time. I ended up using two yml files and running one after another. Anyway, it is okay for me to run this way. – Err0rr Nov 11 '18 at 10:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.