caylorme
caylorme

Reputation: 56

Ansible: Running a single play on a single host more than a single time

I would like to distribute some ansible deployment scripts where a customer only needs to describe their infrastructure and place some important values into an inventory file. On occasion, a single role may need to be applied to a single host more than one time. In order to keep the customer from having to make modifications to the playbook itself, I would like for them to be able to describe the infrastructure similar to the following:

inventory_file

[servers:children]
servers-a
servers-b

[servers-a]
host1 server_port=1337

[servers-b]
host1 server_port=1335

playbook.yml:

---

- name: Set up Servers
  hosts: servers
  roles:
    - role: server_setup

I'm aware of the allow_duplicates meta value for a role and it is enabled for the example 'servers' role. It appears that the role will only be applied once with the above playbook and with the latter defined variable.

I know of one solution, which would be to create additional hostname aliases for the host.

Is anyone familiar with a better solution?

Upvotes: 1

Views: 859

Answers (1)

Gregory Shulov
Gregory Shulov

Reputation: 204

If I understand the question correctly, sometimes server_port value should be 1335 or 1337.

I would not recommend creating an inventory in such a way, as it misleading. If the value of server port is always 1335 or 1337, I would configure a default value in server_setup under defaults\main.yml:

# server_setup\defaults\main.yml
server_port: 1335

If any other value should be configured the playbook can be executed as follows:

ansible-playbook playbook.yml --limit=host1 --extra-vars "server_port=1337"

In this case the customer can run the playbook as many times as needed.

Upvotes: 1

Related Questions