@operation(pipeline_facts={
    'link': 'name'
})
def link(
    state, host, name, source=None, present=True, symbolic=True
):

    if present and not source:
        raise OperationError('If present is True source must be provided')

    info = host.fact.link(name)
    commands = []

    # Not a link?
    if info is False:
        raise OperationError('{0} exists and is not a link'.format(name))

    add_cmd = 'ln{0} {1} {2}'.format(
        ' -s' if symbolic else '',
        source, name
    )

    remove_cmd = 'rm -f {0}'.format(name)

    # No link and we want it
    if info is None and present:
        commands.append(add_cmd)

    # It exists and we don't want it
    elif info and not present:
        commands.append(remove_cmd)

    # Exists and want to ensure it's state
    elif info and present:
        # If the source is wrong, remove & recreate the link
        if info['link_target'] != source:
            commands.extend((
                remove_cmd,
                add_cmd
            ))

    return commands
Manage the state of links.
dataset/ETHPy150Open Fizzadar/pyinfra/pyinfra/modules/files.py/link