SSH [SOLVED]: Conditional Handler within Ansible

SSH [SOLVED]: Conditional Handler within Ansible

Home Forums SSH SSH [SOLVED]: Conditional Handler within Ansible

Tagged: , ,

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #36066

    Anonymous

    QuestionQuestion

    we use a users module that restarts sshd on a handler, however this is only valid if the server is Ubuntu 16.

    I am trying to over ride it like so:

    - name: Set SSH/SSHD service
      include_vars:
        ssh_service: "sshd"
      when: ( ansible_distribution_version == "16.04" ) and ( ansible_distribution == "Ubuntu" )
    
    - name: Set SSH/SSHD service
      include_vars:
        ssh_service: "ssh"
      when: ( ansible_distribution_version == "14.04" ) and ( ansible_distribution == "Ubuntu" )
    

    However this is returning an error regarding debug:

    fatal: [172.48.0.146]: FAILED! => {"failed": true, "msg": "ssh_service is not a valid option in debug"}
    

    The handler is as follows:

    - name: restart_sshd
      service: name="{{ ssh_service }}"
               state=restarted
    

    Is there a tidy way to do this? I don’t want to have to specify this via groupvars.

    #36067

    Anonymous

    Accepted AnswerAnswer

    As per:

    https://fossies.org/linux/ansible/lib/ansible/plugins/action/include_vars.py

    I am not using include_vars by design, it expects a file.

    I can use set_fact instead:

    - name: Set SSH/SSHD service
      set_fact:
        ssh_service: "sshd"
      when: ( ansible_distribution_version == "16.04" ) and ( ansible_distribution == "Ubuntu" )
    

    Source: https://stackoverflow.com/questions/47835748/conditional-handler-within-ansible
    Author: Anthony 'Runt' Cleaves
    Creative Commons License
    This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.